Completed
Push — master ( 611d86...fe8e17 )
by Rafael
02:00
created

CartComponent::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 1
eloc 8
nc 1
nop 2
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
    }
55
56
    /**
57
     * @param \Cart\Entity\EntityPriceAwareInterface $entity
58
     * @param int $quantity
59
     * @return bool
60
     * @throws \Exception
61
     */
62
    public function add(\Cart\Entity\EntityPriceAwareInterface $entity, $quantity = 1)
63
    {
64
        $this->_validate($entity, $quantity);
65
        $this->_entityExists($entity);
66
67
        $this->_objects[] = [
68
            'entity' => $entity,
69
            'quantity' => $quantity
70
        ];
71
72
        $this->storage()->write($this->_objects);
73
        return true;
74
    }
75
76
    /**
77
     * @param \Cart\Entity\EntityPriceAwareInterface $entity
78
     * @param int $quantity
79
     * @return bool
80
     * @throws \Exception
81
     */
82
    public function edit(\Cart\Entity\EntityPriceAwareInterface $entity, $quantity = 1)
83
    {
84
        $this->_validate($entity, $quantity);
85 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...
86
            if ($object['entity'] == $entity) {
87
                $object['quantity'] = $quantity;
88
                $this->storage()->write($this->_objects);
89
90
                return true;
91
            }
92
        }
93
94
        throw new \Exception();
95
    }
96
97
    /**
98
     * @param \Cart\Entity\EntityPriceAwareInterface $entity
99
     * @return bool
100
     * @throws \Exception
101
     */
102
    public function delete(\Cart\Entity\EntityPriceAwareInterface $entity)
103
    {
104 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...
105
            if ($object['entity'] == $entity) {
106
                unset ($this->_objects[$key]);
107
                $this->storage()->write($this->_objects);
108
                return true;
109
            }
110
        }
111
112
113
        throw new \Exception();
114
    }
115
116
    /**
117
     * @param \Cart\Storage\StorageInterface $storage
118
     * @return \Cart\Storage\StorageInterface
119
     */
120
    public function storage(\Cart\Storage\StorageInterface $storage = null)
121
    {
122
        if (!$this->_storage instanceof \Cart\Storage\StorageInterface) {
123
            $this->_storage = $storage;
124
        }
125
126
        return $this->_storage;
127
    }
128
129
    /**
130
     * @param $entity
131
     * @param $quantity
132
     * @throws \Exception
133
     */
134
    protected function _validate($entity, $quantity)
135
    {
136
        if (!$entity instanceof \Cart\Entity\EntityPriceAwareInterface) {
137
            throw new \Exception();
138
        }
139
        if ($quantity < 1) {
140
            throw new \Exception();
141
        }
142
    }
143
144
    /**
145
     * @param $entity
146
     * @throws \Exception
147
     */
148
    protected function _entityExists($entity)
149
    {
150
        foreach ($this->_objects as $object) {
151
            if ($object['entity'] == $entity) {
152
                throw new \Exception();
153
            }
154
        }
155
    }
156
157
}