ItemCollection::getPriceSum()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php namespace Darryldecode\Cart;
2
3
/**
4
 * Created by PhpStorm.
5
 * User: darryl
6
 * Date: 1/17/2015
7
 * Time: 11:03 AM
8
 */
9
10
use Darryldecode\Cart\Helpers\Helpers;
11
use Illuminate\Support\Collection;
12
13
class ItemCollection extends Collection
14
{
15
16
    /**
17
     * Sets the config parameters.
18
     *
19
     * @var
20
     */
21
    protected $config;
22
23
    /**
24
     * ItemCollection constructor.
25
     * @param array|mixed $items
26
     * @param $config
27
     */
28
    public function __construct($items, $config = [])
29
    {
30
        parent::__construct($items);
31
32
        $this->config = $config;
33
    }
34
35
    /**
36
     * get the sum of price
37
     *
38
     * @return mixed|null
39
     */
40
    public function getPriceSum()
41
    {
42
        return Helpers::formatValue($this->price * $this->quantity, $this->config['format_numbers'], $this->config);
43
    }
44
45
    public function __get($name)
46
    {
47
        if ($this->has($name) || $name == 'model') {
48
            return !is_null($this->get($name)) ? $this->get($name) : $this->getAssociatedModel();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return !is_null($this->g...->getAssociatedModel(); (object|integer|double|string|array|boolean) is incompatible with the return type of the parent method Illuminate\Support\Collection::__get of type Illuminate\Support\HigherOrderCollectionProxy.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
49
        }
50
        return null;
51
    }
52
53
    /**
54
     * return the associated model of an item
55
     *
56
     * @return bool
57
     */
58
    protected function getAssociatedModel()
59
    {
60
        if (!$this->has('associatedModel')) {
61
            return null;
62
        }
63
64
        $associatedModel = $this->get('associatedModel');
65
66
        return with(new $associatedModel())->find($this->get('id'));
67
    }
68
69
    /**
70
     * check if item has conditions
71
     *
72
     * @return bool
73
     */
74
    public function hasConditions()
75
    {
76
        if (!isset($this['conditions'])) return false;
77
        if (is_array($this['conditions'])) {
78
            return count($this['conditions']) > 0;
79
        }
80
        $conditionInstance = "Darryldecode\\Cart\\CartCondition";
81
        if ($this['conditions'] instanceof $conditionInstance) return true;
82
83
        return false;
84
    }
85
86
    /**
87
     * check if item has conditions
88
     *
89
     * @return mixed|null
90
     */
91
    public function getConditions()
92
    {
93
        if (!$this->hasConditions()) return [];
94
        return $this['conditions'];
95
    }
96
97
    /**
98
     * get the single price in which conditions are already applied
99
     * @param bool $formatted
100
     * @return mixed|null
101
     */
102
    public function getPriceWithConditions($formatted = true)
103
    {
104
        $originalPrice = $this->price;
105
        $newPrice = 0.00;
106
        $processed = 0;
107
108
        if ($this->hasConditions()) {
109
            if (is_array($this->conditions)) {
110
                foreach ($this->conditions as $condition) {
111
                    ($processed > 0) ? $toBeCalculated = $newPrice : $toBeCalculated = $originalPrice;
112
                    $newPrice = $condition->applyCondition($toBeCalculated);
113
                    $processed++;
114
                }
115
            } else {
116
                $newPrice = $this['conditions']->applyCondition($originalPrice);
117
            }
118
119
            return Helpers::formatValue($newPrice, $formatted, $this->config);
120
        }
121
        return Helpers::formatValue($originalPrice, $formatted, $this->config);
122
    }
123
124
    /**
125
     * get the sum of price in which conditions are already applied
126
     * @param bool $formatted
127
     * @return mixed|null
128
     */
129
    public function getPriceSumWithConditions($formatted = true)
130
    {
131
        return Helpers::formatValue($this->getPriceWithConditions(false) * $this->quantity, $formatted, $this->config);
132
    }
133
}
134