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 | namespace Sylius\Bundle\CoreBundle\Fixture\Factory; |
||
13 | |||
14 | use Sylius\Component\Attribute\Factory\AttributeFactoryInterface; |
||
15 | use Sylius\Component\Core\Formatter\StringInflector; |
||
16 | use Sylius\Component\Locale\Model\LocaleInterface; |
||
17 | use Sylius\Component\Product\Model\OptionInterface; |
||
18 | use Sylius\Component\Resource\Repository\RepositoryInterface; |
||
19 | use Symfony\Component\OptionsResolver\Options; |
||
20 | use Symfony\Component\OptionsResolver\OptionsResolver; |
||
21 | |||
22 | /** |
||
23 | * @author Kamil Kokot <[email protected]> |
||
24 | */ |
||
25 | final class ProductAttributeExampleFactory implements ExampleFactoryInterface |
||
26 | { |
||
27 | /** |
||
28 | * @var AttributeFactoryInterface |
||
29 | */ |
||
30 | private $productAttributeFactory; |
||
31 | |||
32 | /** |
||
33 | * @var RepositoryInterface |
||
34 | */ |
||
35 | private $localeRepository; |
||
36 | |||
37 | /** |
||
38 | * @var \Faker\Generator |
||
39 | */ |
||
40 | private $faker; |
||
41 | |||
42 | /** |
||
43 | * @var OptionsResolver |
||
44 | */ |
||
45 | private $optionsResolver; |
||
46 | |||
47 | /** |
||
48 | * @param AttributeFactoryInterface $productAttributeFactory |
||
49 | * @param RepositoryInterface $localeRepository |
||
50 | * @param array $attributeTypes |
||
51 | */ |
||
52 | public function __construct( |
||
53 | AttributeFactoryInterface $productAttributeFactory, |
||
54 | RepositoryInterface $localeRepository, |
||
55 | array $attributeTypes |
||
56 | ) { |
||
57 | $this->productAttributeFactory = $productAttributeFactory; |
||
58 | $this->localeRepository = $localeRepository; |
||
59 | |||
60 | $this->faker = \Faker\Factory::create(); |
||
61 | $this->optionsResolver = |
||
62 | (new OptionsResolver()) |
||
63 | ->setDefault('name', function (Options $options) { |
||
64 | return $this->faker->words(3, true); |
||
65 | }) |
||
66 | ->setDefault('code', function (Options $options) { |
||
67 | return StringInflector::nameToCode($options['name']); |
||
68 | }) |
||
69 | ->setDefault('type', function (Options $options) use ($attributeTypes) { |
||
70 | return $this->faker->randomElement(array_keys($attributeTypes)); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
71 | }) |
||
72 | ->setAllowedValues('type', array_keys($attributeTypes)) |
||
73 | ; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * {@inheritdoc} |
||
78 | */ |
||
79 | public function create(array $options = []) |
||
80 | { |
||
81 | $options = $this->optionsResolver->resolve($options); |
||
82 | |||
83 | /** @var OptionInterface $productAttribute */ |
||
84 | $productAttribute = $this->productAttributeFactory->createTyped($options['type']); |
||
85 | $productAttribute->setCode($options['code']); |
||
86 | |||
87 | foreach ($this->getLocales() as $localeCode) { |
||
88 | $productAttribute->setCurrentLocale($localeCode); |
||
89 | $productAttribute->setFallbackLocale($localeCode); |
||
90 | |||
91 | $productAttribute->setName($options['name']); |
||
92 | } |
||
93 | |||
94 | return $productAttribute; |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * @return array |
||
99 | */ |
||
100 | private function getLocales() |
||
101 | { |
||
102 | /** @var LocaleInterface[] $locales */ |
||
103 | $locales = $this->localeRepository->findAll(); |
||
104 | foreach ($locales as $locale) { |
||
105 | yield $locale->getCode(); |
||
106 | } |
||
107 | } |
||
108 | } |
||
109 |