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

DMSDocumentCartExtension::HasCartExtension()   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
        // Running total of print requests on this document
21
        'PrintRequestCount' => 'Int',
22
    );
23
24
    /**
25
     * Returns if a Document is permitted to reflect in a cart
26
     *
27
     * @return boolean
28
     */
29
    public function isAllowedInCart()
30
    {
31
        return $this->owner->AllowedInCart;
32
    }
33
34
    public function updateCMSFields(FieldList $fields)
35
    {
36
        $fields->insertBefore(CheckboxField::create(
37
            'AllowedInCart',
38
            _t('DMSDocument.ALLOWED_IN_CART', 'Allowed in document cart')
39
        ), 'Description');
0 ignored issues
show
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...
40
    }
41
42
    /**
43
     * Increments the number of times a document was printed
44
     *
45
     * @return DMSDocument
46
     */
47
    public function incrementPrintRequest()
48
    {
49
        $this->owner->PrintRequestCount++;
50
        $this->owner->write();
51
52
        return $this->owner;
53
    }
54
55
    /**
56
     * Checks if a given document already exists within the Cart. True if it does, false otherwise
57
     *
58
     * @return bool
59
     */
60
    public function isInCart()
61
    {
62
        return $this->getCart()->isInCart($this->owner->ID) ? true : false;
63
    }
64
65
    /**
66
     * Checks to see if this extension (DMSDocumentCartExtension) has been loaded.
67
     *
68
     * @return bool
69
     */
70
    public function HasCartExtension()
71
    {
72
        return $this->owner->hasExtension('DMSDocumentCartExtension');
73
    }
74
75
    /**
76
     * Builds and returns a valid DMSDocumentController URL from the given $action link
77
     *
78
     * @param string $action Can be either 'add', 'remove' or 'checkout
79
     *
80
     * @return string
81
     */
82
    public function getActionLink($action = 'add')
83
    {
84
        $action = strtolower($action);
85
        $allowedActions = array_merge($this->getCartController()->allowedActions(), array('checkout'));
86
        if (!in_array($action, $allowedActions)) {
87
            throw new InvalidArgumentException("{$action} is not accepted for this method.");
88
        }
89
90
        if ($action !== 'checkout') {
91
            $result = Controller::join_links('documentcart', $action, $this->owner->ID);
92
        } else {
93
            $result = SiteTree::get_one('DMSDocumentCartCheckoutPage')->Link();
94
        }
95
96
        return $result;
97
    }
98
99
    /**
100
     * Retrieves a DMSDocumentCartController handle
101
     *
102
     * @return DMSDocumentCartController
103
     */
104
    public function getCartController()
105
    {
106
        if (!$this->cartController) {
107
            $this->cartController = DMSDocumentCartController::create();
108
        }
109
110
        return $this->cartController;
111
    }
112
113
    /**
114
     * Retrieves a DMSDocumentCart handle
115
     *
116
     * @return DMSDocumentCart
117
     */
118
    public function getCart()
119
    {
120
        return $this->getCartController()->getCart();
121
    }
122
}
123