Completed
Push — master ( 89f64c...410dc6 )
by Scott
03:05
created

DevSeeder::run()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 7
nc 2
nop 0
1
<?php namespace Bedard\Shop\Updates;
2
3
use Bedard\Shop\Models\Category;
4
use Bedard\Shop\Models\Discount;
5
use Bedard\Shop\Models\Inventory;
6
use Bedard\Shop\Models\Option;
7
use Bedard\Shop\Models\OptionValue;
8
use Bedard\Shop\Models\Product;
9
use Bedard\Shop\Tests\Factory;
10
use Carbon\Carbon;
11
use October\Rain\Database\Updates\Seeder;
12
13
class DevSeeder extends Seeder
14
{
15
    public function run()
16
    {
17
        // only run the seeder in development
18
        if (app()->env !== 'dev') {
19
            return;
20
        }
21
22
        $this->seedCategories();
23
        $this->seedProducts(10);
24
        $this->seedOptionsAndInventories();
25
        $this->seedDiscounts();
26
    }
27
28
    protected function seedCategories()
29
    {
30
        $parent1 = Factory::create(new Category, ['name' => 'Parent 1', 'slug' => 'parent-1']);
31
        $parent2 = Factory::create(new Category, ['name' => 'Parent 2', 'slug' => 'parent-2']);
32
        Factory::create(new Category, ['name' => 'Child 1', 'slug' => 'child-1', 'parent_id' => $parent1->id]);
33
        Factory::create(new Category, ['name' => 'Child 2', 'slug' => 'child-2', 'parent_id' => $parent2->id]);
34
        Factory::create(new Category, [
35
            'name' => 'On sale',
36
            'slug' => 'sale',
37
            'category_filters' => [
38
                [
39
                    'comparator' => '<',
40
                    'id' => null,
41
                    'is_deleted' => false,
42
                    'left' => 'price',
43
                    'right' => 'base_price',
44
                    'value' => 0,
45
                ],
46
            ],
47
        ]);
48
49
    }
50
51
    protected function seedProducts($count)
52
    {
53
        $categories = Category::count();
54
55
        for ($i = 0; $i < $count; $i++) {
56
            $product = Factory::create(new Product, ['name' => 'Product '.$i, 'slug' => 'product-'.$i]);
57
            $product->categories()->sync([rand(1, $categories)]);
58
        }
59
60
        Product::syncAllInheritedCategories();
61
    }
62
63
    protected function seedOptionsAndInventories()
64
    {
65
        Product::all()->each(function ($product) {
66
            $option = Factory::create(new Option, [
67
                'name' => 'Size',
68
                'placeholder' => '-- select size --',
69
                'product_id' => $product->id,
70
            ]);
71
72
            $value = Factory::create(new OptionValue, ['option_id' => $option->id, 'name' => 'Small', 'sort_order' => 0]);
73
            Factory::create(new OptionValue, ['option_id' => $option->id, 'name' => 'Medium', 'sort_order' => 1]);
74
            Factory::create(new OptionValue, ['option_id' => $option->id, 'name' => 'Large', 'sort_order' => 2]);
75
76
            $inventory = Factory::create(new Inventory, [
77
                'product_id' => $product->id,
78
                'quantity' => rand(0, 3),
79
            ]);
80
81
            $inventory->optionValues()->sync([$value->id]);
82
        });
83
    }
84
85
    protected function seedDiscounts()
86
    {
87
        Factory::create(new Discount, [
88
            'name' => 'Expired',
89
            'end_at' => Carbon::yesterday(),
90
        ]);
91
92
        $active = Factory::create(new Discount, [
93
            'name' => 'Active',
94
            'start_at' => Carbon::yesterday(),
95
            'end_at' => Carbon::tomorrow(),
96
            'amount_percentage' => 25,
97
            'is_percentage' => true,
98
        ]);
99
100
        $active->products()->sync([1, 2, 3]);
101
        $active->save();
102
103
        $upcoming = Factory::create(new Discount, [
104
            'name' => 'Upcoming',
105
            'start_at' => Carbon::tomorrow(),
106
            'amount_exact' => 5,
107
            'is_percentage' => false,
108
        ]);
109
110
        $upcoming->categories()->sync([1]);
111
        $upcoming->save();
112
    }
113
}
114