Completed
Push — master ( 3bd471...8319fc )
by
unknown
32s queued 27s
created

DMSDocumentCartController   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 154
Duplicated Lines 6.49 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 10
dl 10
loc 154
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getReceiverInfo() 0 17 4
A getIsCartEmpty() 0 4 1
A items() 0 4 1
D add() 5 32 10
A deduct() 5 17 4
A remove() 0 12 2
A getCart() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
class DMSDocumentCartController extends Controller
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...
4
{
5
    private static $url_handlers = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $url_handlers 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...
6
        '$Action//$ID' => 'handleAction',
7
    );
8
9
    private static $allowed_actions = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $allowed_actions 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...
10
        'DocumentCartForm',
11
        'add',
12
        'deduct',
13
        'remove',
14
    );
15
16
    /**
17
     * See {@link DMSDocumentCart::getItems()}
18
     *
19
     * @return ArrayList
20
     */
21
    public function items()
22
    {
23
        return $this->getCart()->getItems();
24
    }
25
26
    /**
27
     * Prepares receiver info for the template.
28
     * Additionally it uses Zend_Locale to retrieve the localised spelling of the Country
29
     *
30
     * @return array
31
     */
32
    public function getReceiverInfo()
33
    {
34
        $receiverInfo = $this->getCart()->getReceiverInfo();
35
36
        if (isset($receiverInfo['DeliveryAddressCountry']) && $receiverInfo['DeliveryAddressCountry']) {
37
            $source = Zend_Locale::getTranslationList('territory', $receiverInfo['DeliveryAddressCountry'], 2);
38
            $receiverInfo['DeliveryAddressCountryLiteral'] = $source[$receiverInfo['DeliveryAddressCountry']];
39
        }
40
41
        if (!empty($receiverInfo)) {
42
            $result = $receiverInfo;
43
        } else {
44
            $result = array('Result' => 'no data');
45
        }
46
47
        return $result;
48
    }
49
50
    /**
51
     * See DMSDocumentCart::isCartEmpty()
52
     *
53
     * @return bool
54
     */
55
    public function getIsCartEmpty()
56
    {
57
        return $this->getCart()->isCartEmpty();
58
    }
59
60
    /**
61
     * Add quantity to an item that exists in {@link DMSDocumentCart}.
62
     * If the item does nt exist, try to add a new item of the particular
63
     * class given the URL parameters available.
64
     *
65
     * @param SS_HTTPRequest $request
66
     *
67
     * @return SS_HTTPResponse|string
68
     */
69
    public function add(SS_HTTPRequest $request)
70
    {
71
        $quantity = ($request->requestVar('quantity')) ? intval($request->requestVar('quantity')) : 1;
72
        $documentId = (int)$request->param('ID');
73
        if ($doc = DMSDocument::get()->byID($documentId)) {
74
            if ($doc->isAllowedInCart() && $doc->canView()) {
75
                if ($this->getCart()->getItem($documentId)) {
76
                    $this->getCart()->updateItemQuantity($documentId, $quantity);
77
                } else {
78
                    $requestItem = DMSRequestItem::create()->setDocument($doc)->setQuantity($quantity);
0 ignored issues
show
Compatibility introduced by
$doc of type object<DataObject> is not a sub-type of object<DMSDocument>. It seems like you assume a child class of the class DataObject to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
79
                    $this->getCart()->addItem($requestItem);
80
                }
81
                $backURL = $request->getVar('BackURL');
82
                // make sure that backURL is a relative path (starts with /)
83
                if (isset($backURL) && preg_match('/^\//', $backURL)) {
84
                    $this->getCart()->setBackUrl($backURL);
85
                }
86
            }
87
        }
88
89 View Code Duplication
        if ($request->isAjax()) {
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...
90
            $this->response->addHeader('Content-Type', 'application/json');
91
92
            return Convert::raw2json(array('result' => true));
93
        }
94
95
        if ($backURL = $request->getVar('BackURL')) {
96
            return $this->redirect($backURL);
97
        }
98
99
        return $this->redirectBack();
100
    }
101
102
    /**
103
     * Deduct quantity from an item that exists in {@link DMSDocumentCart}
104
     *
105
     * @param SS_HTTPRequest $request
106
     *
107
     * @return SS_HTTPResponse|string
108
     */
109
    public function deduct(SS_HTTPRequest $request)
110
    {
111
        $quantity = ($request->requestVar('quantity')) ? intval($request->requestVar('quantity')) : 1;
112
        $this->getCart()->updateItemQuantity((int)$request->param('ID'), $quantity);
113
        $this->redirectBack();
114
115 View Code Duplication
        if ($request->isAjax()) {
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...
116
            $this->response->addHeader('Content-Type', 'application/json');
117
118
            return Convert::raw2json(array('result' => true));
119
        }
120
        if ($backURL = $request->getVar('BackURL')) {
121
            return $this->redirect($backURL);
122
        }
123
124
        return $this->redirectBack();
125
    }
126
127
    /**
128
     * Completely remove an item that exists in {@link DMSDocumentCart}
129
     *
130
     * @param SS_HTTPRequest $request
131
     *
132
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|SS_HTTPResponse|null|false?

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...
133
     */
134
    public function remove(SS_HTTPRequest $request)
135
    {
136
        $this->getCart()->removeItemByID(intval($request->param('ID')));
137
138
        if ($request->isAjax()) {
139
            $this->response->addHeader('Content-Type', 'application/json');
140
141
            return Convert::raw2json(array('result' => !$this->getIsCartEmpty()));
142
        }
143
144
        return $this->redirectBack();
145
    }
146
147
    /**
148
     * Retrieves a {@link DMSDocumentCart} instance
149
     *
150
     * @return DMSDocumentCart
151
     */
152
    public function getCart()
153
    {
154
        return singleton('DMSDocumentCart');
155
    }
156
}
157