Completed
Push — master ( 8cc523...26132b )
by Kamil
18:11
created

ShippingMethodExampleFactory::configureOptions()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 39
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 39
rs 8.8571
cc 2
eloc 27
nc 1
nop 1
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\Bundle\CoreBundle\Fixture\OptionsResolver\LazyOption;
15
use Sylius\Component\Addressing\Model\ZoneInterface;
16
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
17
use Sylius\Component\Core\Formatter\StringInflector;
18
use Sylius\Component\Core\Model\ChannelInterface;
19
use Sylius\Component\Core\Model\ShippingMethodInterface;
20
use Sylius\Component\Locale\Model\LocaleInterface;
21
use Sylius\Component\Resource\Factory\FactoryInterface;
22
use Sylius\Component\Resource\Repository\RepositoryInterface;
23
use Sylius\Component\Shipping\Calculator\DefaultCalculators;
24
use Sylius\Component\Shipping\Model\ShippingCategoryInterface;
25
use Symfony\Component\OptionsResolver\Options;
26
use Symfony\Component\OptionsResolver\OptionsResolver;
27
28
/**
29
 * @author Kamil Kokot <[email protected]>
30
 */
31
class ShippingMethodExampleFactory extends AbstractExampleFactory implements ExampleFactoryInterface
32
{
33
    /**
34
     * @var FactoryInterface
35
     */
36
    private $shippingMethodFactory;
37
38
    /**
39
     * @var RepositoryInterface
40
     */
41
    private $zoneRepository;
42
43
    /**
44
     * @var RepositoryInterface
45
     */
46
    private $shippingCategoryRepository;
47
48
    /**
49
     * @var RepositoryInterface
50
     */
51
    private $localeRepository;
52
53
    /**
54
     * @var ChannelRepositoryInterface
55
     */
56
    private $channelRepository;
57
58
    /**
59
     * @var \Faker\Generator
60
     */
61
    private $faker;
62
63
    /**
64
     * @var OptionsResolver
65
     */
66
    private $optionsResolver;
67
68
    /**
69
     * @param FactoryInterface $shippingMethodFactory
70
     * @param RepositoryInterface $zoneRepository
71
     * @param RepositoryInterface $shippingCategoryRepository
72
     * @param RepositoryInterface $localeRepository
73
     * @param ChannelRepositoryInterface $channelRepository
74
     */
75
    public function __construct(
76
        FactoryInterface $shippingMethodFactory,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $shippingMethodFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
77
        RepositoryInterface $zoneRepository,
78
        RepositoryInterface $shippingCategoryRepository,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $shippingCategoryRepository exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
79
        RepositoryInterface $localeRepository,
80
        ChannelRepositoryInterface $channelRepository
81
    ) {
82
        $this->shippingMethodFactory = $shippingMethodFactory;
83
        $this->zoneRepository = $zoneRepository;
84
        $this->shippingCategoryRepository = $shippingCategoryRepository;
85
        $this->localeRepository = $localeRepository;
86
        $this->channelRepository = $channelRepository;
87
88
        $this->faker = \Faker\Factory::create();
89
        $this->optionsResolver = new OptionsResolver();
90
91
        $this->configureOptions($this->optionsResolver);
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function create(array $options = [])
98
    {
99
        $options = $this->optionsResolver->resolve($options);
100
101
        /** @var ShippingMethodInterface $shippingMethod */
102
        $shippingMethod = $this->shippingMethodFactory->createNew();
103
        $shippingMethod->setCode($options['code']);
104
        $shippingMethod->setEnabled($options['enabled']);
105
        $shippingMethod->setZone($options['zone']);
106
        $shippingMethod->setCalculator($options['calculator']['type']);
107
        $shippingMethod->setConfiguration($options['calculator']['configuration']);
108
109
        if (array_key_exists('shipping_category', $options)) {
110
            $shippingMethod->setCategory($options['shipping_category']);
111
        }
112
113
        foreach ($this->getLocales() as $localeCode) {
114
            $shippingMethod->setCurrentLocale($localeCode);
115
            $shippingMethod->setFallbackLocale($localeCode);
116
117
            $shippingMethod->setName($options['name']);
118
            $shippingMethod->setDescription($options['description']);
119
        }
120
121
        foreach ($options['channels'] as $channel) {
122
            $shippingMethod->addChannel($channel);
123
        }
124
125
        return $shippingMethod;
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131
    protected function configureOptions(OptionsResolver $resolver)
132
    {
133
        $resolver
134
            ->setDefault('code', function (Options $options) {
135
                return StringInflector::nameToCode($options['name']);
136
            })
137
            ->setDefault('name', function (Options $options) {
0 ignored issues
show
Unused Code introduced by
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...
138
                return $this->faker->words(3, true);
139
            })
140
            ->setDefault('description', function (Options $options) {
0 ignored issues
show
Unused Code introduced by
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...
141
                return $this->faker->sentence();
142
            })
143
            ->setDefault('enabled', function (Options $options) {
0 ignored issues
show
Unused Code introduced by
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...
144
                return $this->faker->boolean(90);
145
            })
146
            ->setAllowedTypes('enabled', 'bool')
147
            ->setDefault('zone', LazyOption::randomOne($this->zoneRepository))
148
            ->setAllowedTypes('zone', ['null', 'string', ZoneInterface::class])
149
            ->setNormalizer('zone', LazyOption::findOneBy($this->zoneRepository, 'code'))
150
            ->setDefined('shipping_category')
151
            ->setAllowedTypes('shipping_category', ['null', 'string', ShippingCategoryInterface::class])
152
            ->setNormalizer('shipping_category', LazyOption::findOneBy($this->shippingCategoryRepository, 'code'))
153
            ->setDefault('calculator', function (Options $options) {
154
                $configuration = [];
155
                /** @var ChannelInterface $channel */
156
                foreach ($options['channels'] as $channel) {
157
                    $configuration[$channel->getCode()] = ['amount' => $this->faker->randomNumber(4)];
158
                }
159
160
                return [
161
                    'type' => DefaultCalculators::FLAT_RATE,
162
                    'configuration' => $configuration,
163
                ];
164
            })
165
            ->setDefault('channels', LazyOption::all($this->channelRepository))
166
            ->setAllowedTypes('channels', 'array')
167
            ->setNormalizer('channels', LazyOption::findBy($this->channelRepository, 'code'))
168
        ;
169
    }
170
171
    /**
172
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be \Generator?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
173
     */
174
    private function getLocales()
175
    {
176
        /** @var LocaleInterface[] $locales */
177
        $locales = $this->localeRepository->findAll();
178
        foreach ($locales as $locale) {
179
            yield $locale->getCode();
180
        }
181
    }
182
}
183