Skip to content

Commit 5bede4b

Browse files
authored
Merge pull request #16 from maximehuran/feature/update-media-manager
2 parents 4be775b + 5fd2cfe commit 5bede4b

File tree

9 files changed

+245
-202
lines changed

9 files changed

+245
-202
lines changed

.php-cs-fixer.dist.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@
3636
->setRiskyAllowed(true)
3737
->setRules([
3838
'@DoctrineAnnotation' => true,
39-
'@PHP71Migration' => true,
40-
'@PHP71Migration:risky' => true,
41-
'@PHPUnit60Migration:risky' => true,
39+
'@PHP7x1Migration' => true,
40+
'@PHP7x1Migration:risky' => true,
41+
'@PHPUnit6x0Migration:risky' => true,
4242
'@Symfony' => true,
4343
'@Symfony:risky' => true,
4444
'align_multiline_comment' => [
@@ -256,7 +256,7 @@
256256
'trim_array_spaces' => true,
257257
'type_declaration_spaces' => true,
258258
'unary_operator_spaces' => true,
259-
'visibility_required' => [
259+
'modifier_keywords' => [
260260
'elements' => [
261261
'const',
262262
'property',

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"php": "^8.2",
99
"sylius/sylius": "~2.0",
1010
"monsieurbiz/sylius-rich-editor-plugin": "^3.0",
11-
"monsieurbiz/sylius-media-manager-plugin": "^2.0"
11+
"monsieurbiz/sylius-media-manager-plugin": "^3.0"
1212
},
1313
"require-dev": {
1414
"friendsofphp/php-cs-fixer": "^3.16",

src/Entity/Author.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
namespace MonsieurBiz\SyliusBlogPlugin\Entity;
1515

16-
class Author implements AuthorInterface
16+
use Stringable;
17+
18+
class Author implements Stringable, AuthorInterface
1719
{
1820
private ?int $id = null;
1921

src/Fixture/Factory/ArticleFixtureFactory.php

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@
2121
use MonsieurBiz\SyliusBlogPlugin\Entity\ArticleInterface;
2222
use MonsieurBiz\SyliusBlogPlugin\Entity\ArticleTranslationInterface;
2323
use MonsieurBiz\SyliusBlogPlugin\Repository\TagRepositoryInterface;
24-
use MonsieurBiz\SyliusMediaManagerPlugin\Exception\FileNotFoundException;
25-
use MonsieurBiz\SyliusMediaManagerPlugin\Operator\DirectoryOperatorInterface;
26-
use MonsieurBiz\SyliusMediaManagerPlugin\Repository\FileRepositoryInterface;
24+
use RuntimeException;
2725
use Sylius\Abstraction\StateMachine\StateMachineInterface;
2826
use Sylius\Bundle\CoreBundle\Fixture\Factory\AbstractExampleFactory;
2927
use Sylius\Bundle\CoreBundle\Fixture\OptionsResolver\LazyOption;
@@ -34,7 +32,6 @@
3432
use Sylius\Component\Resource\Factory\FactoryInterface;
3533
use Sylius\Component\Resource\Repository\RepositoryInterface;
3634
use Symfony\Component\Config\FileLocatorInterface;
37-
use Symfony\Component\HttpFoundation\File\UploadedFile;
3835
use Symfony\Component\OptionsResolver\Options;
3936
use Symfony\Component\OptionsResolver\OptionsResolver;
4037

@@ -58,8 +55,6 @@ public function __construct(
5855
private ChannelRepositoryInterface $channelRepository,
5956
private RepositoryInterface $authorRepository,
6057
private FileLocatorInterface $fileLocator,
61-
private FileRepositoryInterface $fileRepository,
62-
private DirectoryOperatorInterface $directoryOperator,
6358
private string $defaultLocaleCode,
6459
private string $publicDir,
6560
) {
@@ -267,6 +262,9 @@ private function lazyImageDefault(int $chanceOfRandomOne): Closure
267262
};
268263
}
269264

265+
/**
266+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
267+
*/
270268
private function getFilePath(?string $imagePath, string $folder): ?string
271269
{
272270
if (null === $imagePath) {
@@ -279,21 +277,31 @@ private function getFilePath(?string $imagePath, string $folder): ?string
279277
return $existingImage;
280278
}
281279

282-
$file = new UploadedFile($sourcePath, basename($sourcePath));
280+
$filename = basename($sourcePath);
283281
$absoluteFolder = $this->publicDir . '/media/gallery/' . $folder . '/blog/';
284-
$this->directoryOperator->addUploadedFile($absoluteFolder, $file);
285282

286-
return 'gallery/' . $folder . '/blog/' . $file->getClientOriginalName();
283+
if (!is_dir($absoluteFolder)) {
284+
mkdir($absoluteFolder, 0777, true);
285+
}
286+
287+
$destinationPath = $absoluteFolder . $filename;
288+
if (!copy($sourcePath, $destinationPath)) {
289+
throw new RuntimeException(\sprintf('Failed to copy file "%s" to "%s"', $sourcePath, $destinationPath));
290+
}
291+
292+
return 'gallery/' . $folder . '/blog/' . $filename;
287293
}
288294

289295
private function findExistingFile(string $filename, string $folder): ?string
290296
{
291297
$absoluteFolder = $this->publicDir . '/media/gallery/' . $folder . '/blog/';
292298

293-
try {
294-
return $this->fileRepository->findOneFromPath($absoluteFolder . $filename)->getPath();
295-
} catch (FileNotFoundException) {
296-
$this->directoryOperator->createDirectory($absoluteFolder); // Create the folder if it does not exist
299+
if (!is_dir($absoluteFolder)) {
300+
mkdir($absoluteFolder, 0777, true); // Create the folder if it does not exist
301+
}
302+
$filePath = $absoluteFolder . $filename;
303+
if (file_exists($filePath)) {
304+
return 'gallery/' . $folder . '/blog/' . $filename;
297305
}
298306

299307
return null;

src/Fixture/Factory/AuthorFixtureFactory.php

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,10 @@
1717
use Faker\Factory;
1818
use Faker\Generator;
1919
use MonsieurBiz\SyliusBlogPlugin\Entity\AuthorInterface;
20-
use MonsieurBiz\SyliusMediaManagerPlugin\Exception\FileNotFoundException;
21-
use MonsieurBiz\SyliusMediaManagerPlugin\Operator\DirectoryOperatorInterface;
22-
use MonsieurBiz\SyliusMediaManagerPlugin\Repository\FileRepositoryInterface;
20+
use RuntimeException;
2321
use Sylius\Bundle\CoreBundle\Fixture\Factory\AbstractExampleFactory;
2422
use Sylius\Component\Resource\Factory\FactoryInterface;
2523
use Symfony\Component\Config\FileLocatorInterface;
26-
use Symfony\Component\HttpFoundation\File\UploadedFile;
2724
use Symfony\Component\OptionsResolver\Options;
2825
use Symfony\Component\OptionsResolver\OptionsResolver;
2926

@@ -36,8 +33,6 @@ final class AuthorFixtureFactory extends AbstractExampleFactory
3633
public function __construct(
3734
private FactoryInterface $authorFactory,
3835
private FileLocatorInterface $fileLocator,
39-
private FileRepositoryInterface $fileRepository,
40-
private DirectoryOperatorInterface $directoryOperator,
4136
private string $publicDir,
4237
) {
4338
$this->faker = Factory::create();
@@ -93,6 +88,9 @@ private function lazyImageDefault(int $chanceOfRandomOne): Closure
9388
};
9489
}
9590

91+
/**
92+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
93+
*/
9694
private function getImagePath(?string $imagePath): ?string
9795
{
9896
if (null === $imagePath) {
@@ -105,21 +103,32 @@ private function getImagePath(?string $imagePath): ?string
105103
return $existingImage;
106104
}
107105

108-
$file = new UploadedFile($sourcePath, basename($sourcePath));
106+
$filename = basename($sourcePath);
109107
$absoluteFolder = $this->publicDir . '/media/gallery/images/author/';
110-
$this->directoryOperator->addUploadedFile($absoluteFolder, $file);
111108

112-
return 'gallery/images/author/' . $file->getClientOriginalName();
109+
if (!is_dir($absoluteFolder)) {
110+
mkdir($absoluteFolder, 0777, true);
111+
}
112+
113+
$destinationPath = $absoluteFolder . $filename;
114+
if (!copy($sourcePath, $destinationPath)) {
115+
throw new RuntimeException(\sprintf('Failed to copy file "%s" to "%s"', $sourcePath, $destinationPath));
116+
}
117+
118+
return 'gallery/images/author/' . $filename;
113119
}
114120

115121
private function findExistingImage(string $filename): ?string
116122
{
117123
$absoluteFolder = $this->publicDir . '/media/gallery/images/author/';
118124

119-
try {
120-
return $this->fileRepository->findOneFromPath($absoluteFolder . $filename)->getPath();
121-
} catch (FileNotFoundException) {
122-
$this->directoryOperator->createDirectory($absoluteFolder); // Create the folder if it does not exist
125+
if (!is_dir($absoluteFolder)) {
126+
mkdir($absoluteFolder, 0777, true); // Create the folder if it does not exist
127+
}
128+
129+
$filePath = $absoluteFolder . $filename;
130+
if (file_exists($filePath)) {
131+
return 'gallery/images/author/' . $filename;
123132
}
124133

125134
return null;

src/Menu/BlogListUrlProvider.php

Lines changed: 54 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -21,65 +21,71 @@
2121
use Symfony\Contracts\Translation\TranslatorInterface;
2222
use Webmozart\Assert\Assert;
2323

24-
class BlogListUrlProvider extends AbstractUrlProvider
25-
{
26-
public const PROVIDER_CODE = 'blog_list';
24+
if (class_exists(AbstractUrlProvider::class)) {
25+
class BlogListUrlProvider extends AbstractUrlProvider
26+
{
27+
public const PROVIDER_CODE = 'blog_list';
2728

28-
protected string $code = self::PROVIDER_CODE;
29+
protected string $code = self::PROVIDER_CODE;
2930

30-
protected string $icon = 'tabler:news';
31+
protected string $icon = 'tabler:news';
3132

32-
protected int $priority = 35;
33+
protected int $priority = 35;
3334

34-
public function __construct(
35-
RouterInterface $router,
36-
private TagRepositoryInterface $tagRepository,
37-
private TranslatorInterface $translator
38-
) {
39-
parent::__construct($router);
40-
}
35+
public function __construct(
36+
RouterInterface $router,
37+
private TagRepositoryInterface $tagRepository,
38+
private TranslatorInterface $translator
39+
) {
40+
parent::__construct($router);
41+
}
4142

42-
protected function getResults(string $locale, string $search = ''): iterable
43-
{
44-
$queryBuilder = $this->tagRepository->createEnabledListQueryBuilderByType($locale, ArticleInterface::BLOG_TYPE);
43+
protected function getResults(string $locale, string $search = ''): iterable
44+
{
45+
$queryBuilder = $this->tagRepository->createEnabledListQueryBuilderByType($locale, ArticleInterface::BLOG_TYPE);
46+
47+
if (!empty($search)) {
48+
$queryBuilder
49+
->andWhere('translation.name LIKE :search OR translation.slug LIKE :search')
50+
->setParameter('search', '%' . $search . '%')
51+
;
52+
}
53+
54+
$queryBuilder->setMaxResults($this->getMaxResults());
55+
56+
/** @phpstan-ignore-next-line */
57+
return $queryBuilder->getQuery()->getResult();
58+
}
4559

46-
if (!empty($search)) {
47-
$queryBuilder
48-
->andWhere('translation.name LIKE :search OR translation.slug LIKE :search')
49-
->setParameter('search', '%' . $search . '%')
50-
;
60+
protected function addItemFromResult(object $result, string $locale): void
61+
{
62+
Assert::isInstanceOf($result, TagInterface::class);
63+
/** @var TagInterface $result */
64+
$result->setCurrentLocale($locale);
65+
$this->addItem(
66+
(string) $result->getName(),
67+
$this->router->generate('monsieurbiz_blog_tag_show', ['slug' => $result->getSlug(), '_locale' => $locale])
68+
);
5169
}
5270

53-
$queryBuilder->setMaxResults($this->getMaxResults());
71+
public function getItems(string $locale, string $search = ''): array
72+
{
73+
parent::getItems($locale, $search);
5474

55-
/** @phpstan-ignore-next-line */
56-
return $queryBuilder->getQuery()->getResult();
57-
}
75+
// Add item to link to all articles
76+
$firstItemLabel = $this->translator->trans('sylius.ui.all', [], null, $locale);
77+
if (empty($search) || false !== strpos($search, $firstItemLabel)) {
78+
$this->addItem($firstItemLabel, $this->router->generate('monsieurbiz_blog_index', ['_locale' => $locale]));
79+
// Add this last element to the beginning of the array
80+
$lastElement = array_pop($this->items);
81+
array_unshift($this->items, $lastElement);
82+
}
5883

59-
protected function addItemFromResult(object $result, string $locale): void
60-
{
61-
Assert::isInstanceOf($result, TagInterface::class);
62-
/** @var TagInterface $result */
63-
$result->setCurrentLocale($locale);
64-
$this->addItem(
65-
(string) $result->getName(),
66-
$this->router->generate('monsieurbiz_blog_tag_show', ['slug' => $result->getSlug(), '_locale' => $locale])
67-
);
84+
return $this->items;
85+
}
6886
}
69-
70-
public function getItems(string $locale, string $search = ''): array
87+
} else {
88+
class BlogListUrlProvider
7189
{
72-
parent::getItems($locale, $search);
73-
74-
// Add item to link to all articles
75-
$firstItemLabel = $this->translator->trans('sylius.ui.all', [], null, $locale);
76-
if (empty($search) || false !== strpos($search, $firstItemLabel)) {
77-
$this->addItem($firstItemLabel, $this->router->generate('monsieurbiz_blog_index', ['_locale' => $locale]));
78-
// Add this last element to the beginning of the array
79-
$lastElement = array_pop($this->items);
80-
array_unshift($this->items, $lastElement);
81-
}
82-
83-
return $this->items;
8490
}
8591
}

0 commit comments

Comments
 (0)