|
1
|
|
|
<?php namespace Bedard\Shop\Updates; |
|
2
|
|
|
|
|
3
|
|
|
use Bedard\Shop\Classes\Factory; |
|
4
|
|
|
use Bedard\Shop\Models\Category; |
|
5
|
|
|
use Bedard\Shop\Models\Inventory; |
|
6
|
|
|
use Bedard\Shop\Models\Option; |
|
7
|
|
|
use Bedard\Shop\Models\Product; |
|
8
|
|
|
use October\Rain\Database\Updates\Seeder; |
|
9
|
|
|
|
|
10
|
|
|
class Seeds extends Seeder |
|
11
|
|
|
{ |
|
12
|
|
|
public function run() |
|
13
|
|
|
{ |
|
14
|
|
|
// only run the seeder in development |
|
15
|
|
|
if (app()->env !== 'dev') { |
|
16
|
|
|
return; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
echo "\n"; |
|
20
|
|
|
echo ' Seeding test data...'; |
|
21
|
|
|
echo "\n"; |
|
22
|
|
|
|
|
23
|
|
|
$this->seedCategories(); |
|
24
|
|
|
$this->seedProducts(); |
|
25
|
|
|
$this->seedOptions(); |
|
26
|
|
|
$this->seedInventories(); |
|
27
|
|
|
$this->attachProductsToCategories(); |
|
28
|
|
|
|
|
29
|
|
|
echo "\n Done.\n"; |
|
30
|
|
|
echo "\n"; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
protected function attachProductsToCategories() |
|
34
|
|
|
{ |
|
35
|
|
|
Product::whereSlug('no-inventories') |
|
36
|
|
|
->firstOrFail() |
|
37
|
|
|
->categories() |
|
38
|
|
|
->attach(Category::whereSlug('parent')->firstOrFail()); |
|
39
|
|
|
|
|
40
|
|
|
Product::whereSlug('free-thing') |
|
41
|
|
|
->firstOrFail() |
|
42
|
|
|
->categories() |
|
43
|
|
|
->attach(Category::whereSlug('child-a')->firstOrFail()); |
|
44
|
|
|
|
|
45
|
|
|
Product::whereSlug('out-of-stock') |
|
46
|
|
|
->firstOrFail() |
|
47
|
|
|
->categories() |
|
48
|
|
|
->attach(Category::whereSlug('child-b')->firstOrFail()); |
|
49
|
|
|
|
|
50
|
|
|
Product::whereSlug('shirt') |
|
51
|
|
|
->firstOrFail() |
|
52
|
|
|
->categories() |
|
53
|
|
|
->attach(Category::whereSlug('grandchild')->firstOrFail()); |
|
54
|
|
|
|
|
55
|
|
|
Product::syncAllCategories(); |
|
56
|
|
|
|
|
57
|
|
|
echo " - Attaching products to categories\n"; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
protected function seedCategories() |
|
61
|
|
|
{ |
|
62
|
|
|
$parent = Factory::create(new Category, [ |
|
63
|
|
|
'name' => 'Parent', |
|
64
|
|
|
'slug' => 'parent', |
|
65
|
|
|
]); |
|
66
|
|
|
|
|
67
|
|
|
$childA = Factory::create(new Category, [ |
|
68
|
|
|
'name' => 'Child A', |
|
69
|
|
|
'slug' => 'child-a', |
|
70
|
|
|
]); |
|
71
|
|
|
|
|
72
|
|
|
$childB = Factory::create(new Category, [ |
|
73
|
|
|
'name' => 'Child B', |
|
74
|
|
|
'slug' => 'child-b', |
|
75
|
|
|
]); |
|
76
|
|
|
|
|
77
|
|
|
$grandchild = Factory::create(new Category, [ |
|
78
|
|
|
'name' => 'Grandchild', |
|
79
|
|
|
'slug' => 'grandchild', |
|
80
|
|
|
]); |
|
81
|
|
|
|
|
82
|
|
|
$childA->makeChildOf($parent); |
|
83
|
|
|
$childB->makeChildOf($parent); |
|
84
|
|
|
$grandchild->makeChildOf($childA); |
|
85
|
|
|
|
|
86
|
|
|
echo " - Categories\n"; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
protected function seedInventories() |
|
90
|
|
|
{ |
|
91
|
|
|
// out of stock |
|
92
|
|
|
Factory::create(new Inventory, [ |
|
93
|
|
|
'product_id' => Product::whereSlug('out-of-stock')->firstOrFail()->id, |
|
94
|
|
|
'quantity' => 0, |
|
95
|
|
|
]); |
|
96
|
|
|
|
|
97
|
|
|
// free |
|
98
|
|
|
Factory::create(new Inventory, [ |
|
99
|
|
|
'product_id' => Product::whereSlug('free-thing')->firstOrFail()->id, |
|
100
|
|
|
'quantity' => 1, |
|
101
|
|
|
]); |
|
102
|
|
|
|
|
103
|
|
|
// shirt |
|
104
|
|
|
Factory::create(new Inventory, [ |
|
105
|
|
|
'product_id' => Product::whereSlug('shirt')->firstOrFail()->id, |
|
106
|
|
|
'quantity' => 5, |
|
107
|
|
|
'value_ids' => [1, 4], |
|
108
|
|
|
]); |
|
109
|
|
|
|
|
110
|
|
|
Factory::create(new Inventory, [ |
|
111
|
|
|
'product_id' => Product::whereSlug('shirt')->firstOrFail()->id, |
|
112
|
|
|
'quantity' => 5, |
|
113
|
|
|
'value_ids' => [1, 5], |
|
114
|
|
|
]); |
|
115
|
|
|
|
|
116
|
|
|
Factory::create(new Inventory, [ |
|
117
|
|
|
'product_id' => Product::whereSlug('shirt')->firstOrFail()->id, |
|
118
|
|
|
'quantity' => 0, |
|
119
|
|
|
'value_ids' => [1, 6], |
|
120
|
|
|
]); |
|
121
|
|
|
|
|
122
|
|
|
echo " - Inventories\n"; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
protected function seedOptions() |
|
126
|
|
|
{ |
|
127
|
|
|
$shirt = Product::whereSlug('shirt')->firstOrFail(); |
|
128
|
|
|
|
|
129
|
|
|
$size = Factory::create(new Option, [ |
|
|
|
|
|
|
130
|
|
|
'name' => 'Size', |
|
131
|
|
|
'placeholder' => '-- select size --', |
|
132
|
|
|
'product_id' => $shirt->id, |
|
133
|
|
|
'value_data' => [ |
|
134
|
|
|
['id' => null, 'name' => 'Small', 'sort_order' => 0], |
|
135
|
|
|
['id' => null, 'name' => 'Medium', 'sort_order' => 1], |
|
136
|
|
|
['id' => null, 'name' => 'Large', 'sort_order' => 2], |
|
137
|
|
|
], |
|
138
|
|
|
]); |
|
139
|
|
|
|
|
140
|
|
|
$size = Factory::create(new Option, [ |
|
|
|
|
|
|
141
|
|
|
'name' => 'Color', |
|
142
|
|
|
'placeholder' => '-- select color --', |
|
143
|
|
|
'product_id' => $shirt->id, |
|
144
|
|
|
'value_data' => [ |
|
145
|
|
|
['id' => null, 'name' => 'Red', 'sort_order' => 0], |
|
146
|
|
|
['id' => null, 'name' => 'Green', 'sort_order' => 1], |
|
147
|
|
|
['id' => null, 'name' => 'Blue', 'sort_order' => 2], |
|
148
|
|
|
], |
|
149
|
|
|
]); |
|
150
|
|
|
|
|
151
|
|
|
echo " - Options\n"; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
protected function seedProducts() |
|
155
|
|
|
{ |
|
156
|
|
|
$noInventories = Factory::create(new Product, [ |
|
|
|
|
|
|
157
|
|
|
'description_html' => '<p>This product is out of stock because it had no inventories</p>', |
|
158
|
|
|
'name' => 'No inventories', |
|
159
|
|
|
'slug' => 'no-inventories', |
|
160
|
|
|
]); |
|
161
|
|
|
|
|
162
|
|
|
$outOfStock = Factory::create(new Product, [ |
|
|
|
|
|
|
163
|
|
|
'description_html' => '<p>This product is out of stock</p>', |
|
164
|
|
|
'name' => 'Out of stock', |
|
165
|
|
|
'slug' => 'out-of-stock', |
|
166
|
|
|
]); |
|
167
|
|
|
|
|
168
|
|
|
$free = Factory::create(new Product, [ |
|
|
|
|
|
|
169
|
|
|
'base_price' => 0, |
|
170
|
|
|
'description_html' => '<p>This product tests a base price of zero</p>', |
|
171
|
|
|
'name' => 'Free thing', |
|
172
|
|
|
'slug' => 'free-thing', |
|
173
|
|
|
]); |
|
174
|
|
|
|
|
175
|
|
|
$shirt = Factory::create(new Product, [ |
|
|
|
|
|
|
176
|
|
|
'base_price' => 19.99, |
|
177
|
|
|
'description_html' => '<p>This product tests multiple inventories</p>', |
|
178
|
|
|
'name' => 'Shirt', |
|
179
|
|
|
'slug' => 'shirt', |
|
180
|
|
|
]); |
|
181
|
|
|
|
|
182
|
|
|
echo " - Products\n"; |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.