CartItem::offsetSet()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
namespace jamesdb\Cart;
4
5
use ArrayAccess;
6
use jamesdb\Cart\Exception\CartPropertyNotIntegerException;
7
use Money\Currency;
8
9
class CartItem implements ArrayAccess
10
{
11
    /**
12
     * @var array
13
     */
14
    protected $item = [];
15
16
    /**
17
     * Constructor.
18
     *
19
     * @param array $data
20
     */
21 51
    public function __construct(array $data = [])
22
    {
23
        $defaults = [
24 51
            'options'  => [],
25 51
            'price'    => 0,
26 51
            'quantity' => 1,
27
            'tax'      => 0
28 51
        ];
29
30 51
        $data = array_merge($defaults, $data);
31
32 51
        foreach ($data as $key => $value) {
33 51
            $this->set($key, $value);
34 51
        }
35 51
    }
36
37
    /**
38
     * Return the rowid of item.
39
     *
40
     * @return string
41
     */
42 45
    public function getRowId()
43
    {
44 45
        $rowId = $this->item;
45
46 45
        $ignoredProperties = ['quantity'];
47
48 45
        foreach ($ignoredProperties as $property) {
49 45
            if (array_key_exists($property, $rowId)) {
50 45
                unset($rowId[$property]);
51 45
            }
52 45
        }
53
54 45
        return md5(serialize($rowId));
55
    }
56
57
    /**
58
     * Set property key and value.
59
     *
60
     * @param  string $key
61
     * @param  mixed  $value
62
     *
63
     * @throws \jamesdb\Cart\Exception\CartPropertyNotIntegerException
64
     *
65
     * @return void
66
     */
67 51
    public function set($key, $value)
68
    {
69 51
        $numeric = ['price', 'tax', 'quantity'];
70
71 51
        if ((in_array($key, $numeric)) && (! is_int($value))) {
72 3
            throw new CartPropertyNotIntegerException(
73 3
                sprintf('The [%s] property must be an integer', $key)
74 3
            );
75
        }
76
77 51
        $this->item[$key] = $value;
78 51
    }
79
80
    /**
81
     * Get a property by key.
82
     *
83
     * @param  string $key
84
     *
85
     * @return mixed
86
     */
87 39
    public function get($key)
88
    {
89 39
        if ($key === 'rowid') {
90 3
            return $this->getRowId();
91
        }
92
93 36
        return $this->item[$key];
94
    }
95
96
    /**
97
     * Return item price excluding tax.
98
     *
99
     * @param  \Money\Currency $currency
100
     *
101
     * @return \Money\Money
102
     */
103 3
    public function getPriceExcludingTax(Currency $currency)
104
    {
105 3
        return (new Money($this->price, $currency))->getMoney()->multiply($this->quantity);
0 ignored issues
show
Documentation introduced by
The property price does not exist on object<jamesdb\Cart\CartItem>. 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<jamesdb\Cart\CartItem>. 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...
106
    }
107
108
    /**
109
     * Return item price including tax.
110
     *
111
     * @param  \Money\Currency $currency
112
     *
113
     * @return \Money\Money
114
     */
115 6
    public function getPrice(Currency $currency)
116
    {
117 6
        return (new Money($this->price + $this->tax, $currency))->getMoney()->multiply($this->quantity);
0 ignored issues
show
Documentation introduced by
The property price does not exist on object<jamesdb\Cart\CartItem>. 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 tax does not exist on object<jamesdb\Cart\CartItem>. 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<jamesdb\Cart\CartItem>. 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...
118
    }
119
120
    /**
121
     * Return the item tax.
122
     *
123
     * @param  \Money\Currency $currency
124
     *
125
     * @return \Money\Money
126
     */
127 3
    public function getTax(Currency $currency)
128
    {
129 3
        return (new Money($this->tax, $currency))->getMoney()->multiply($this->quantity);
0 ignored issues
show
Documentation introduced by
The property tax does not exist on object<jamesdb\Cart\CartItem>. 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<jamesdb\Cart\CartItem>. 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...
130
    }
131
132
    /**
133
     * Export the item as array.
134
     *
135
     * @return array
136
     */
137 42
    public function toArray()
138
    {
139
        return [
140 42
            'id'   => $this->getRowId(),
141 42
            'data' => $this->item
142 42
        ];
143
    }
144
145
    /**
146
     * Set property key and value.
147
     *
148
     * @param  string $key
149
     * @param  mixed  $value
150
     *
151
     * @return void
152
     */
153 21
    public function __set($key, $value)
154
    {
155 21
        $this->set($key, $value);
156 18
    }
157
158
    /**
159
     * Get a property by key.
160
     *
161
     * @param  string $key
162
     *
163
     * @return mixed
164
     */
165 36
    public function __get($key)
166
    {
167 36
        return $this->get($key);
168
    }
169
170
    /**
171
     * Array Access get.
172
     *
173
     * @param  string $offset
174
     *
175
     * @return boolean
176
     */
177 6
    public function offsetExists($offset)
178
    {
179 6
        return isset($this->item[$offset]);
180
    }
181
182
    /**
183
     * Array Access get.
184
     *
185
     * @param  string $offset
186
     *
187
     * @return mixed
188
     */
189 6
    public function offsetGet($offset)
190
    {
191 6
        return $this->get($offset);
192
    }
193
194
    /**
195
     * Array Access set.
196
     *
197
     * @param  string $key
198
     * @param  mixed  $value
199
     *
200
     * @return void
201
     */
202 3
    public function offsetSet($key, $value)
203
    {
204 3
        $this->set($key, $value);
205 3
    }
206
207
    /**
208
     * Array Access unset.
209
     *
210
     * @param  string $offset
211
     *
212
     * @return void
213
     */
214 3
    public function offsetUnset($offset)
215
    {
216 3
        unset($this->item[$offset]);
217 3
    }
218
}
219