Completed
Push — master ( e6495e...30ded3 )
by Scott
02:04
created

DevSeeder   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 6
dl 0
loc 76
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 12 2
A seedCategories() 0 7 1
A seedProducts() 0 11 2
A seedOptionsAndInventories() 0 21 1
A seedDiscounts() 0 18 1
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
    }
35
36
    protected function seedProducts($count)
37
    {
38
        $categories = Category::count();
39
40
        for ($i = 0; $i < $count; $i++) {
41
            $product = Factory::create(new Product, ['name' => 'Product '.$i, 'slug' => 'product-'.$i]);
42
            $product->categories()->sync([rand(1, $categories)]);
43
        }
44
45
        Product::syncAllInheritedCategories();
46
    }
47
48
    protected function seedOptionsAndInventories()
49
    {
50
        Product::all()->each(function($product) {
51
            $option = Factory::create(new Option, [
52
                'name' => 'Size',
53
                'placeholder' => '-- select size --',
54
                'product_id' => $product->id,
55
            ]);
56
57
            $value = Factory::create(new OptionValue, ['option_id' => $option->id, 'name' => 'Small', 'sort_order' => 0]);
58
            Factory::create(new OptionValue, ['option_id' => $option->id, 'name' => 'Medium', 'sort_order' => 1]);
59
            Factory::create(new OptionValue, ['option_id' => $option->id, 'name' => 'Large', 'sort_order' => 2]);
60
61
            $inventory = Factory::create(new Inventory, [
62
                'product_id' => $product->id,
63
                'quantity' => rand(0, 3),
64
            ]);
65
66
            $inventory->optionValues()->sync([$value->id]);
67
        });
68
    }
69
70
    protected function seedDiscounts()
71
    {
72
        Factory::create(new Discount, [
73
            'name' => 'Expired',
74
            'end_at' => Carbon::yesterday(),
75
        ]);
76
77
        Factory::create(new Discount, [
78
            'name' => 'Active',
79
            'start_at' => Carbon::yesterday(),
80
            'end_at' => Carbon::tomorrow(),
81
        ]);
82
83
        Factory::create(new Discount, [
84
            'name' => 'Upcoming',
85
            'start_at' => Carbon::tomorrow(),
86
        ]);
87
    }
88
}
89