These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the Sylius package. |
||
5 | * |
||
6 | * (c) Paweł Jędrzejewski |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | declare(strict_types=1); |
||
13 | |||
14 | namespace Sylius\Bundle\CoreBundle\Fixture\Factory; |
||
15 | |||
16 | use Sylius\Component\Core\Formatter\StringInflector; |
||
17 | use Sylius\Component\Core\Model\TaxonInterface; |
||
18 | use Sylius\Component\Locale\Model\LocaleInterface; |
||
19 | use Sylius\Component\Resource\Factory\FactoryInterface; |
||
20 | use Sylius\Component\Resource\Repository\RepositoryInterface; |
||
21 | use Sylius\Component\Taxonomy\Generator\TaxonSlugGeneratorInterface; |
||
22 | use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface; |
||
23 | use Symfony\Component\OptionsResolver\Options; |
||
24 | use Symfony\Component\OptionsResolver\OptionsResolver; |
||
25 | |||
26 | class TaxonExampleFactory extends AbstractExampleFactory implements ExampleFactoryInterface |
||
27 | { |
||
28 | /** |
||
29 | * @var FactoryInterface |
||
30 | */ |
||
31 | private $taxonFactory; |
||
32 | |||
33 | /** |
||
34 | * @var TaxonRepositoryInterface |
||
35 | */ |
||
36 | private $taxonRepository; |
||
37 | |||
38 | /** |
||
39 | * @var RepositoryInterface |
||
40 | */ |
||
41 | private $localeRepository; |
||
42 | |||
43 | /** |
||
44 | * @var \Faker\Generator |
||
45 | */ |
||
46 | private $faker; |
||
47 | |||
48 | /** |
||
49 | * @var TaxonSlugGeneratorInterface |
||
50 | */ |
||
51 | private $taxonSlugGenerator; |
||
52 | |||
53 | /** |
||
54 | * @var OptionsResolver |
||
55 | */ |
||
56 | private $optionsResolver; |
||
57 | |||
58 | /** |
||
59 | * @param FactoryInterface $taxonFactory |
||
60 | * @param TaxonRepositoryInterface $taxonRepository |
||
61 | * @param RepositoryInterface $localeRepository |
||
62 | * @param TaxonSlugGeneratorInterface $taxonSlugGenerator |
||
63 | */ |
||
64 | public function __construct( |
||
65 | FactoryInterface $taxonFactory, |
||
66 | TaxonRepositoryInterface $taxonRepository, |
||
67 | RepositoryInterface $localeRepository, |
||
68 | TaxonSlugGeneratorInterface $taxonSlugGenerator |
||
69 | ) { |
||
70 | $this->taxonFactory = $taxonFactory; |
||
71 | $this->taxonRepository = $taxonRepository; |
||
72 | $this->localeRepository = $localeRepository; |
||
73 | $this->taxonSlugGenerator = $taxonSlugGenerator; |
||
74 | |||
75 | $this->faker = \Faker\Factory::create(); |
||
76 | $this->optionsResolver = new OptionsResolver(); |
||
77 | |||
78 | $this->configureOptions($this->optionsResolver); |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | */ |
||
84 | public function create(array $options = []): TaxonInterface |
||
85 | { |
||
86 | $options = $this->optionsResolver->resolve($options); |
||
87 | |||
88 | /** @var TaxonInterface $taxon */ |
||
89 | $taxon = $this->taxonRepository->findOneBy(['code' => $options['code']]); |
||
90 | |||
91 | if (null === $taxon) { |
||
92 | $taxon = $this->taxonFactory->createNew(); |
||
93 | } |
||
94 | |||
95 | $taxon->setCode($options['code']); |
||
96 | |||
97 | foreach ($this->getLocales() as $localeCode) { |
||
98 | $taxon->setCurrentLocale($localeCode); |
||
99 | $taxon->setFallbackLocale($localeCode); |
||
100 | |||
101 | $taxon->setName($options['name']); |
||
102 | $taxon->setDescription($options['description']); |
||
103 | $taxon->setSlug($options['slug'] ?: $this->taxonSlugGenerator->generate($taxon, $localeCode)); |
||
104 | } |
||
105 | |||
106 | foreach ($options['children'] as $childOptions) { |
||
107 | $taxon->addChild($this->create($childOptions)); |
||
108 | } |
||
109 | |||
110 | return $taxon; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * {@inheritdoc} |
||
115 | */ |
||
116 | protected function configureOptions(OptionsResolver $resolver): void |
||
117 | { |
||
118 | $resolver |
||
119 | ->setDefault('name', function (Options $options): string { |
||
120 | return $this->faker->words(3, true); |
||
121 | }) |
||
122 | ->setDefault('code', function (Options $options): string { |
||
123 | return StringInflector::nameToCode($options['name']); |
||
124 | }) |
||
125 | ->setDefault('slug', null) |
||
126 | ->setDefault('description', function (Options $options): string { |
||
0 ignored issues
–
show
|
|||
127 | return $this->faker->paragraph; |
||
128 | }) |
||
129 | ->setDefault('children', []) |
||
130 | ->setAllowedTypes('children', ['array']) |
||
131 | ; |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * @return iterable |
||
136 | */ |
||
137 | private function getLocales(): iterable |
||
138 | { |
||
139 | /** @var LocaleInterface[] $locales */ |
||
140 | $locales = $this->localeRepository->findAll(); |
||
141 | foreach ($locales as $locale) { |
||
142 | yield $locale->getCode(); |
||
143 | } |
||
144 | } |
||
145 | } |
||
146 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.