Completed
Push — master ( d4660e...a3374d )
by Scott
02:19
created

DevSeeder::seedPromotions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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