Completed
Pull Request — master (#33)
by Franco
02:00
created

DMSDocumentCart::getSummary()   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
    /**
16
     * Variable to control whether a cart is being updated or not
17
     *
18
     * @var bool
19
     */
20
    private $viewOnly = false;
21
22
    /**
23
     * Instantiate a cart backend either by that provided, or a session default
24
     *
25
     * @param DMSCartBackendInterface $backend
0 ignored issues
show
Documentation introduced by
Should the type for parameter $backend not be DMSCartBackendInterface|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
26
     * @throws DMSDocumentCartException If a backend was provided but doesn't implement the backend interface
27
     */
28
    public function __construct($backend = null)
29
    {
30
        parent::__construct();
31
        if ($backend && !($backend instanceof DMSCartBackendInterface)) {
32
            throw new DMSDocumentCartException('Backend must implement DMSCartBackendInterface!');
33
        }
34
        $this->backend = ($backend) ?: DMSSessionBackend::singleton();
35
    }
36
37
    /**
38
     * Returns all the cart items as an array
39
     *
40
     * @return ArrayList
41
     */
42
    public function getItems()
43
    {
44
        return ArrayList::create($this->backend->getItems());
45
    }
46
47
    /**
48
     * Add an {@link DMSRequestItem} object into the cart.
49
     *
50
     * @param DMSRequestItem $item
51
     *
52
     * @return DMSDocumentCart
53
     */
54
    public function addItem(DMSRequestItem $item)
55
    {
56
        $this->backend->addItem($item);
57
58
        return $this;
59
    }
60
61
    /**
62
     * Get a {@link DMSRequestItem} object from the cart.
63
     *
64
     * @param int $itemID The ID of the item
65
     *
66
     * @return DMSRequestItem|boolean
67
     */
68
    public function getItem($itemID)
69
    {
70
        return $this->backend->getItem($itemID);
71
    }
72
73
    /**
74
     * Removes a {@link DMSRequestItem} from the cart by it's id
75
     *
76
     * @param DMSRequestItem $item
77
     *
78
     * @return DMSDocumentCart
79
     */
80
    public function removeItem(DMSRequestItem $item)
81
    {
82
        $this->backend->removeItem($item);
83
84
        return $this;
85
    }
86
87
    /**
88
     * Removes a {@link DMSRequestItem} from the cart by it's id
89
     *
90
     * @param int $itemID
91
     *
92
     * @return DMSDocumentCart
93
     */
94
    public function removeItemByID($itemID)
95
    {
96
        $this->backend->removeItemByID($itemID);
97
98
        return $this;
99
    }
100
101
    /**
102
     * Adjusts (increments, decrements or amends) the quantity of an {@link DMSRequestItem}.'
103
     * A positive $quantity increments the total, whereas a negative value decrements the total. A cart item
104
     * is removed completely if it's value reaches <= 0.
105
     *
106
     * @param int $itemID
107
     * @param int $quantity
108
     *
109
     * @return DMSDocumentCart
110
     */
111
    public function updateItemQuantity($itemID, $quantity)
112
    {
113
        if ($item = $this->getItem($itemID)) {
114
            $currentQuantity = $item->getQuantity();
115
            $newQuantity = $currentQuantity + $quantity;
116
            if ($newQuantity <= 0) {
117
                $this->removeItemByID($itemID);
118
            } else {
119
                $item->setQuantity($newQuantity);
120
                $this->addItem($item);
0 ignored issues
show
Bug introduced by
It seems like $item defined by $this->getItem($itemID) on line 113 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...
121
            }
122
        }
123
124
        return $this;
125
    }
126
127
    /**
128
     * Completely empties a cart
129
     *
130
     * @return DMSDocumentCart
131
     */
132
    public function emptyCart()
133
    {
134
        $this->backend->emptyCart();
135
136
        return $this;
137
    }
138
139
    /**
140
     * Checks if a cart is empty.
141
     * Returns true if cart is empty, false otherwise.
142
     *
143
     * @return boolean
144
     */
145
    public function isCartEmpty()
146
    {
147
        $items = $this->getItems();
148
149
        return !$items->exists();
150
    }
151
152
    /**
153
     * Set the backURL to be a Session variable for the current Document Cart
154
     *
155
     * @param string $backURL
156
     *
157
     * @return DMSDocumentCart
158
     */
159
    public function setBackUrl($backURL)
160
    {
161
        $this->backend->setBackUrl($backURL);
162
163
        return $this;
164
    }
165
166
    /**
167
     * Returns the backURL for the current Document Cart
168
     *
169
     * @return string
170
     */
171
    public function getBackUrl()
172
    {
173
        return $this->backend->getBackUrl();
174
    }
175
176
    /**
177
     * Sets the recipients info as an array (e.g. array('Name'=>'Joe','Surname'=>'Soap'))
178
     *
179
     * @param array $receiverInfo
180
     *
181
     * @return DMSDocumentCart
182
     */
183
    public function setReceiverInfo($receiverInfo)
184
    {
185
        $this->backend->setReceiverInfo($receiverInfo);
186
187
        return $this;
188
    }
189
190
    /**
191
     * Retrieves the recipients info as an array (e.g. array('Name'=>'Joe','Surname'=>'Soap'))
192
     *
193
     * @return array
194
     */
195
    public function getReceiverInfo()
196
    {
197
        return $this->backend->getReceiverInfo();
198
    }
199
200
    /**
201
     * Returns the recipients in a Viewable format
202
     *
203
     * @return ArrayData|bool
204
     */
205
    public function getReceiverInfoNice()
206
    {
207
        return (is_array($this->getReceiverInfo())) ? ArrayData::create($this->getReceiverInfo()) : false;
208
    }
209
210
    /**
211
     * Gets the backend handler
212
     *
213
     * @return DMSSessionBackend
214
     */
215
    public function getBackend()
216
    {
217
        return $this->backend;
218
    }
219
220
    /**
221
     * Checks if an item exists within a cart. Returns true (if exists) or false.
222
     *
223
     * @param int $itemID
224
     *
225
     * @return bool
226
     */
227
    public function isInCart($itemID)
228
    {
229
        return (bool) $this->getItem($itemID);
230
    }
231
232
    /**
233
     * Persists a cart submission to the database
234
     *
235
     * @param Form $form
236
     *
237
     * @return int
238
     */
239
    public function saveSubmission(Form $form)
240
    {
241
        $submission = DMSDocumentCartSubmission::create();
242
        $form->saveInto($submission);
243
        $return = $submission->write();
244
        $this->getItems()->each(function ($row) use ($submission) {
245
            $values = array(
246
                'Quantity' => $row->getQuantity(),
247
                'DocumentID' => $row->getDocument()->ID,
248
            );
249
            $submissionItem = DMSDocumentCartSubmissionItem::create($values);
250
            $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...
251
252
            $row->getDocument()->incrementPrintRequest();
253
        });
254
255
        return $return;
256
    }
257
258
    /**
259
     * Returns true if the cart is being updated. False otherwise
260
     * @return bool
261
     */
262
    public function isViewOnly()
263
    {
264
        return $this->viewOnly;
265
    }
266
267
    /**
268
     * Sets the updating flag
269
     *
270
     * @param bool $updating
271
     * @return DMSDocumentCart
272
     */
273
    public function setViewOnly($updating)
274
    {
275
        $this->viewOnly = (bool) $updating;
276
        return $this;
277
    }
278
279
    /**
280
     * Displays a view-only table of the cart items.
281
     *
282
     * @return HTMLText
283
     */
284
    public function getSummary()
285
    {
286
        return $this->renderWith('DMSDocumentCartSummary');
287
    }
288
289
    /**
290
     * Utility method to link to the current controllers action
291
     *
292
     * @param string $action
293
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

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...
294
     */
295
    public function getLink($action)
296
    {
297 View Code Duplication
        if ($url = array_search(
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...
298
            'DMSDocumentCartController',
299
            (array)Config::inst()->get('Director', 'rules')
300
        )
301
        ) {
302
            return Controller::join_links($url, $action);
303
        }
304
    }
305
}
306