Completed
Push — master ( 39d622...e71f1d )
by Nicolaas
01:48
created

OrderStepFeedback::initStep()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
/**
4
 *
5
 * 1 July bough
6
 * +10 days start sending
7
 * +20 days stop sending
8
 * SO
9
 * on 11 July
10
 * 1 July + 10 < Now
11
 * 1 July + 20 > Now
12
 *
13
 */
14
15
16
/**
17
 *
18
 * 1 July bought
19
 * +10 days start sending
20
 * +20 days stop sending
21
 * SO
22
 * on 11 July
23
 * 1 July + 10 < Now
24
 * 1 July + 20 > Now
25
 *
26
 */
27
28
29
class OrderStepFeedback extends OrderStep
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...
30
{
31
    private static $verbose = false;
0 ignored issues
show
Unused Code introduced by
The property $verbose 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...
32
33
    /**
34
     * @var String
35
     */
36
    protected $emailClassName = "OrderStepFeedback_Email";
37
38
    private static $db = 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 $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...
39
        'SendFeedbackEmail' => 'Boolean',
40
        'MinDays' => 'Int',
41
        'MaxDays' => 'Int',
42
        'MessageAfterProductsList' => 'HTMLText',
43
        'LinkText' => 'Varchar'
44
    );
45
46
    private static $defaults = 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 $defaults 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...
47
        'CustomerCanEdit' => 0,
48
        'CustomerCanCancel' => 0,
49
        'CustomerCanPay' => 0,
50
        'Name' => 'Get feedback',
51
        'Code' => 'FEEDBACK',
52
        "ShowAsInProcessOrder" => true,
53
        "HideStepFromCustomer" => true,
54
        'SendFeedbackEmail' => true,
55
        'MinDays' => 10,
56
        'MaxDays' => 20
57
    );
58
59
60
    public function getCMSFields()
61
    {
62
        $fields = parent::getCMSFields();
63
        $fields->addFieldsToTab(
64
            'Root.CustomerMessage',
65
            array(
66
                CheckboxField::create('SendFeedbackEmail', 'Send feedback email to customer?'),
67
                $minDaysField = NumericField::create('MinDays', "<strong>Min Days</strong> before sending"),
68
                $maxDaysField = NumericField::create('MaxDays', "<strong>Max Days</strong> before sending")
69
            ),
70
            "EmailSubject"
71
        );
72
        $minDaysField->setRightTitle('What is the <strong>mininum number of days to wait after completing an order</strong> before this email should be sent?');
73
        $maxDaysField->setRightTitle('
74
            What is the <strong>maxinum number of days to wait after completing an order</strong> before this email should be sent?<br>
75
            <strong>If set to zero, this step will be ignored.</strong>'
76
        );
77
        $fields->addFieldsToTab(
78
            'Root.CustomerMessage',
79
            array(
80
                HTMLEditorField::create(
81
                    'MessageAfterProductsList',
82
                    _t('OrderStepFeedback.MESSAGEAFTERPRODUCTSLIST', 'Message After Products List')
83
                )->setRightTitle(
84
                    'Optional message displayed after the list of products'
85
                )->setRows(3),
86
                TextField::create(
87
                    'LinkText',
88
                    _t('OrderStepFeedback.BUTTONTEXT', 'Link Text')
89
                )->setRightTitle('This is the text displayed on the "order again" link/button')
90
            )
91
        );
92
        return $fields;
93
    }
94
95
    public function initStep(Order $order)
96
    {
97
        if ($this->SendFeedbackEmail) {
0 ignored issues
show
Documentation introduced by
The property SendFeedbackEmail does not exist on object<OrderStepFeedback>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
98
            Config::inst()->update("OrderStep", "number_of_days_to_send_update_email", $this->MaxDays);
0 ignored issues
show
Documentation introduced by
The property MaxDays does not exist on object<OrderStepFeedback>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
99
        }
100
        return true;
101
    }
102
103
    public function doStep(Order $order)
104
    {
105
        //ignore altogether?
106
        if ($this->SendFeedbackEmail) {
0 ignored issues
show
Documentation introduced by
The property SendFeedbackEmail does not exist on object<OrderStepFeedback>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
107
            // too late to send
108
            if ($this->isExpiredFeedbackStep($order)) {
109
                if ($this->Config()->get("verbose")) {
110
                    DB::alteration_message(" - Time to send feedback is expired");
111
                }
112
                return true;
113
            }
114
            //is now the right time to send?
115
            elseif ($this->isReadyToGo($order)) {
116
                $subject = $this->EmailSubject;
0 ignored issues
show
Documentation introduced by
The property EmailSubject does not exist on object<OrderStepFeedback>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
117
                $message = $this->CustomerMessage;
0 ignored issues
show
Documentation introduced by
The property CustomerMessage does not exist on object<OrderStepFeedback>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
118
                if ($this->hasBeenSent($order, false)) {
119
                    if ($this->Config()->get("verbose")) {
120
                        DB::alteration_message(" - already sent!");
121
                    }
122
                    return true; //do nothing
123
                } else {
124
                    if ($this->Config()->get("verbose")) {
125
                        DB::alteration_message(" - Sending it now!");
126
                    }
127
                    return $order->sendEmail(
128
                        $this->getEmailClassName(),
129
                        $subject,
130
                        $message,
131
                        $resend = false,
132
                        $adminOnlyOrToEmail = false
0 ignored issues
show
Documentation introduced by
$adminOnlyOrToEmail = false is of type boolean, 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...
133
                    );
134
                }
135
            }
136
            //wait until later....
137
            else {
138
                if ($this->Config()->get("verbose")) {
139
                    DB::alteration_message(" - We need to wait until minimum number of days.");
140
                }
141
                return false;
142
            }
143
        } else {
144
            return true;
145
        }
146
    }
