Completed
Pull Request — master (#72)
by
unknown
06:35
created

ItemCollection::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
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 Illuminate\Support\Collection;
11
12
class ItemCollection extends Collection {
13
14
    /**
15
     * Sets the number of decimal points.
16
     *
17
     * @var
18
     */
19
    protected $decimals;
20
21
    /**
22
     * Definces the decimal point delimiter type
23
     *
24
     * @var
25
     */
26
    protected $dec_point;
27
28
    /**
29
     * Defines the thousands point delimiter type
30
     *
31
     * @var
32
     */
33
    protected $thousands_sep;
34
35
    /**
36
     * ItemCollection constructor.
37
     * @param array|mixed $items
38
     * @param $config
39
     */
40
    public function __construct($items, $config)
41
    {
42
        parent::__construct($items);
43
44
        $this->decimals = $config['decimals'];
45
        $this->dec_point = $config['dec_point'];
46
        $this->thousands_sep = $config['thousands_sep'];
47
    }
48
49
    /**
50
     * get the sum of price
51
     *
52
     * @return mixed|null
53
     */
54
    public function getPriceSum()
55
    {
56
        return $this->price * $this->quantity;
0 ignored issues
show
Documentation introduced by
The property price does not exist on object<Darryldecode\Cart\ItemCollection>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property quantity does not exist on object<Darryldecode\Cart\ItemCollection>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
57
    }
58
59
    public function __get($name)
60
    {
61
        if( $this->has($name) ) return $this->get($name);
62
        return null;
63
    }
64
65
    /**
66
     * check if item has conditions
67
     *
68
     * @return bool
69
     */
70
    public function hasConditions()
71
    {
72
        if( ! isset($this['conditions']) ) return false;
73
        if( is_array($this['conditions']) )
74
        {
75
            return count($this['conditions']) > 0;
76
        }
77
        $conditionInstance = "Darryldecode\\Cart\\CartCondition";
78
        if( $this['conditions'] instanceof $conditionInstance ) return true;
79
80
        return false;
81
    }
82
83
    /**
84
     * get the single price in which conditions are already applied
85
     *
86
     * @return mixed|null
87
     */
88
    public function getPriceWithConditions() 
89
    {
90
        $originalPrice = $this->price;
0 ignored issues
show
Bug introduced by
The property price does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
91
        $newPrice = 0.00;
92
        $processed = 0;
93
94
        if( $this->hasConditions() )
95
        {
96
            if( is_array($this->conditions) )
0 ignored issues
show
Documentation introduced by
The property conditions does not exist on object<Darryldecode\Cart\ItemCollection>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
97
            {
98
                foreach($this->conditions as $condition)
0 ignored issues
show
Documentation introduced by
The property conditions does not exist on object<Darryldecode\Cart\ItemCollection>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
99
                {
100
                    if( $condition->getTarget() === 'item' )
101
                    {
102
                        ( $processed > 0 ) ? $toBeCalculated = $newPrice : $toBeCalculated = $originalPrice;
103
                        $newPrice = $condition->applyCondition($toBeCalculated);
104
                        $processed++;
105
                    }
106
                }
107
            }
108
            else
109
            {
110
                if( $this['conditions']->getTarget() === 'item' )
111
                {
112
                    $newPrice = $this['conditions']->applyCondition($originalPrice);
113
                }
114
            }
115
116
            return number_format($newPrice, $this->decimals, $this->dec_point, $this->thousands_sep);
117
        }
118
        return number_format($originalPrice, $this->decimals, $this->dec_point, $this->thousands_sep);
119
    }
120
121
    /**
122
     * get the sum of price in which conditions are already applied
123
     *
124
     * @return mixed|null
125
     */
126
    public function getPriceSumWithConditions()
127
    {
128
        return number_format($this->getPriceWithConditions() * $this->quantity, $this->decimals, $this->dec_point, $this->thousands_sep);
0 ignored issues
show
Documentation introduced by
The property quantity does not exist on object<Darryldecode\Cart\ItemCollection>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
129
    }
130
}
131