Item::__get()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 11
cts 11
cp 1
rs 9.584
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 4
1
<?php
2
3
/*
4
 * This file is part of the overtrue/laravel-shopping-cart.
5
 *
6
 * (c) 2016 overtrue <[email protected]>
7
 */
8
9
namespace Overtrue\LaravelShoppingCart;
10
11
use Illuminate\Support\Collection;
12
13
/**
14
 * Shopping cart item.
15
 *
16
 * @property int|string $id
17
 * @property string     $__raw_id
18
 */
19
class Item extends Collection
20
{
21
    /**
22
     * The Eloquent model a cart is associated with.
23
     *
24
     * @var string
25
     */
26
    protected $model;
27
28
    /**
29
     * Magic accessor.
30
     *
31
     * @param string $property property name
32
     *
33
     * @return mixed
34
     */
35 8
    public function __get($property)
36
    {
37 8
        if ($this->has($property)) {
38 8
            return $this->get($property);
39
        }
40
41 1
        if (!$this->get('__model')) {
42 1
            return;
43
        }
44
45 1
        $model = $this->get('__model');
46 1
        $class = explode('\\', $model);
47
48 1
        if (strtolower(end($class)) === $property) {
49 1
            $model = new $model();
50
51 1
            return $model->find($this->id);
52
        }
53
54 1
        return;
55
    }
56
57
    /**
58
     * Return the raw ID of item.
59
     *
60
     * @return string
61
     */
62 4
    public function rawId()
63
    {
64 4
        return $this->__raw_id;
65
    }
66
}
67