Completed
Pull Request — master (#10)
by Franco
01:59
created

DMSDocumentCart::getItemsAsArrayList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Class DMSDocumentCart represents the shopping cart.
4
 *
5
 */
6
class DMSDocumentCart extends ViewableData
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
7
{
8
    /**
9
     * A handle to the classes' {@link DMSCartBackendInterface}
10
     *
11
     * @var DMSCartBackendInterface
12
     */
13
    protected $backend;
14
15
    public function __construct($backend = null)
16
    {
17
        parent::__construct();
18
        $this->backend = ($backend) ?: DMSSessionBackend::create(); // Default to DMSSessionBackend if not provided
19
    }
20
21
    /**
22
     * Returns all the cart items as an array
23
     *
24
     * @return array
25
     */
26
    public function getItems()
27
    {
28
        $result = $this->backend->getItems();
29
30
        return $result;
31
    }
32
33
    /**
34
     * Returns all the cart items as an ArrayList
35
     *
36
     * @return ArrayList
37
     */
38
    public function getItemsAsArrayList()
39
    {
40
        return ArrayList::create($this->getItems());
41
    }
42
43
    /**
44
     * Add an {@link DMSRequestItem} object into the cart.
45
     *
46
     * @param DMSRequestItem $item
47
     *
48
     * @return DMSDocumentCart
49
     */
50
    public function addItem(DMSRequestItem $item)
51
    {
52
        $this->backend->addItem($item);
53
54
        return $this;
55
    }
56
57
    /**
58
     * Get a {@link DMSRequestItem} object from the cart.
59
     *
60
     * @param int $itemID The ID of the item
61
     *
62
     * @return DMSRequestItem|boolean
63
     */
64
    public function getItem($itemID)
65
    {
66
        return $this->backend->getItem($itemID);
67
    }
68
69
    /**
70
     * Removes a {@link DMSRequestItem} from the cart by it's id
71
     *
72
     * @param DMSRequestItem $item
73
     *
74
     * @return DMSDocumentCart
75
     */
76
    public function removeItem(DMSRequestItem $item)
77
    {
78
        $this->backend->removeItem($item);
79
80
        return $this;
81
    }
82
83
    /**
84
     * Removes a {@link DMSRequestItem} from the cart by it's id
85
     *
86
     * @param int $itemID
87
     *
88
     * @return DMSDocumentCart
89
     */
90
    public function removeItemByID($itemID)
91
    {
92
        $this->backend->removeItemByID($itemID);
93
94
        return $this;
95
    }
96
97
    /**
98
     * Adjusts (increments, decrements or amends) the quantity of an {@link DMSRequestItem}.'
99
     * A positive $quantity increments the total, whereas a negative value decrements the total. A cart item
100
     * is removed completely if it's value reaches <= 0.
101
     *
102
     * @param int $itemID
103
     * @param int $quantity
104
     *
105
     * @return DMSDocumentCart
106
     */
107
    public function updateItemQuantity($itemID, $quantity)
108
    {
109
        if ($item = $this->getItem($itemID)) {
110
            $currentQuantity = $item->getQuantity();
111
            $newQuantity = $currentQuantity + $quantity;
112
            if ($newQuantity <= 0) {
113
                $this->removeItemByID($itemID);
114
            } else {
115
                $item->setQuantity($newQuantity);
116
                $this->addItem($item);
0 ignored issues
show
Bug introduced by
It seems like $item defined by $this->getItem($itemID) on line 109 can also be of type boolean; however, DMSDocumentCart::addItem() does only seem to accept object<DMSRequestItem>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
117
            }
118
        }
119
120
        return $this;
121
    }
122
123
    /**
124
     * Completely empties a cart
125
     *
126
     * @return DMSDocumentCart
127
     */
128
    public function emptyCart()
129
    {
130
        $this->backend->emptyCart();
131
132
        return $this;
133
    }
134
135
    /**
136
     * Checks if a cart is empty
137
     *
138
     * @return boolean
139
     */
140
    public function isCartEmpty()
141
    {
142
        $items = $this->getItems();
143
144
        return empty($items);
145
    }
146
147
    /**
148
     * Set the backURL to be a Session variable for the current Document Cart
149
     *
150
     * @param string $backURL
151
     *
152
     * @return DMSDocumentCart
153
     */
154
    public function setBackUrl($backURL)
155
    {
156
        $this->backend->setBackUrl($backURL);
157
158
        return $this;
159
    }
160
161
    /**
162
     * Returns the backURL for the current Document Cart
163
     *
164
     * @return string
165
     */
166
    public function getBackUrl()
167
    {
168
        return $this->backend->getBackUrl();
169
    }
170
171
    /**
172
     * Sets the recipients info as an array (e.g. array('Name'=>'Joe','Surname'=>'Soap'))
173
     *
174
     * @param array $receiverInfo
175
     *
176
     * @return DMSDocumentCart
177
     */
178
    public function setReceiverInfo($receiverInfo)
179
    {
180
        $this->backend->setReceiverInfo($receiverInfo);
181
182
        return $this;
183
    }
184
185
    /**
186
     * Retrieves the recipients info as an array (e.g. array('Name'=>'Joe','Surname'=>'Soap'))
187
     *
188
     * @return array
189
     */
190
    public function getReceiverInfo()
191
    {
192
        return $this->backend->getReceiverInfo();
193
    }
194
195
    /**
196
     * Returns the recipients in a Viewable format
197
     *
198
     * @return ArrayData|bool
199
     */
200
    public function getReceiverInfoNice()
201
    {
202
203
        return (is_array($this->getReceiverInfo())) ? ArrayData::create($this->getReceiverInfo()) : false;
204
    }
205
206
    /**
207
     * Gets the backend handler
208
     *
209
     * @return DMSSessionBackend
210
     */
211
    public function getBackend()
212
    {
213
        return $this->backend;
214
    }
215
216
    /**
217
     * Checks if an item exists within a cart. Returns true (if exists) or false.
218
     *
219
     * @param int $itemID
220
     *
221
     * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be DMSRequestItem|boolean?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
222
     */
223
    public function isInCart($itemID)
224
    {
225
        return $this->getItem($itemID);
226
    }
227
228
    /**
229
     * Persists a cart submission to the database
230
     *
231
     * @param Form $form
232
     *
233
     * @return int
234
     */
235
    public function saveSubmission(Form $form)
236
    {
237
        $submission = DMSDocumentCartSubmission::create();
238
        $form->saveInto($submission);
239
        $return = $submission->write();
240
        $this->getItemsAsArrayList()->each(function ($row) use ($submission) {
241
            $values = array(
242
                'Quantity' => $row->getQuantity(),
243
                'DocumentID' => $row->getDocument()->ID,
244
            );
245
            $submissionItem = DMSDocumentCartSubmissionItem::create($values);
246
            $submission->Items()->add($submissionItem);
0 ignored issues
show
Documentation Bug introduced by
The method Items does not exist on object<DMSDocumentCartSubmission>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
247
        });
248
249
        return $return;
250
    }
251
}
252