Factory::fill()   C
last analyzed

Complexity

Conditions 9
Paths 16

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 6.412
c 0
b 0
f 0
cc 9
eloc 14
nc 16
nop 3
1
<?php namespace Bedard\Shop\Classes;
2
3
use Faker;
4
use Faker\Provider\Lorem;
5
use Model;
6
7
class Factory
8
{
9
    /**
10
     * Create a model and save it to the database.
11
     *
12
     * @param  Model    $model  Model to create
13
     * @param  array    $omit   Data to fill model with
14
     * @return Model
15
     */
16
    public static function create(Model $model, array $data = [], array $omit = [])
17
    {
18
        $model = self::fill($model, $data, $omit);
19
        $model->save();
20
21
        return $model;
22
    }
23
24
    /**
25
     * Create a model and fill it with data.
26
     *
27
     * @param  Model    $model  Model to fill
28
     * @param  array    $data   Data to fill the model with
29
     * @return Model
30
     */
31
    public static function fill(Model $model, array $data = [], array $omit = [])
32
    {
33
        $seed = [];
34
35
        switch (get_class($model)) {
36
            case 'Bedard\Shop\Models\Cart': $seed = self::getCartData($data); break;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
37
            case 'Bedard\Shop\Models\CartItem': $seed = self::getCartItemData($data); break;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
38
            case 'Bedard\Shop\Models\Category': $seed = self::getCategoryData($data); break;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
39
            case 'Bedard\Shop\Models\Inventory': $seed = self::getInventoryData($data); break;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
40
            case 'Bedard\Shop\Models\Option': $seed = self::getOptionData($data); break;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
41
            case 'Bedard\Shop\Models\Product': $seed = self::getProductData($data); break;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
42
            case 'Bedard\Shop\Models\Status': $seed = self::getStatusData($data); break;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
43
        }
44
45
        $model->fill(array_merge($seed, $data));
46
47
        foreach ($omit as $key) {
48
            unset($model->attributes[$key]);
49
        }
50
51
        return $model;
52
    }
53
54
    /**
55
     * Cart.
56
     *
57
     * @param  array $data
58
     * @return array
59
     */
60
    public static function getCartData(array $data = [])
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
61
    {
62
        return [
63
            //
64
        ];
65
    }
66
67
    /**
68
     * CartItem.
69
     *
70
     * @param  array $data
71
     * @return array
72
     */
73
    public static function getCartItemData(array $data = [])
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
74
    {
75
        return [
76
            'cart_id' => 0,
77
            'inventory_id' => 0,
78
            'product_id' => 0,
79
        ];
80
    }
81
82
    /**
83
     * Category.
84
     *
85
     * @param  array $data
86
     * @return array
87
     */
88
    public static function getCategoryData(array $data = [])
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
89
    {
90
        $faker = Faker\Factory::create();
91
92
        return [
93
            'description_html' => '<p>'.Lorem::paragraph().'</p>',
94
            'name' => $faker->words(2, true),
95
            'slug' => $faker->slug,
96
        ];
97
    }
98
99
    public static function getInventoryData(array $data = [])
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
100
    {
101
        $faker = Faker\Factory::create();
0 ignored issues
show
Unused Code introduced by
$faker 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...
102
103
        return [
104
            'sku' => null,
105
            'quantity' => rand(0, 10),
106
        ];
107
    }
108
109
    /**
110
     * Option.
111
     *
112
     * @param  array $data
113
     * @return array
114
     */
115
    public static function getOptionData(array $data = [])
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
116
    {
117
        $faker = Faker\Factory::create();
118
119
        return [
120
            'name' => $faker->words(2, true),
121
            'placeholder' => $faker->words(3, true),
122
            'sort_order' => 0,
123
            'value_data' => [
124
                ['_key' => 1, 'id' => null, 'name' => 'a', 'sort_order' => 0],
125
            ],
126
        ];
127
    }
128
129
    /**
130
     * Category.
131
     *
132
     * @param  array $data
133
     * @return array
134
     */
135
    public static function getProductData(array $data = [])
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
136
    {
137
        $faker = Faker\Factory::create();
138
139
        return [
140
            'base_price' => rand(1, 100) + (rand(0, 100) / 100),
141
            'description_html' => '<p>'.Lorem::paragraph().'</p>',
142
            'name' => $faker->words(2, true),
143
            'slug' => $faker->slug,
144
        ];
145
    }
146
147
    /**
148
     * Status.
149
     *
150
     * @param  array $data
151
     * @return array
152
     */
153
    public static function getStatusData(array $data = [])
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
154
    {
155
        $faker = Faker\Factory::create();
156
157
        return [
158
            'is_abandoned' => false,
159
            'is_default' => false,
160
            'is_reducing' => false,
161
            'name' => $faker->words(2, true),
162
        ];
163
    }
164
}
165