Completed
Push — allow-no-default-tax-zone-in-c... ( 67cea0 )
by Kamil
06:23
created

ChannelExampleFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 6
dl 0
loc 138
rs 10
c 0
b 0
f 0

3 Methods

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