Completed
Push — master ( a818cb...b1c6ca )
by Kamil
07:13 queued 10s
created

ChannelExampleFactory::configureOptions()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 8.9381
c 0
b 0
f 0
cc 1
nc 1
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Bundle\CoreBundle\Fixture\OptionsResolver\LazyOption;
17
use Sylius\Component\Addressing\Model\Scope as AddressingScope;
18
use Sylius\Component\Addressing\Model\ZoneInterface;
19
use Sylius\Component\Channel\Factory\ChannelFactoryInterface;
20
use Sylius\Component\Core\Formatter\StringInflector;
21
use Sylius\Component\Core\Model\ChannelInterface;
22
use Sylius\Component\Core\Model\Scope;
23
use Sylius\Component\Core\Model\ShopBillingData;
24
use Sylius\Component\Currency\Model\CurrencyInterface;
25
use Sylius\Component\Locale\Model\LocaleInterface;
26
use Sylius\Component\Resource\Repository\RepositoryInterface;
27
use Symfony\Component\OptionsResolver\Options;
28
use Symfony\Component\OptionsResolver\OptionsResolver;
29
30
class ChannelExampleFactory extends AbstractExampleFactory implements ExampleFactoryInterface
31
{
32
    /** @var ChannelFactoryInterface */
33
    private $channelFactory;
34
35
    /** @var RepositoryInterface */
36
    private $localeRepository;
37
38
    /** @var RepositoryInterface */
39
    private $currencyRepository;
40
41
    /** @var RepositoryInterface */
42
    private $zoneRepository;
43
44
    /** @var \Faker\Generator */
45
    private $faker;
46
47
    /** @var OptionsResolver */
48
    private $optionsResolver;
49
50
    public function __construct(
51
        ChannelFactoryInterface $channelFactory,
52
        RepositoryInterface $localeRepository,
53
        RepositoryInterface $currencyRepository,
54
        RepositoryInterface $zoneRepository
55
    ) {
56
        $this->channelFactory = $channelFactory;
57
        $this->localeRepository = $localeRepository;
58
        $this->currencyRepository = $currencyRepository;
59
        $this->zoneRepository = $zoneRepository;
60
61
        $this->faker = \Faker\Factory::create();
62
        $this->optionsResolver = new OptionsResolver();
63
64
        $this->configureOptions($this->optionsResolver);
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function create(array $options = []): ChannelInterface
71
    {
72
        $options = $this->optionsResolver->resolve($options);
73
74
        /** @var ChannelInterface $channel */
75
        $channel = $this->channelFactory->createNamed($options['name']);
76
        $channel->setCode($options['code']);
77
        $channel->setHostname($options['hostname']);
78
        $channel->setEnabled($options['enabled']);
79
        $channel->setColor($options['color']);
80
        $channel->setDefaultTaxZone($options['default_tax_zone']);
81
        $channel->setTaxCalculationStrategy($options['tax_calculation_strategy']);
82
        $channel->setThemeName($options['theme_name']);
83
        $channel->setContactEmail($options['contact_email']);
84
        $channel->setSkippingShippingStepAllowed($options['skipping_shipping_step_allowed']);
85
        $channel->setSkippingPaymentStepAllowed($options['skipping_payment_step_allowed']);
86
        $channel->setAccountVerificationRequired($options['account_verification_required']);
87
88
        $channel->setDefaultLocale($options['default_locale']);
89
        foreach ($options['locales'] as $locale) {
90
            $channel->addLocale($locale);
91
        }
92
93
        $channel->setBaseCurrency($options['base_currency']);
94
        foreach ($options['currencies'] as $currency) {
95
            $channel->addCurrency($currency);
96
        }
97
98
        if (isset($options['shop_billing_data']) && null !== $options['shop_billing_data']) {
99
            $shopBillingData = new ShopBillingData();
100
            $shopBillingData->setCompany($options['shop_billing_data']['company'] ?? null);
101
            $shopBillingData->setTaxId($options['shop_billing_data']['tax_id'] ?? null);
102
            $shopBillingData->setCountryCode($options['shop_billing_data']['country_code'] ?? null);
103
            $shopBillingData->setStreet($options['shop_billing_data']['street'] ?? null);
104
            $shopBillingData->setCity($options['shop_billing_data']['city'] ?? null);
105
            $shopBillingData->setPostcode($options['shop_billing_data']['postcode'] ?? null);
106
107
            $channel->setShopBillingData($shopBillingData);
108
        }
109
110
        return $channel;
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    protected function configureOptions(OptionsResolver $resolver): void
117
    {
118
        $resolver
119
            ->setDefault('name', function (Options $options): string {
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...
120
                return $this->faker->words(3, true);
121
            })
122
            ->setDefault('code', function (Options $options): string {
123
                return StringInflector::nameToCode($options['name']);
124
            })
125
            ->setDefault('hostname', function (Options $options): string {
126
                return $options['code'] . '.localhost';
127
            })
128
            ->setDefault('color', function (Options $options): string {
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...
129
                return $this->faker->colorName;
130
            })
131
            ->setDefault('enabled', function (Options $options): bool {
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...
132
                return $this->faker->boolean(90);
133
            })
134
            ->setAllowedTypes('enabled', 'bool')
135
            ->setDefault('skipping_shipping_step_allowed', false)
136
            ->setAllowedTypes('skipping_shipping_step_allowed', 'bool')
137
            ->setDefault('skipping_payment_step_allowed', false)
138
            ->setAllowedTypes('skipping_payment_step_allowed', 'bool')
139
            ->setDefault('account_verification_required', true)
140
            ->setAllowedTypes('account_verification_required', 'bool')
141
            ->setDefault(
142
                'default_tax_zone',
143
                LazyOption::randomOneOrNull($this->zoneRepository, 100, ['scope' => [Scope::TAX, AddressingScope::ALL]])
144
            )
145
            ->setAllowedTypes('default_tax_zone', ['null', 'string', ZoneInterface::class])
146
            ->setNormalizer(
147
                'default_tax_zone',
148
                LazyOption::findOneBy($this->zoneRepository, 'code', ['scope' => [Scope::TAX, AddressingScope::ALL]])
149
            )
150
            ->setDefault('tax_calculation_strategy', 'order_items_based')
151
            ->setAllowedTypes('tax_calculation_strategy', 'string')
152
            ->setDefault('default_locale', function (Options $options): LocaleInterface {
153
                return $this->faker->randomElement($options['locales']);
154
            })
155
            ->setAllowedTypes('default_locale', ['string', LocaleInterface::class])
156
            ->setNormalizer('default_locale', LazyOption::findOneBy($this->localeRepository, 'code'))
157
            ->setDefault('locales', LazyOption::all($this->localeRepository))
158
            ->setAllowedTypes('locales', 'array')
159
            ->setNormalizer('locales', LazyOption::findBy($this->localeRepository, 'code'))
160
            ->setDefault('base_currency', function (Options $options): CurrencyInterface {
161
                return $this->faker->randomElement($options['currencies']);
162
            })
163
            ->setAllowedTypes('base_currency', ['string', CurrencyInterface::class])
164
            ->setNormalizer('base_currency', LazyOption::findOneBy($this->currencyRepository, 'code'))
165
            ->setDefault('currencies', LazyOption::all($this->currencyRepository))
166
            ->setAllowedTypes('currencies', 'array')
167
            ->setNormalizer('currencies', LazyOption::findBy($this->currencyRepository, 'code'))
168
            ->setDefault('theme_name', null)
169
            ->setDefault('contact_email', null)
170
            ->setDefault('shop_billing_data', null)
171
        ;
172
    }
173
}
174