CartModify::getName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/*
3
 * Licensed under the Apache License, Version 2.0 (the "License");
4
 * you may not use this file except in compliance with the License.
5
 * You may obtain a copy of the License at
6
 *
7
 * http://www.apache.org/licenses/LICENSE-2.0
8
 *
9
 * Unless required by applicable law or agreed to in writing, software
10
 * distributed under the License is distributed on an "AS IS" BASIS,
11
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
 * See the License for the specific language governing permissions and
13
 * limitations under the License.
14
 */
15
16
namespace ApaiIO\Operations;
17
18
/**
19
 * A cart modify operation
20
 *
21
 * @see http://docs.aws.amazon.com/AWSECommerceService/latest/DG/CartModify.html
22
 */
23
class CartModify extends AbstractOperation implements \Countable
24
{
25
    private $itemCounter = 1;
26
27
    const ITEM_LIMIT = 10;
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 1
    public function getName()
33
    {
34 1
        return 'CartModify';
35
    }
36
37
    /**
38
     * Returns the cart id
39
     *
40
     * @return string
41
     */
42 1
    public function getCartId()
43
    {
44 1
        return $this->getSingleOperationParameter('CartId');
45
    }
46
47
    /**
48
     * Sets the cart id
49
     *
50
     * @param string $cartId
51
     */
52 2
    public function setCartId($cartId)
53
    {
54 2
        $this->parameters['CartId'] = $cartId;
55 2
    }
56
57
    /**
58
     * Returns the HMAC
59
     *
60
     * @return mixed
61
     */
62 1
    public function getHMAC()
63
    {
64 1
        return $this->getSingleOperationParameter('HMAC');
65
    }
66
67
    /**
68
     * Sets the HMAC
69
     *
70
     * @param string $HMAC
71
     */
72 2
    public function setHMAC($HMAC)
73
    {
74 2
        $this->parameters['HMAC'] = $HMAC;
75 2
    }
76
77
    /**
78
     * Changes quantity of a CartItem
79
     *
80
     * @param string  $cartItemId The CartItemId retrieved from CartCreate | CartAdd result
81
     * @param integer $quantity   How many items should be in cart after modification
82
     *
83
     * @return $this
84
     *
85
     * @throws \OverflowException        if more then self::ITEM_LIMIT items are to be modified
86
     * @throws \InvalidArgumentException if $quantity is not an integer
87
     * @throws \OutOfRangeException      if $quantity is not in the allowed range: 0 <= $quantity <= 999
88
     */
89 8 View Code Duplication
    public function modifyQuantity($cartItemId, $quantity)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
90
    {
91 8
        $this->validateModificationCount();
92 8
        $this->validateQuantity($quantity);
93
94 4
        $this->parameters['Item.' . $this->itemCounter . '.CartItemId'] = $cartItemId;
95 4
        $this->parameters['Item.' . $this->itemCounter . '.Quantity']   = $quantity;
96
97 4
        $this->itemCounter++;
98
99 4
        return $this;
100
    }
101
102
    /**
103
     * Changes an action of CartIteam to move between Cart and SaveForLater lists
104
     *
105
     * @param string $cartItemId The CartItemId retrieved from CartCreate | CartAdd result
106
     * @param string $action     Valid Values: MoveToCart | SaveForLater
107
     *
108
     * @return $this
109
     *
110
     * @throws \OutOfRangeException if more then self::ITEM_LIMIT items are to be modified
111
     */
112 5 View Code Duplication
    public function modifyAction($cartItemId, $action)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
113
    {
114 5
        $this->validateModificationCount();
115 5
        $this->validateAction($action);
116
117 3
        $this->parameters['Item.' . $this->itemCounter . '.CartItemId'] = $cartItemId;
118 3
        $this->parameters['Item.' . $this->itemCounter . '.Action']     = $action;
119
120 3
        $this->itemCounter++;
121
122 3
        return $this;
123
    }
124
125
    /**
126
     * Counts cart modification for this operation
127
     *
128
     * @link  http://php.net/manual/en/countable.count.php
129
     * @return int The number of modifications for this operations
130
     *
131
     * @since 5.1.0
132
     */
133 12
    public function count()
134
    {
135 12
        return $this->itemCounter - 1;
136
    }
137
138
    /**
139
     * @param integer $quantity
140
     */
141 8
    private function validateQuantity($quantity)
142
    {
143 8
        if (false === is_int($quantity)) {
144 2
            throw new \InvalidArgumentException(
145 2
                sprintf('quantity must be integer, %s given', gettype($quantity))
146
            );
147
        }
148
149 6
        if ($quantity < 0 || $quantity > 999) {
150 2
            throw new \OutOfRangeException(
151 2
                sprintf('%s is an invalid quantity value. It has to be numeric and between 0 and 999', $quantity)
152
            );
153
        }
154 4
    }
155
156
    /**
157
     * Validates ActionTypes
158
     *
159
     * @param string $action
160
     */
161 5
    private function validateAction($action)
162
    {
163
        switch ($action) {
164 5
            case 'MoveToCart':
165 4
            case 'SaveForLater':
166 3
                return;
167
            default:
168 2
                throw new \OutOfRangeException(
169 2
                    sprintf('%s is not a valid action. Must be one of: MoveToCart, SaveForLater', $action)
170
                );
171
        }
172
    }
173
174
    /**
175
     * Validates maximum request size
176
     */
177 12
    private function validateModificationCount()
178
    {
179 12
        if (count($this) >= self::ITEM_LIMIT) {
180 1
            throw new \OverflowException(
181 1
                sprintf('You must not do more than %d modifications per request', self::ITEM_LIMIT)
182
            );
183
        }
184 12
    }
185
}
186