Completed
Pull Request — master (#19)
by Robbie
01:56
created

DMSDocumentCartExtension::isInCart()   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
/**
4
 * Class DMSDocumentCartExtension
5
 *
6
 * @property Boolean     AllowedInCart
7
 * @property Int         PrintRequestCount
8
 *
9
 * @property DMSDocument owner
10
 */
11
class DMSDocumentCartExtension extends DataExtension
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...
12
{
13
    /**
14
     * @var DMSDocumentCartController
15
     */
16
    private $cartController;
17
18
    private static $db = array(
0 ignored issues
show
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
19
        'AllowedInCart'       => 'Boolean',
20
        'MaximumCartQuantity' => 'Int',
21
        // Running total of print requests on this document
22
        'PrintRequestCount'   => 'Int',
23
    );
24
25
    /**
26
     * Returns if a Document is permitted to reflect in a cart
27
     *
28
     * @return boolean
29
     */
30
    public function isAllowedInCart()
31
    {
32
        return ($this->owner->AllowedInCart && $this->owner->canView());
33
    }
34
35
    public function updateCMSFields(FieldList $fields)
36
    {
37
        Requirements::javascript(DMS_CART_DIR . '/javascript/dmscart.js');
38
39
        $newFields = array(
40
            CheckboxField::create(
41
                'AllowedInCart',
42
                _t('DMSDocumentCart.ALLOWED_IN_CART', 'Allowed in document cart')
43
            )->addExtraClass('dms-allowed-in-cart'),
44
            TextField::create(
45
                'MaximumCartQuantity',
46
                _t('DMSDocumentCart.MAXIMUM_CART_QUANTITY', 'Maximum cart quantity')
47
            )->setRightTitle(
48
                _t(
49
                    'DMSDocumentCart.MAXIMUM_CART_QUANTITY_HELP',
50
                    'If set, this will enforce a maximum number of this item that can be ordered per cart'
51
                )
52
            )->addExtraClass('dms-maximum-cart-quantity hide')
53
        );
54
55
        foreach ($newFields as $field) {
56
            $fields->insertBefore($field, 'Description');
0 ignored issues
show
Documentation introduced by
$field is of type object<CheckboxField>|object<TextField>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
'Description' is of type string, but the function expects a object<FormField>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
57
        }
58
    }
59
60
    /**
61
     * Increments the number of times a document was printed
62
     *
63
     * @return DMSDocument
64
     */
65
    public function incrementPrintRequest()
66
    {
67
        $this->owner->PrintRequestCount++;
68
        $this->owner->write();
69
70
        return $this->owner;
71
    }
72
73
    /**
74
     * Checks if a given document already exists within the Cart. True if it does, false otherwise
75
     *
76
     * @return bool
77
     */
78
    public function isInCart()
79
    {
80
        return (bool) $this->getCart()->isInCart($this->owner->ID);
81
    }
82
83
    /**
84
     * Returns whether the current document has a limit on how many items can be added to a single cart
85
     *
86
     * @return bool
87
     */
88
    public function getHasQuantityLimit()
89
    {
90
        return $this->owner->getMaximumQuantity() > 0;
91
    }
92
93
    /**
94
     * Get the maximum quantity of this document that can be ordered in a single cart
95
     *
96
     * @return int
97
     */
98
    public function getMaximumQuantity()
99
    {
100
        return (int) $this->owner->MaximumCartQuantity;
101
    }
102
103
    /**
104
     * Builds and returns a valid DMSDocumentController URL from the given $action link
105
     *
106
     * @param string $action Can be either 'add', 'remove' or 'checkout
107
     *
108
     * @return string
109
     *
110
     * @throws InvalidArgumentException if the provided $action is not allowed.
111
     */
112
    public function getActionLink($action = 'add')
113
    {
114
        $action = strtolower($action);
115
        $allowedActions = array_merge($this->getCartController()->allowedActions(), array('checkout'));
116
        if (!in_array($action, $allowedActions)) {
117
            throw new InvalidArgumentException("{$action} is not accepted for this method.");
118
        }
119
120
        if ($action !== 'checkout') {
121
            $result = Controller::join_links('documentcart', $action, $this->owner->ID);
122
        } else {
123
            $result = SiteTree::get_one('DMSDocumentCartCheckoutPage')->Link();
124
        }
125
126
        return $result;
127
    }
128
129
    /**
130
     * Retrieves a DMSDocumentCartController handle
131
     *
132
     * @return DMSDocumentCartController
133
     */
134
    public function getCartController()
135
    {
136
        if (!$this->cartController) {
137
            $this->cartController = DMSDocumentCartController::create();
138
        }
139
140
        return $this->cartController;
141
    }
142
143
    /**
144
     * Retrieves a DMSDocumentCart handle
145
     *
146
     * @return DMSDocumentCart
147
     */
148
    public function getCart()
149
    {
150
        return $this->getCartController()->getCart();
151
    }
152
153
    /**
154
     * Returns any validation messages that may have been in the session and clears them
155
     *
156
     * @return false
0 ignored issues
show
Documentation introduced by
Should the return type not be 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...
157
     */
158
    public function getValidationResult()
159
    {
160
        if ($result = Session::get('dms-cart-validation-message')) {
161
            Session::clear('dms-cart-validation-message');
162
            return $result;
163
        }
164
        return false;
165
    }
166
}
167