Completed
Push — master ( d81c19...f57266 )
by Kamil
20s
created

Fixture/Factory/ProductAttributeExampleFactory.php (1 issue)

Upgrade to new PHP Analysis Engine

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\Attribute\Factory\AttributeFactoryInterface;
17
use Sylius\Component\Core\Formatter\StringInflector;
18
use Sylius\Component\Locale\Model\LocaleInterface;
19
use Sylius\Component\Product\Model\ProductAttributeInterface;
20
use Sylius\Component\Resource\Repository\RepositoryInterface;
21
use Symfony\Component\OptionsResolver\Options;
22
use Symfony\Component\OptionsResolver\OptionsResolver;
23
24
class ProductAttributeExampleFactory extends AbstractExampleFactory implements ExampleFactoryInterface
25
{
26
    /**
27
     * @var AttributeFactoryInterface
28
     */
29
    private $productAttributeFactory;
30
31
    /**
32
     * @var RepositoryInterface
33
     */
34
    private $localeRepository;
35
36
    /**
37
     * @var \Faker\Generator
38
     */
39
    private $faker;
40
41
    /**
42
     * @var OptionsResolver
43
     */
44
    private $optionsResolver;
45
46
    /**
47
     * @var array
48
     */
49
    private $attributeTypes;
50
51
    /**
52
     * @param AttributeFactoryInterface $productAttributeFactory
53
     * @param RepositoryInterface $localeRepository
54
     * @param array $attributeTypes
55
     */
56
    public function __construct(
57
        AttributeFactoryInterface $productAttributeFactory,
58
        RepositoryInterface $localeRepository,
59
        array $attributeTypes
60
    ) {
61
        $this->productAttributeFactory = $productAttributeFactory;
62
        $this->localeRepository = $localeRepository;
63
        $this->attributeTypes = $attributeTypes;
64
65
        $this->faker = \Faker\Factory::create();
66
        $this->optionsResolver = new OptionsResolver();
67
68
        $this->configureOptions($this->optionsResolver);
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function create(array $options = []): ProductAttributeInterface
75
    {
76
        $options = $this->optionsResolver->resolve($options);
77
78
        /** @var ProductAttributeInterface $productAttribute */
79
        $productAttribute = $this->productAttributeFactory->createTyped($options['type']);
80
        $productAttribute->setCode($options['code']);
81
82
        foreach ($this->getLocales() as $localeCode) {
83
            $productAttribute->setCurrentLocale($localeCode);
84
            $productAttribute->setFallbackLocale($localeCode);
85
86
            $productAttribute->setName($options['name']);
87
        }
88
89
        $productAttribute->setConfiguration($options['configuration']);
90
91
        return $productAttribute;
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    protected function configureOptions(OptionsResolver $resolver): void
98
    {
99
        $resolver
100
            ->setDefault('name', function (Options $options): string {
0 ignored issues
show
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
101
                return $this->faker->words(3, true);
102
            })
103
            ->setDefault('code', function (Options $options): string {
104
                return StringInflector::nameToCode($options['name']);
105
            })
106
            ->setDefault('type', function (Options $options): string {
107
                return $this->faker->randomElement(array_keys($this->attributeTypes));
108
            })
109
            ->setDefault('configuration', function (Options $options): array {
110
                return [];
111
            })
112
            ->setAllowedValues('type', array_keys($this->attributeTypes))
113
        ;
114
    }
115
116
    /**
117
     * @return iterable
118
     */
119
    private function getLocales(): iterable
120
    {
121
        /** @var LocaleInterface[] $locales */
122
        $locales = $this->localeRepository->findAll();
123
        foreach ($locales as $locale) {
124
            yield $locale->getCode();
125
        }
126
    }
127
}
128