1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Odiseo\SyliusBlogPlugin\Fixture\Factory; |
6
|
|
|
|
7
|
|
|
use Faker\Factory; |
8
|
|
|
use Odiseo\BlogBundle\Model\ArticleCategoryInterface; |
9
|
|
|
use Sylius\Bundle\CoreBundle\Fixture\Factory\AbstractExampleFactory; |
10
|
|
|
use Sylius\Component\Core\Formatter\StringInflector; |
11
|
|
|
use Sylius\Component\Locale\Model\LocaleInterface; |
12
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
13
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
14
|
|
|
use Symfony\Component\OptionsResolver\Options; |
15
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
16
|
|
|
|
17
|
|
|
final class BlogArticleCategoryExampleFactory extends AbstractExampleFactory |
18
|
|
|
{ |
19
|
|
|
/** @var FactoryInterface */ |
20
|
|
|
private $articleCategoryFactory; |
21
|
|
|
|
22
|
|
|
/** @var RepositoryInterface */ |
23
|
|
|
private $localeRepository; |
24
|
|
|
|
25
|
|
|
/** @var \Faker\Generator */ |
26
|
|
|
private $faker; |
27
|
|
|
|
28
|
|
|
/** @var OptionsResolver */ |
29
|
|
|
private $optionsResolver; |
30
|
|
|
|
31
|
|
|
public function __construct( |
32
|
|
|
FactoryInterface $articleCategoryFactory, |
33
|
|
|
RepositoryInterface $localeRepository |
34
|
|
|
) { |
35
|
|
|
$this->articleCategoryFactory = $articleCategoryFactory; |
36
|
|
|
$this->localeRepository = $localeRepository; |
37
|
|
|
|
38
|
|
|
$this->faker = Factory::create(); |
39
|
|
|
$this->optionsResolver = new OptionsResolver(); |
40
|
|
|
|
41
|
|
|
$this->configureOptions($this->optionsResolver); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public function create(array $options = []): ArticleCategoryInterface |
48
|
|
|
{ |
49
|
|
|
$options = $this->optionsResolver->resolve($options); |
50
|
|
|
|
51
|
|
|
/** @var ArticleCategoryInterface $articleCategory */ |
52
|
|
|
$articleCategory = $this->articleCategoryFactory->createNew(); |
53
|
|
|
$articleCategory->setCode($options['code']); |
54
|
|
|
$articleCategory->setEnabled($options['enabled']); |
55
|
|
|
|
56
|
|
|
foreach ($this->getLocales() as $localeCode) { |
57
|
|
|
$articleCategory->setCurrentLocale((string) $localeCode); |
58
|
|
|
$articleCategory->setFallbackLocale((string) $localeCode); |
59
|
|
|
|
60
|
|
|
$articleCategory->setTitle($options['title']); |
61
|
|
|
$articleCategory->setSlug($options['slug']); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $articleCategory; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private function getLocales(): \Generator |
68
|
|
|
{ |
69
|
|
|
/** @var LocaleInterface[] $locales */ |
70
|
|
|
$locales = $this->localeRepository->findAll(); |
71
|
|
|
foreach ($locales as $locale) { |
72
|
|
|
yield $locale->getCode(); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
|
|
protected function configureOptions(OptionsResolver $resolver): void |
80
|
|
|
{ |
81
|
|
|
$resolver |
82
|
|
|
->setDefault('code', function (Options $options): string { |
83
|
|
|
return StringInflector::nameToCode((string) $options['title']); |
84
|
|
|
}) |
85
|
|
|
->setDefault('enabled', function (Options $options): bool { |
|
|
|
|
86
|
|
|
return $this->faker->boolean(90); |
87
|
|
|
}) |
88
|
|
|
->setAllowedTypes('enabled', 'bool') |
89
|
|
|
->setDefault('title', function (Options $options): string { |
|
|
|
|
90
|
|
|
return $this->faker->text(20); |
91
|
|
|
}) |
92
|
|
|
->setDefault('slug', function (Options $options): string { |
93
|
|
|
return StringInflector::nameToCode((string) $options['title']); |
94
|
|
|
}) |
95
|
|
|
; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.