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

ChannelExampleFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 2
cbo 8
dl 0
loc 120
rs 10

3 Methods

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