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

DMSDocumentCartExtension   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 8
dl 0
loc 107
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A isAllowedInCart() 0 4 1
A updateCMSFields() 0 10 1
A incrementPrintRequest() 0 7 1
A isInCart() 0 4 1
A getActionLink() 0 16 3
A getCartController() 0 8 2
A getCart() 0 4 1
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(
37
            'Description',
38
            CheckboxField::create(
39
                'AllowedInCart',
40
                _t('DMSDocumentCart.ALLOWED_IN_CART', 'Allowed in document cart')
41
            )
42
        );
43
    }
44
45
    /**
46
     * Increments the number of times a document was printed
47
     *
48
     * @return DMSDocument
49
     */
50
    public function incrementPrintRequest()
51
    {
52
        $this->owner->PrintRequestCount++;
53
        $this->owner->write();
54
55
        return $this->owner;
56
    }
57
58
    /**
59
     * Checks if a given document already exists within the Cart. True if it does, false otherwise
60
     *
61
     * @return bool
62
     */
63
    public function isInCart()
64
    {
65
        return (bool) $this->getCart()->isInCart($this->owner->ID);
66
    }
67
68
    /**
69
     * Builds and returns a valid DMSDocumentController URL from the given $action link
70
     *
71
     * @param string $action Can be either 'add', 'remove' or 'checkout
72
     *
73
     * @return string
74
     *
75
     * @throws InvalidArgumentException if the provided $action is not allowed.
76
     */
77
    public function getActionLink($action = 'add')
78
    {
79
        $action = strtolower($action);
80
        $allowedActions = array_merge($this->getCartController()->allowedActions(), array('checkout'));
81
        if (!in_array($action, $allowedActions)) {
82
            throw new InvalidArgumentException("{$action} is not accepted for this method.");
83
        }
84
85
        if ($action !== 'checkout') {
86
            $result = Controller::join_links('documentcart', $action, $this->owner->ID);
87
        } else {
88
            $result = SiteTree::get_one('DMSDocumentCartCheckoutPage')->Link();
89
        }
90
91
        return $result;
92
    }
93
94
    /**
95
     * Retrieves a DMSDocumentCartController handle
96
     *
97
     * @return DMSDocumentCartController
98
     */
99
    public function getCartController()
100
    {
101
        if (!$this->cartController) {
102
            $this->cartController = DMSDocumentCartController::create();
103
        }
104
105
        return $this->cartController;
106
    }
107
108
    /**
109
     * Retrieves a DMSDocumentCart handle
110
     *
111
     * @return DMSDocumentCart
112
     */
113
    public function getCart()
114
    {
115
        return $this->getCartController()->getCart();
116
    }
117
}
118