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

DMSDocumentCartCheckoutPage_Controller   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 192
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 11
dl 0
loc 192
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A DMSDocumentRequestForm() 0 18 2
B send() 0 27 3
A doRequestSend() 0 11 1
A complete() 0 10 1
A trackTimestampedPrintRequest() 0 9 2
A getCart() 0 4 1
B updateCartItems() 0 13 5
A updateCartReceiverInfo() 0 5 1
1
<?php
2
3
class DMSDocumentCartCheckoutPage_Controller extends Page_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 $allowed_actions = array(
0 ignored issues
show
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...
6
        'DMSDocumentRequestForm',
7
        'complete'
8
    );
9
10
    /**
11
     * An array containing the recipients basic information
12
     *
13
     * @var array
14
     */
15
    public static $receiverInfo = array(
16
        'ReceiverName'            => '',
17
        'ReceiverPhone'           => '',
18
        'ReceiverEmail'           => '',
19
        'DeliveryAddressLine1'    => '',
20
        'DeliveryAddressLine2'    => '',
21
        'DeliveryAddressCountry'  => '',
22
        'DeliveryAddressPostCode' => '',
23
    );
24
25
    /**
26
     * Gets and displays an editable list of items within the cart, as well as a contact form with entry
27
     * fields for the recipients information.
28
     *
29
     * To extend use the following from within an Extension subclass:
30
     *
31
     * <code>
32
     * public function updateDMSDocumentRequestForm($form)
33
     * {
34
     *     // Do something here
35
     * }
36
     * </code>
37
     *
38
     * @return DMSDocumentRequestForm
39
     */
40
    public function DMSDocumentRequestForm()
41
    {
42
        $fields = DMSDocumentCartSubmission::create()->scaffoldFormFields();
43
        $fields->replaceField('DeliveryAddressLine2', TextField::create('DeliveryAddressLine2', ''));
44
        $fields->replaceField('DeliveryAddressCountry', CountryDropdownField::create(
45
            'DeliveryAddressCountry',
46
            _t('DMSDocumentCartCheckoutPage.RECEIVER_COUNTRY', 'Country')
47
        )->setValue('NZ'));
48
49
        $form = DMSDocumentRequestForm::create($this, 'DMSDocumentRequestForm', $fields, FieldList::create());
50
        if ($receiverInfo = $this->getCart()->getReceiverInfo()) {
51
            $form->loadDataFrom($receiverInfo);
52
        }
53
54
        $this->extend('updateDMSDocumentRequestForm', $form);
55
56
        return $form;
57
    }
58
59
    /**
60
     * Sends an email to both the configured recipient as well as the requester. The
61
     * configured recipient is bcc'ed to the email in order to fulfill it.
62
     *
63
     * To extend use the following from within an Extension subclass:
64
     *
65
     * <code>
66
     * public function updateSend($email)
67
     * {
68
     *     // Do something here
69
     * }
70
     * </code>
71
     * @return mixed
72
     *
73
     * @throws DMSDocumentCartException
74
     */
75
    public function send()
76
    {
77
        $member = $this->CartEmailRecipient();
78
        if ($member->exists()) {
79
            $cart = $this->getCart();
80
            $from = Config::inst()->get('Email', 'admin_email');
81
            $emailAddress = ($info = $cart->getReceiverInfo()) ? $info['ReceiverEmail'] : $from;
82
            $email = Email::create(
83
                $from,
84
                $emailAddress,
85
                _t('DMSDocumentCartCheckoutPage.EMAIL_SUBJECT', 'Request for Printed Publications')
86
            );
87
            $email->setBcc($member->Email);
88
            $renderedCart = $cart->renderWith('DocumentCart_email');
89
            $body = _t(
90
                'DMSDocumentCartCheckoutPage.EMAIL_BODY',
91
                '<p>A request for printed publications has been submitted with the following details:</p>'
92
            );
93
            $body .= $renderedCart->getValue();
94
            $email->setBody($body)->setReplyTo($emailAddress);
95
            $this->extend('updateSend', $email);
96
97
            return $email->send();
98
        } else {
99
            throw new DMSDocumentCartException('No recipient has been configured. Please do so from the CMS');
100
        }
101
    }
102
103
    /**
104
     * Handles form submission.
105
     * Totals requested are updated, delivery details added, email sent for fulfillment
106
     * and print request totals updated.
107
     *
108
     * @param array          $data
109
     * @param Form           $form
110
     * @param SS_HTTPRequest $request
111
     *
112
     * @return SS_HTTPResponse
113
     */
114
    public function doRequestSend($data, Form $form, SS_HTTPRequest $request)
115
    {
116
        $this->updateCartItems($data);
117
        $this->updateCartReceiverInfo($data);
118
        $this->send();
119
        $this->trackTimestampedPrintRequest();
120
        $this->getCart()->saveSubmission($form);
121
        $this->getCart()->emptyCart();
122
123
        return $this->redirect($this->Link('complete'));
124
    }
125
126
    /**
127
     * Displays the preconfigured thank you message to the user upon completion
128
     *
129
     * @return ViewableData_Customised
130
     */
131
    public function complete()
132
    {
133
        return $this->customise(
134
            ArrayData::create(
135
                array(
136
                    'Content'=>$this->ThanksMessage
137
                )
138
            )
139
        );
140
    }
141
142
    /**
143
     * Increments the print counts of all documents which were successfully sent.
144
     */
145
    public function trackTimestampedPrintRequest()
146
    {
147
        /** @var DMSRequestItem $item */
148
        foreach ($this->getCart()->getItems() as $item) {
149
            /** @var DMSDocument|DMSDocumentCartExtension $doc */
150
            $doc = $item->getDocument();
151
            $doc->incrementPrintRequest();
0 ignored issues
show
Bug introduced by
The method incrementPrintRequest does only exist in DMSDocumentCartExtension, but not in DMSDocument.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
152
        }
153
    }
154
155
    /**
156
     * Retrieves a {@link DMSDocumentCart} instance
157
     *
158
     * @return DMSDocumentCart
159
     */
160
    public function getCart()
161
    {
162
        return singleton('DMSDocumentCart');
163
    }
164
165
    /**
166
     * Updates the document quantities just before the request is sent.
167
     *
168
     * @param array $data
169
     */
170
    public function updateCartItems($data)
171
    {
172
        if (isset($data['ItemQuantity']) && !empty($data['ItemQuantity'])) {
173
            foreach ($data['ItemQuantity'] as $itemID => $quantity) {
174
                // Only update if quantity has changed
175
                $item = $this->getCart()->getItem($itemID);
176
                if ($item->getQuantity() == $quantity) {
177
                    continue;
178
                }
179
                $this->getCart()->updateItemQuantity($itemID, $quantity - 1);
180
            }
181
        }
182
    }
183
184
    /**
185
     * Updates the cart receiver info just before the request is sent.
186
     *
187
     * @param array $data
188
     */
189
    public function updateCartReceiverInfo($data)
190
    {
191
        $info = array_merge(self::$receiverInfo, $data);
192
        $this->getCart()->setReceiverInfo($info);
193
    }
194
}
195