Completed
Push — master ( 0f0d9a...074283 )
by Scott
02:15
created

Cart::applyPromotion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php namespace Bedard\Shop\Models;
2
3
use Model;
4
5
/**
6
 * Cart Model.
7
 */
8
class Cart extends Model
9
{
10
    /**
11
     * @var string The database table used by the model.
12
     */
13
    public $table = 'bedard_shop_carts';
14
15
    /**
16
     * @var array Default attributes
17
     */
18
    public $attributes = [
19
        'id' => null,
20
    ];
21
22
    /**
23
     * @var array Guarded fields
24
     */
25
    protected $guarded = ['*'];
26
27
    /**
28
     * @var array Fillable fields
29
     */
30
    protected $fillable = [
31
        'token',
32
    ];
33
34
    /**
35
     * @var array Relations
36
     */
37
    public $belongsTo = [
38
        'promotion' => [
39
            'Bedard\Shop\Models\Promotion',
40
        ],
41
    ];
42
43
    public $hasMany = [
44
        'items' => [
45
            'Bedard\Shop\Models\CartItem',
46
        ],
47
    ];
48
49
    /**
50
     * Apply a promotion to the cart.
51
     *
52
     * @param  string $code     The promotion code to apply.
0 ignored issues
show
Bug introduced by
There is no parameter named $code. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
53
     * @return void
54
     */
55
    public function applyPromotion($name)
56
    {
57
        $promotion = Promotion::isActive()->whereName($name)->firstOrFail();
58
        $this->promotion_id = $promotion->id;
59
        $this->save();
60
    }
61
62
    /**
63
     * Before create.
64
     *
65
     * @return void
66
     */
67
    public function beforeCreate()
68
    {
69
        $this->generateToken();
70
    }
71
72
    /**
73
     * Generate a unique token.
74
     *
75
     * @return void
76
     */
77
    protected function generateToken()
78
    {
79
        do {
80
            $this->token = str_random(40);
81
        } while (self::whereToken($this->token)->exists());
82
    }
83
84
    /**
85
     * Select carts that are open.
86
     *
87
     * @param  \October\Rain\Database\Builder   $query
88
     * @return \October\Rain\Database\Builder
89
     */
90
    public function scopeIsOpen($query)
91
    {
92
        return $query; // @todo
93
    }
94
}
95