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