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

TaxRateExampleFactory::configureOptions()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 26
rs 8.8571
cc 1
eloc 19
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\Core\Formatter\StringInflector;
17
use Sylius\Component\Core\Model\TaxRateInterface;
18
use Sylius\Component\Resource\Factory\FactoryInterface;
19
use Sylius\Component\Resource\Repository\RepositoryInterface;
20
use Sylius\Component\Taxation\Model\TaxCategoryInterface;
21
use Symfony\Component\OptionsResolver\Options;
22
use Symfony\Component\OptionsResolver\OptionsResolver;
23
24
/**
25
 * @author Kamil Kokot <[email protected]>
26
 */
27
class TaxRateExampleFactory extends AbstractExampleFactory implements ExampleFactoryInterface
28
{
29
    /**
30
     * @var FactoryInterface
31
     */
32
    private $taxRateFactory;
33
34
    /**
35
     * @var RepositoryInterface
36
     */
37
    private $zoneRepository;
38
39
    /**
40
     * @var RepositoryInterface
41
     */
42
    private $taxCategoryRepository;
43
44
    /**
45
     * @var \Faker\Generator
46
     */
47
    private $faker;
48
49
    /**
50
     * @var OptionsResolver
51
     */
52
    private $optionsResolver;
53
54
    /**
55
     * @param FactoryInterface $taxRateFactory
56
     * @param RepositoryInterface $zoneRepository
57
     * @param RepositoryInterface $taxCategoryRepository
58
     */
59
    public function __construct(
60
        FactoryInterface $taxRateFactory,
61
        RepositoryInterface $zoneRepository,
62
        RepositoryInterface $taxCategoryRepository
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $taxCategoryRepository 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...
63
    ) {
64
        $this->taxRateFactory = $taxRateFactory;
65
        $this->zoneRepository = $zoneRepository;
66
        $this->taxCategoryRepository = $taxCategoryRepository;
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 TaxRateInterface $taxRate */
82
        $taxRate = $this->taxRateFactory->createNew();
83
84
        $taxRate->setCode($options['code']);
85
        $taxRate->setName($options['name']);
86
        $taxRate->setAmount($options['amount']);
87
        $taxRate->setIncludedInPrice($options['included_in_price']);
88
        $taxRate->setCalculator($options['calculator']);
89
        $taxRate->setZone($options['zone']);
90
        $taxRate->setCategory($options['category']);
91
92
        return $taxRate;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    protected function configureOptions(OptionsResolver $resolver)
99
    {
100
        $resolver
101
            ->setDefault('code', function (Options $options) {
102
                return StringInflector::nameToCode($options['name']);
103
            })
104
            ->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...
105
                return $this->faker->words(3, true);
106
            })
107
            ->setDefault('amount', 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...
108
                return $this->faker->randomFloat(2, 0, 0.4);
109
            })
110
            ->setAllowedTypes('amount', 'float')
111
            ->setDefault('included_in_price', 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...
112
                return $this->faker->boolean();
113
            })
114
            ->setAllowedTypes('included_in_price', 'bool')
115
            ->setDefault('calculator', 'default')
116
            ->setDefault('zone', LazyOption::randomOne($this->zoneRepository))
117
            ->setAllowedTypes('zone', ['null', 'string', ZoneInterface::class])
118
            ->setNormalizer('zone', LazyOption::findOneBy($this->zoneRepository, 'code'))
119
            ->setDefault('category', LazyOption::randomOne($this->taxCategoryRepository))
120
            ->setAllowedTypes('category', ['null', 'string', TaxCategoryInterface::class])
121
            ->setNormalizer('category', LazyOption::findOneBy($this->taxCategoryRepository, 'code'))
122
        ;
123
    }
124
}
125