Item   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 59
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B __get() 0 24 6
A rawId() 0 4 1
A getKey() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of ibrand/laravel-shopping-cart.
5
 *
6
 * (c) iBrand <https://www.ibrand.cc>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace iBrand\Shoppingcart;
13
14
use Illuminate\Database\Eloquent\Relations\Relation;
15
use Illuminate\Support\Collection;
16
17
/**
18
 * Shopping cart item.
19
 *
20
 * @property int|string $id
21
 * @property string     $__raw_id
22
 */
23
class Item extends Collection
24
{
25
    /**
26
     * The Eloquent model a cart is associated with.
27
     *
28
     * @var string
29
     */
30
    protected $model;
31
32
    /**
33
     * Magic accessor.
34
     *
35
     * @param string $property property name
36
     *
37
     * @return mixed
38
     */
39
    public function __get($property)
40
    {
41
        if ($this->has($property)) {
42
            return $this->get($property);
43
        }
44
45
        if (!$this->get('__model')) {
46
            return;
47
        }
48
49
        $model = $this->get('__model');
50
51
        if(!class_exists($model)){
52
            $model = Relation::getMorphedModel($model);
53
        }
54
55
        $class = explode('\\', $model);
56
57
        if (strtolower(end($class)) === $property || 'model' === $property) {
58
            $model = new $model();
59
60
            return $model->find($this->id);
61
        }
62
    }
63
64
    /**
65
     * Return the raw ID of item.
66
     *
67
     * @return string
68
     */
69
    public function rawId()
70
    {
71
        return $this->__raw_id;
72
    }
73
74
    /**
75
     * @return string
76
     */
77
    public function getKey()
78
    {
79
        return $this->rawId();
80
    }
81
}
82