Completed
Push — master ( f82868...9e65d7 )
by Scott
02:13
created

Cart::generateToken()   A

Complexity

Conditions 2
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 2
eloc 4
nc 1
nop 0
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 Guarded fields
17
     */
18
    protected $guarded = ['*'];
19
20
    /**
21
     * @var array Fillable fields
22
     */
23
    protected $fillable = [
24
        'token',
25
    ];
26
27
    /**
28
     * @var array Relations
29
     */
30
    public $hasMany = [
31
        'cartItems' => [
32
            'Bedard\Shop\Models\CartItem',
33
        ],
34
    ];
35
36
    /**
37
     * Before create.
38
     *
39
     * @return void
40
     */
41
    public function beforeCreate()
42
    {
43
        $this->generateToken();
44
    }
45
46
    /**
47
     * Generate a unique token.
48
     *
49
     * @return void
50
     */
51
    protected function generateToken()
52
    {
53
        do {
54
            $this->token = str_random(40);
55
        } while (self::whereToken($this->token)->exists());
56
    }
57
}
58