This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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
|
|||
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
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. ![]() |
|||
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 | public function count() |
||
126 | { |
||
127 | return count ($this->get()); |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * @return void |
||
132 | */ |
||
133 | public function clear() |
||
134 | { |
||
135 | $this->storage()->delete(); |
||
136 | } |
||
137 | |||
138 | |||
139 | /** |
||
140 | * @return int |
||
141 | */ |
||
142 | public function total() |
||
143 | { |
||
144 | $total = 0; |
||
145 | foreach ($this->_objects as $object) { |
||
146 | $total += $object['entity']->price * $object['quantity']; |
||
147 | } |
||
148 | |||
149 | return $total; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * @param \Cart\Storage\StorageInterface $storage |
||
154 | * @return \Cart\Storage\StorageInterface |
||
155 | */ |
||
156 | public function storage(\Cart\Storage\StorageInterface $storage = null) |
||
157 | { |
||
158 | if (!$this->_storage instanceof \Cart\Storage\StorageInterface) { |
||
159 | $this->_storage = $storage; |
||
160 | } |
||
161 | |||
162 | return $this->_storage; |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * @param $entity |
||
167 | * @param $quantity |
||
168 | * @throws \Exception |
||
169 | */ |
||
170 | protected function _validate($entity, $quantity) |
||
171 | { |
||
172 | if (!$entity instanceof \Cart\Entity\EntityPriceAwareInterface) { |
||
173 | throw new \Exception(); |
||
174 | } |
||
175 | if ($quantity < 1) { |
||
176 | throw new \Exception(); |
||
177 | } |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * @param $entity |
||
182 | * @throws \Exception |
||
183 | */ |
||
184 | protected function _entityExists($entity) |
||
185 | { |
||
186 | foreach ($this->_objects as $object) { |
||
187 | if ($object['entity'] == $entity) { |
||
188 | throw new \Exception(); |
||
189 | } |
||
190 | } |
||
191 | } |
||
192 | |||
193 | } |
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.