Completed
Push — master ( fe8e17...a04764 )
by Rafael
01:54
created

CartComponent::_entityExists()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 8
c 1
b 0
f 0
rs 9.4285
cc 3
eloc 4
nc 3
nop 1
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license.
17
 */
18
19
namespace Cart\Controller\Component;
20
21
use Cake\Controller\Component;
22
use Cake\Controller\ComponentRegistry;
23
24
/**
25
 * @author Rafael Queiroz <[email protected]>
26
 */
27
class CartComponent extends Component
28
{
29
30
    /**
31
     * @var array
32
     */
33
    protected $_defaultConfig = [
34
        'storage' => \Cart\Storage\SessionStorage::class
35
    ];
36
37
    /**
38
     * @var array
39
     */
40
    protected $_objects = [];
41
42
    /**
43
     * @var \Cart\Storage\StorageInterface
44
     */
45
    protected $_storage;
46
47
    /**
48
     * @param array $config
49
     */
50
    public function initialize(array $config)
51
    {
52
        parent::initialize($config);
53
        $this->storage(new $this->_config['storage']($this->_registry->getController()->request));
54
        $this->_objects = $this->storage()->read();
55
    }
56
57
    /**
58
     * @param \Cart\Entity\EntityPriceAwareInterface $entity
59
     * @param int $quantity
60
     * @return bool
61
     * @throws \Exception
62
     */
63
    public function add(\Cart\Entity\EntityPriceAwareInterface $entity, $quantity = 1)
64
    {
65
        $this->_validate($entity, $quantity);
66
        $this->_entityExists($entity);
67
68
        $this->_objects[] = [
69
            'entity' => $entity,
70
            'quantity' => $quantity
71
        ];
72
73
        $this->storage()->write($this->_objects);
74
        return true;
75
    }
76
77
    /**
78
     * @param \Cart\Entity\EntityPriceAwareInterface $entity
79
     * @param int $quantity
80
     * @return bool
81
     * @throws \Exception
82
     */
83
    public function edit(\Cart\Entity\EntityPriceAwareInterface $entity, $quantity = 1)
84
    {
85
        $this->_validate($entity, $quantity);
86 View Code Duplication
        foreach ($this->_objects as &$object) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
            if ($object['entity'] == $entity) {
88
                $object['quantity'] = $quantity;
89
                $this->storage()->write($this->_objects);
90
91
                return true;
92
            }
93
        }
94
95
        throw new \Exception();
96
    }
97
98
    /**
99
     * @param \Cart\Entity\EntityPriceAwareInterface $entity
100
     * @return bool
101
     * @throws \Exception
102
     */
103
    public function delete(\Cart\Entity\EntityPriceAwareInterface $entity)
104
    {
105 View Code Duplication
        foreach ($this->_objects as $key => $object) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
            if ($object['entity'] == $entity) {
107
                unset ($this->_objects[$key]);
108
                $this->storage()->write($this->_objects);
109
                return true;
110
            }
111
        }
112
113
114
        throw new \Exception();
115
    }
116
117
    /**
118
     * @return array
119
     */
120
    public function get()
121
    {
122
        return $this->storage()->read();
123
    }
124
125
    /**
126
     * @return int
127
     */
128
    public function total()
129
    {
130
        $total = 0;
131
        foreach ($this->_objects as $object) {
132
            $total += $object['entity']->price * $object['quantity'];
133
        }
134
135
        return $total;
136
    }
137
138
    /**
139
     * @param \Cart\Storage\StorageInterface $storage
140
     * @return \Cart\Storage\StorageInterface
141
     */
142
    public function storage(\Cart\Storage\StorageInterface $storage = null)
143
    {
144
        if (!$this->_storage instanceof \Cart\Storage\StorageInterface) {
145
            $this->_storage = $storage;
146
        }
147
148
        return $this->_storage;
149
    }
150
151
    /**
152
     * @param $entity
153
     * @param $quantity
154
     * @throws \Exception
155
     */
156
    protected function _validate($entity, $quantity)
157
    {
158
        if (!$entity instanceof \Cart\Entity\EntityPriceAwareInterface) {
159
            throw new \Exception();
160
        }
161
        if ($quantity < 1) {
162
            throw new \Exception();
163
        }
164
    }
165
166
    /**
167
     * @param $entity
168
     * @throws \Exception
169
     */
170
    protected function _entityExists($entity)
171
    {
172
        foreach ($this->_objects as $object) {
173
            if ($object['entity'] == $entity) {
174
                throw new \Exception();
175
            }
176
        }
177
    }
178
179
}