Completed
Push — master ( f34924...c8c8f9 )
by Paweł
13:31
created

BookProductFixture   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
c 1
b 0
f 1
lcom 1
cbo 12
dl 0
loc 149
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 22 1
A getName() 0 4 1
B load() 0 53 4
A configureOptionsNode() 0 7 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;
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
    public function __construct(
75
        TaxonFixture $taxonFixture,
76
        RepositoryInterface $taxonRepository,
77
        ProductAttributeFixture $productAttributeFixture,
78
        ProductOptionFixture $productOptionFixture,
79
        ProductArchetypeFixture $productArchetypeFixture,
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
        if (null === $this->taxonRepository->findOneBy(['code' => 'CATEGORY'])) {
114
            $taxons[] = ['name' => 'Category', 'code' => 'CATEGORY', 'parent' => null];
115
        }
116
117
        if (null === $this->taxonRepository->findOneBy(['code' => 'BRAND'])) {
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
            ];
157
        }
158
159
        $this->productFixture->load(['custom' => $products]);
160
    }
161
162
    /**
163
     * {@inheritdoc}
164
     */
165
    protected function configureOptionsNode(ArrayNodeDefinition $optionsNode)
166
    {
167
        $optionsNode
168
            ->children()
169
                ->integerNode('amount')->isRequired()->min(0)->end()
170
        ;
171
    }
172
}
173