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\TextAttributeType; |
16
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
17
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
18
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Kamil Kokot <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
final class TshirtProductFixture extends AbstractFixture |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var TaxonFixture |
27
|
|
|
*/ |
28
|
|
|
private $taxonFixture; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var RepositoryInterface |
32
|
|
|
*/ |
33
|
|
|
private $taxonRepository; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var ProductAttributeFixture |
37
|
|
|
*/ |
38
|
|
|
private $productAttributeFixture; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var ProductOptionFixture |
42
|
|
|
*/ |
43
|
|
|
private $productOptionFixture; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var ProductArchetypeFixture |
47
|
|
|
*/ |
48
|
|
|
private $productArchetypeFixture; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var ProductFixture |
52
|
|
|
*/ |
53
|
|
|
private $productFixture; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var \Faker\Generator |
57
|
|
|
*/ |
58
|
|
|
private $faker; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var OptionsResolver |
62
|
|
|
*/ |
63
|
|
|
private $optionsResolver; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param TaxonFixture $taxonFixture |
67
|
|
|
* @param RepositoryInterface $taxonRepository |
68
|
|
|
* @param ProductAttributeFixture $productAttributeFixture |
69
|
|
|
* @param ProductOptionFixture $productOptionFixture |
70
|
|
|
* @param ProductArchetypeFixture $productArchetypeFixture |
71
|
|
|
* @param ProductFixture $productFixture |
72
|
|
|
*/ |
73
|
|
|
public function __construct( |
74
|
|
|
TaxonFixture $taxonFixture, |
75
|
|
|
RepositoryInterface $taxonRepository, |
76
|
|
|
ProductAttributeFixture $productAttributeFixture, |
77
|
|
|
ProductOptionFixture $productOptionFixture, |
78
|
|
|
ProductArchetypeFixture $productArchetypeFixture, |
79
|
|
|
ProductFixture $productFixture |
80
|
|
|
) { |
81
|
|
|
$this->taxonFixture = $taxonFixture; |
82
|
|
|
$this->taxonRepository = $taxonRepository; |
83
|
|
|
$this->productAttributeFixture = $productAttributeFixture; |
84
|
|
|
$this->productOptionFixture = $productOptionFixture; |
85
|
|
|
$this->productArchetypeFixture = $productArchetypeFixture; |
86
|
|
|
$this->productFixture = $productFixture; |
87
|
|
|
|
88
|
|
|
$this->faker = \Faker\Factory::create(); |
89
|
|
|
$this->optionsResolver = |
90
|
|
|
(new OptionsResolver()) |
91
|
|
|
->setRequired('amount') |
92
|
|
|
->setAllowedTypes('amount', 'int') |
93
|
|
|
; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
|
|
public function getName() |
100
|
|
|
{ |
101
|
|
|
return 'tshirt_product'; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
|
|
public function load(array $options) |
108
|
|
|
{ |
109
|
|
|
$options = $this->optionsResolver->resolve($options); |
110
|
|
|
|
111
|
|
|
$taxons = []; |
112
|
|
|
if (null === $this->taxonRepository->findOneBy(['code' => 'CATEGORY'])) { |
113
|
|
|
$taxons[] = ['name' => 'Category', 'code' => 'CATEGORY', 'parent' => null]; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if (null === $this->taxonRepository->findOneBy(['code' => 'BRAND'])) { |
117
|
|
|
$taxons[] = ['name' => 'Brand', 'code' => 'BRAND', 'parent' => null]; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$this->taxonFixture->load(['custom' => array_merge($taxons, [ |
121
|
|
|
['name' => 'T-Shirts', 'code' => 'TSHIRTS', 'parent' => 'CATEGORY'], |
122
|
|
|
['name' => 'Super Tees', 'code' => 'SUPER-TEES', 'parent' => 'BRAND'], |
123
|
|
|
])]); |
124
|
|
|
|
125
|
|
|
$this->productAttributeFixture->load(['custom' => [ |
126
|
|
|
['name' => 'T-Shirt brand', 'code' => 'TSHIRT-BRAND', 'type' => TextAttributeType::TYPE], |
127
|
|
|
['name' => 'T-Shirt collection', 'code' => 'TSHIRT-COLLECTION', 'type' => TextAttributeType::TYPE], |
128
|
|
|
['name' => 'T-Shirt material', 'code' => 'TSHIRT-MATERIAL', 'type' => TextAttributeType::TYPE], |
129
|
|
|
]]); |
130
|
|
|
|
131
|
|
|
$this->productOptionFixture->load(['custom' => [ |
132
|
|
|
[ |
133
|
|
|
'name' => 'T-Shirt color', |
134
|
|
|
'code' => 'TSHIRT-COLOR', |
135
|
|
|
'values' => [ |
136
|
|
|
'TSHIRT-COLOR-RED' => 'Red', |
137
|
|
|
'TSHIRT-COLOR-BLACK' => 'Black', |
138
|
|
|
'TSHIRT-COLOR-WHITE' => 'White', |
139
|
|
|
], |
140
|
|
|
], |
141
|
|
|
[ |
142
|
|
|
'name' => 'T-Shirt size', |
143
|
|
|
'code' => 'TSHIRT-SIZE', |
144
|
|
|
'values' => [ |
145
|
|
|
'TSHIRT-SIZE-S' => 'S', |
146
|
|
|
'TSHIRT-SIZE-M' => 'M', |
147
|
|
|
'TSHIRT-SIZE-L' => 'L', |
148
|
|
|
'TSHIRT-SIZE-XL' => 'XL', |
149
|
|
|
'TSHIRT-SIZE-XXL' => 'XXL', |
150
|
|
|
], |
151
|
|
|
], |
152
|
|
|
]]); |
153
|
|
|
|
154
|
|
|
$this->productArchetypeFixture->load(['custom' => [ |
155
|
|
|
[ |
156
|
|
|
'name' => 'T-Shirt', |
157
|
|
|
'code' => 'TSHIRT', |
158
|
|
|
'product_attributes' => ['TSHIRT-BRAND', 'TSHIRT-COLLECTION', 'TSHIRT-MATERIAL'], |
159
|
|
|
'product_options' => ['TSHIRT-COLOR', 'TSHIRT-SIZE'], |
160
|
|
|
], |
161
|
|
|
]]); |
162
|
|
|
|
163
|
|
|
$products = []; |
164
|
|
|
for ($i = 0; $i < $options['amount']; ++$i) { |
165
|
|
|
$products[] = [ |
166
|
|
|
'name' => sprintf('T-Shirt "%s"', $this->faker->word), |
167
|
|
|
'code' => $this->faker->uuid, |
168
|
|
|
'main_taxon' => 'TSHIRTS', |
169
|
|
|
'product_archetype' => 'TSHIRT', |
170
|
|
|
'taxons' => ['TSHIRTS', 'SUPER-TEES'], |
171
|
|
|
'product_attributes' => [ |
172
|
|
|
'TSHIRT-BRAND' => $this->faker->randomElement(['Nike', 'Adidas', 'JKM-476 Streetwear', 'Potato', 'Centipede Wear']), |
|
|
|
|
173
|
|
|
'TSHIRT-COLLECTION' => sprintf('Sylius %s %s', $this->faker->randomElement(['Summer', 'Winter', 'Spring', 'Autumn']), mt_rand(1995, 2012)), |
|
|
|
|
174
|
|
|
'TSHIRT-MATERIAL' => $this->faker->randomElement(['Centipede', 'Wool', 'Centipede 10% / Wool 90%', 'Potato 100%']), |
|
|
|
|
175
|
|
|
], |
176
|
|
|
]; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
$this->productFixture->load(['custom' => $products]); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* {@inheritdoc} |
184
|
|
|
*/ |
185
|
|
|
protected function configureOptionsNode(ArrayNodeDefinition $optionsNode) |
186
|
|
|
{ |
187
|
|
|
$optionsNode |
|
|
|
|
188
|
|
|
->children() |
189
|
|
|
->integerNode('amount')->isRequired()->min(0)->end() |
190
|
|
|
; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|