Completed
Push — master ( 1212b8...011c4b )
by Paweł
29:44 queued 19:42
created

BookProductFixture::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 56
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 56
rs 9.7251
cc 2
eloc 37
nc 2
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\SelectAttributeType;
17
use Sylius\Component\Attribute\AttributeType\TextAttributeType;
18
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
19
use Symfony\Component\OptionsResolver\OptionsResolver;
20
21
/**
22
 * @author Kamil Kokot <[email protected]>
23
 */
24
class BookProductFixture extends AbstractFixture
25
{
26
    /**
27
     * @var AbstractResourceFixture
28
     */
29
    private $taxonFixture;
30
31
    /**
32
     * @var AbstractResourceFixture
33
     */
34
    private $productAttributeFixture;
35
36
    /**
37
     * @var AbstractResourceFixture
38
     */
39
    private $productFixture;
40
41
    /**
42
     * @var \Faker\Generator
43
     */
44
    private $faker;
45
46
    /**
47
     * @var OptionsResolver
48
     */
49
    private $optionsResolver;
50
51
    /**
52
     * @param AbstractResourceFixture $taxonFixture
53
     * @param AbstractResourceFixture $productAttributeFixture
54
     * @param AbstractResourceFixture $productFixture
55
     */
56
    public function __construct(
57
        AbstractResourceFixture $taxonFixture,
58
        AbstractResourceFixture $productAttributeFixture,
59
        AbstractResourceFixture $productFixture
60
    ) {
61
        $this->taxonFixture = $taxonFixture;
62
        $this->productAttributeFixture = $productAttributeFixture;
63
        $this->productFixture = $productFixture;
64
65
        $this->faker = \Faker\Factory::create();
66
        $this->optionsResolver =
67
            (new OptionsResolver())
68
                ->setRequired('amount')
69
                ->setAllowedTypes('amount', 'int')
70
        ;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function getName()
77
    {
78
        return 'book_product';
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function load(array $options)
85
    {
86
        $options = $this->optionsResolver->resolve($options);
87
88
        $this->taxonFixture->load(['custom' => [[
89
            'code' => 'category',
90
            'name' => 'Category',
91
            'children' => [
92
                [
93
                    'code' => 'books',
94
                    'name' => 'Books',
95
                ]
96
            ]
97
        ]]]);
98
99
        $bookGenres = ['Fiction', 'Romance', 'Thriller', 'Sports'];
100
        $this->productAttributeFixture->load(['custom' => [
101
            ['name' => 'Book author', 'code' => 'book_author', 'type' => TextAttributeType::TYPE],
102
            ['name' => 'Book ISBN', 'code' => 'book_isbn', 'type' => TextAttributeType::TYPE],
103
            ['name' => 'Book pages', 'code' => 'book_pages', 'type' => IntegerAttributeType::TYPE],
104
            [
105
                'name' => 'Book genre',
106
                'code' => 'book_genre',
107
                'type' => SelectAttributeType::TYPE,
108
                'configuration' => [
109
                    'multiple' => true,
110
                    'choices' => $bookGenres,
111
                ]
112
            ],
113
        ]]);
114
115
        $products = [];
116
        $productsNames = $this->getUniqueNames($options['amount']);
117
        for ($i = 0; $i < $options['amount']; ++$i) {
118
            $authorName = $this->faker->name;
119
120
            $products[] = [
121
                'name' => sprintf('Book "%s" by %s', $productsNames[$i], $authorName),
122
                'code' => $this->faker->uuid,
123
                'main_taxon' => 'books',
124
                'taxons' => ['books'],
125
                'product_attributes' => [
126
                    'book_author' => $authorName,
127
                    'book_isbn' => $this->faker->isbn13,
128
                    'book_pages' => $this->faker->numberBetween(42, 1024),
129
                    'book_genre' => array_keys($this->faker->randomElements($bookGenres, $this->faker->randomKey($bookGenres) + 1)),
130
                ],
131
                'images' => [
132
                    [sprintf('%s/../Resources/fixtures/%s', __DIR__, 'books.jpg'), 'main'],
133
                    [sprintf('%s/../Resources/fixtures/%s', __DIR__, 'books.jpg'), 'thumbnail'],
134
                ],
135
            ];
136
        }
137
138
        $this->productFixture->load(['custom' => $products]);
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    protected function configureOptionsNode(ArrayNodeDefinition $optionsNode)
145
    {
146
        $optionsNode
147
            ->children()
148
                ->integerNode('amount')->isRequired()->min(0)->end()
149
        ;
150
    }
151
152
    /**
153
     * @param int $amount
154
     *
155
     * @return string
156
     */
157
    private function getUniqueNames($amount)
158
    {
159
        $productsNames = [];
160
161
        for ($i = 0; $i < $amount; ++$i) {
162
            $name = $this->faker->word;
163
            while (in_array($name, $productsNames)) {
164
                $name = $this->faker->word;
165
            }
166
            $productsNames[] = $name;
167
        }
168
169
        return $productsNames;
170
    }
171
}
172