Completed
Push — master ( 4ad486...2231e0 )
by Paweł
19:28 queued 09:00
created

ChannelExampleFactory::configureOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 48
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

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