Completed
Branch master (b23e60)
by Kamil
35:19
created

BookProductFixture::load()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 54
Code Lines 34

Duplication

Lines 6
Ratio 11.11 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 6
loc 54
rs 9.0306
cc 4
eloc 34
nc 8
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
namespace Sylius\Bundle\CoreBundle\Fixture;
13
14
use Sylius\Bundle\FixturesBundle\Fixture\AbstractFixture;
15
use Sylius\Component\Attribute\AttributeType\IntegerAttributeType;
16
use Sylius\Component\Attribute\AttributeType\TextAttributeType;
17
use Sylius\Component\Resource\Repository\RepositoryInterface;
18
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
19
use Symfony\Component\OptionsResolver\OptionsResolver;
20
21
/**
22
 * @author Kamil Kokot <[email protected]>
23
 */
24
final class BookProductFixture extends AbstractFixture
25
{
26
    /**
27
     * @var TaxonFixture
28
     */
29
    private $taxonFixture;
30
31
    /**
32
     * @var RepositoryInterface
33
     */
34
    private $taxonRepository;
35
36
    /**
37
     * @var ProductAttributeFixture
38
     */
39
    private $productAttributeFixture;
40
41
    /**
42
     * @var ProductOptionFixture
43
     */
44
    private $productOptionFixture;
45
46
    /**
47
     * @var ProductArchetypeFixture
48
     */
49
    private $productArchetypeFixture;
50
51
    /**
52
     * @var ProductFixture
53
     */
54
    private $productFixture;
55
56
    /**
57
     * @var \Faker\Generator
58
     */
59
    private $faker;
60
61
    /**
62
     * @var OptionsResolver
63
     */
64
    private $optionsResolver;
65
66
    /**
67
     * @param TaxonFixture $taxonFixture
68
     * @param RepositoryInterface $taxonRepository
69
     * @param ProductAttributeFixture $productAttributeFixture
70
     * @param ProductOptionFixture $productOptionFixture
71
     * @param ProductArchetypeFixture $productArchetypeFixture
72
     * @param ProductFixture $productFixture
73
     */
74 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
        TaxonFixture $taxonFixture,
76
        RepositoryInterface $taxonRepository,
77
        ProductAttributeFixture $productAttributeFixture,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $productAttributeFixture 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...
78
        ProductOptionFixture $productOptionFixture,
79
        ProductArchetypeFixture $productArchetypeFixture,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $productArchetypeFixture 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...
80
        ProductFixture $productFixture
81
    ) {
82
        $this->taxonFixture = $taxonFixture;
83
        $this->taxonRepository = $taxonRepository;
84
        $this->productAttributeFixture = $productAttributeFixture;
85
        $this->productOptionFixture = $productOptionFixture;
86
        $this->productArchetypeFixture = $productArchetypeFixture;
87
        $this->productFixture = $productFixture;
88
89
        $this->faker = \Faker\Factory::create();
90
        $this->optionsResolver =
91
            (new OptionsResolver())
92
                ->setRequired('amount')
93
                ->setAllowedTypes('amount', 'int')
94
        ;
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function getName()
101
    {
102
        return 'book_product';
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function load(array $options)
109
    {
110
        $options = $this->optionsResolver->resolve($options);
111
112
        $taxons = [];
113 View Code Duplication
        if (null === $this->taxonRepository->findOneBy(['code' => 'CATEGORY'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114
            $taxons[] = ['name' => 'Category', 'code' => 'CATEGORY', 'parent' => null];
115
        }
116
117 View Code Duplication
        if (null === $this->taxonRepository->findOneBy(['code' => 'BRAND'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
118
            $taxons[] = ['name' => 'Brand', 'code' => 'BRAND', 'parent' => null];
119
        }
120
121
        $this->taxonFixture->load(['custom' => array_merge($taxons, [
122
            ['name' => 'Books', 'code' => 'BOOKS', 'parent' => 'CATEGORY'],
123
            ['name' => 'BookMania', 'code' => 'BOOKMANIA', 'parent' => 'BRAND'],
124
        ])]);
125
126
        $this->productAttributeFixture->load(['custom' => [
127
            ['name' => 'Book author', 'code' => 'BOOK-AUTHOR', 'type' => TextAttributeType::TYPE],
128
            ['name' => 'Book ISBN', 'code' => 'BOOK-ISBN', 'type' => TextAttributeType::TYPE],
129
            ['name' => 'Book pages', 'code' => 'BOOK-PAGES', 'type' => IntegerAttributeType::TYPE],
130
        ]]);
131
132
        $this->productArchetypeFixture->load(['custom' => [
133
            [
134
                'name' => 'Book',
135
                'code' => 'BOOK',
136
                'product_attributes' => ['BOOK-AUTHOR', 'BOOK-ISBN', 'BOOK-PAGES'],
137
                'product_options' => [],
138
            ],
139
        ]]);
140
141
        $products = [];
142
        for ($i = 0; $i < $options['amount']; ++$i) {
143
            $name = $this->faker->name;
144
145
            $products[] = [
146
                'name' => sprintf('Book "%s" by %s', $this->faker->word, $name),
147
                'code' => $this->faker->uuid,
148
                'main_taxon' => 'BOOKS',
149
                'product_archetype' => 'BOOK',
150
                'taxons' => ['BOOKS', 'BOOKMANIA'],
151
                'product_attributes' => [
152
                    'BOOK-AUTHOR' => $name,
153
                    'BOOK-ISBN' => $this->faker->isbn13,
154
                    'BOOK-PAGES' => $this->faker->numberBetween(42, 1024),
155
                ],
156
                'images' => [sprintf('%s/../Resources/fixtures/%s', __DIR__, 'books.jpg')],
157
            ];
158
        }
159
160
        $this->productFixture->load(['custom' => $products]);
161
    }
162
163
    /**
164
     * {@inheritdoc}
165
     */
166
    protected function configureOptionsNode(ArrayNodeDefinition $optionsNode)
167
    {
168
        $optionsNode
169
            ->children()
170
                ->integerNode('amount')->isRequired()->min(0)->end()
171
        ;
172
    }
173
}
174