Seeds::seedOptions()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
dl 0
loc 28
rs 8.8571
c 3
b 0
f 0
cc 1
eloc 19
nc 1
nop 0
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, [
0 ignored issues
show
Unused Code introduced by
$size is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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, [
0 ignored issues
show
Unused Code introduced by
$size is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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, [
0 ignored issues
show
Unused Code introduced by
$noInventories is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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, [
0 ignored issues
show
Unused Code introduced by
$outOfStock is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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, [
0 ignored issues
show
Unused Code introduced by
$free is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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, [
0 ignored issues
show
Unused Code introduced by
$shirt is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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