147
148
    /**
149
     * can continue if emails has been sent or if there is no need to send a receipt.
150
     * @param DataObject $order Order
151
     * @return DataObject | Null - DataObject = next OrderStep
0 ignored issues
show
Documentation introduced by
Should the return type not be OrderStep|null?

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...
152
     **/
153
    public function nextStep(Order $order)
154
    {
155
        if (
156
            ! $this->SendFeedbackEmail ||
0 ignored issues
show
Documentation introduced by
The property SendFeedbackEmail does not exist on object<OrderStepFeedback>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
157
             $this->hasBeenSent($order, false) ||
158
             $this->isExpiredFeedbackStep($order)
159
        ) {
160
            if ($this->Config()->get("verbose")) {
161
                DB::alteration_message(" - Moving to next step");
162
            }
163
            return parent::nextStep($order);
164
        }
165
        if ($this->Config()->get("verbose")) {
166
            DB::alteration_message(" - no next step: has not been sent");
167
        }
168
        return null;
169
    }
170
171
    /**
172
     * For some ordersteps this returns true...
173
     * @return Boolean
174
     **/
175
    protected function hasCustomerMessage()
176
    {
177
        return true;
178
    }
179
180
    /**
181
     * Explains the current order step.
182
     * @return String
183
     */
184
    protected function myDescription()
185
    {
186
        return "The customer is sent a feedback request email.";
187
    }
188
189
    /**
190
     * returns true if the Minimum number of days is met....
191
     * @param Order
192
     * @return Boolean
193
     */
194
    protected function isReadyToGo(Order $order)
195
    {
196
        if ($this->MinDays) {
0 ignored issues
show
Documentation introduced by
The property MinDays does not exist on object<OrderStepFeedback>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
197
            $log = $order->SubmissionLog();
198
            if ($log) {
199
                $createdTS = strtotime($log->Created);
200
                $nowTS = strtotime('now');
201
                $startSendingTS = strtotime("+{$this->MinDays} days", $createdTS);
0 ignored issues
show
Documentation introduced by
The property MinDays does not exist on object<OrderStepFeedback>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
202
                //current TS = 10
203
                //order TS = 8
204
                //add 4 days: 12
205
                //thus if 12 <= now then go for it (start point in time has passed)
206
                if ($this->Config()->get("verbose")) {
207
                    DB::alteration_message("Time comparison: Start Sending TS: ".$startSendingTS." current TS: ".$nowTS.". If SSTS > NowTS then Go for it.");
208
                }
209
                return ($startSendingTS <= $nowTS) ? true : false;
210
            } else {
211
                user_error("can not find order log for ".$order->ID);
212
                return false;
213
            }
214
        } else {
215
            //send immediately
216
            return true;
217
        }
218
    }
219
220
    /**
221
     * returns true if it is too late to send the feedback step
222
     * @param Order
223
     * @return Boolean
224
     */
225
    protected function isExpiredFeedbackStep(Order $order)
226
    {
227
        if ($this->MaxDays) {
0 ignored issues
show
Documentation introduced by
The property MaxDays does not exist on object<OrderStepFeedback>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
228
            $log = $order->SubmissionLog();
229
            if ($log) {
230
                $createdTS = strtotime($log->Created);
231
                $nowTS = strtotime('now');
232
                $stopSendingTS = strtotime("+{$this->MaxDays} days", $createdTS);
0 ignored issues
show
Documentation introduced by
The property MaxDays does not exist on object<OrderStepFeedback>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
233
                return ($stopSendingTS < $nowTS) ? true : false;
234
            } else {
235
                user_error("can not find order log for ".$order->ID);
236
                return false;
237
            }
238
        } else {
239
            return true;
240
        }
241
    }
242
243
    public function hasBeenSent(Order $order, $checkDateOfOrder = true)
244
    {
245
        return OrderEmailRecord::get()->filter(
246
            array(
247
                "OrderEmailRecord.OrderID" => $order->ID,
248
                "OrderEmailRecord.OrderStepID" => $this->ID,
249
                "OrderEmailRecord.Result" => 1
250
            )
251
        )->count() ? true : false;
252
    }
253
}
254