Completed
Push — master ( 10a6ac...a2353b )
by Nicolaas
03:46
created

OrderProcessQueue::getHasBeenInQueueForSince()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This class provides a bunch of Meta Objects
4
 * that do not interact with the object at hand, but rather with the datalist as a whole.
5
 *
6
 */
7
8
class OrderProcessQueue extends DataObject
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...
9
{
10
    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...
11
        'DeferTimeInSeconds' => 'Int',
12
        'InProcess' => 'Boolean',
13
        'ProcessAttempts' => 'Int'
14
    );
15
16
    private static $has_one = 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 $has_one 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...
17
        'Order' => 'Order',
18
        'OrderStep' => 'OrderStep'
19
    );
20
21
    private static $indexes = 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 $indexes 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...
22
        'Created' => true,
23
        'DeferTimeInSeconds' => true,
24
        'ProcessAttempts' => true
25
    );
26
27
    private static $casting = 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 $casting 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...
28
        'ToBeProcessedAt' => 'SS_Datetime',
29
        'HasBeenInQueueSince' => 'SS_Datetime'
30
    );
31
32
    private static $default_sort = array(
0 ignored issues
show
Unused Code introduced by
The property $default_sort 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...
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
33
        'Created' => 'DESC'
34
    );
35
36
    /**
37
     * standard SS variable.
38
     *
39
     * @var array
40
     */
41
    private static $summary_fields = 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 $summary_fields 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...
42
        'Order.Title' => 'Order',
43
        'Order.Status.Title' => 'Current Step',
44
        'ProcessAttempts' => 'Attempts',
45
        'ToBeProcessedAt.Nice' => 'To be processed at',
46
        'ToBeProcessedAt.Ago' => 'That is ...',
47
        'HasBeenInQueueForSince.Nice' => 'Added to queue ...',
48
        'InProcess.Nice' => 'Currently Running'
49
    );
50
51
    /**
52
     * standard SS variable.
53
     *
54
     * @var array
55
     */
56
    private static $searchable_fields = 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 $searchable_fields 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...
57
        'OrderID' => array(
58
            'field' => 'NumericField',
59
            'title' => 'Order Number',
60
        )
61
    );
62
63
64
    /**
65
     * Standard SS method.
66
     *
67
     * @param Member $member
0 ignored issues
show
Documentation introduced by
Should the type for parameter $member not be Member|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
68
     *
69
     * @return bool
70
     */
71
    public function canCreate($member = null)
72
    {
73
        return false;
74
    }
75
76
    /**
77
     * Standard SS method.
78
     *
79
     * @param Member $member
0 ignored issues
show
Documentation introduced by
Should the type for parameter $member not be Member|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
80
     *
81
     * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|string|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...
82
     */
83
    public function canView($member = null)
84
    {
85
        if (! $member) {
86
            $member = Member::currentUser();
87
        }
88
        $extended = $this->extendedCan(__FUNCTION__, $member);
0 ignored issues
show
Documentation introduced by
$member is of type object<DataObject>|null, but the function expects a object<Member>|integer.

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...
89
        if ($extended !== null) {
90
            return $extended;
91
        }
92
        if (Permission::checkMember($member, Config::inst()->get('EcommerceRole', 'admin_permission_code'))) {
93
            return true;
94
        }
95
        //is the member is a shop assistant they can always view it
96
        if (EcommerceRole::current_member_is_shop_assistant($member)) {
97
            return true;
98
        }
99
100
        return parent::canView($member);
0 ignored issues
show
Bug introduced by
It seems like $member defined by \Member::currentUser() on line 86 can also be of type object<DataObject>; however, DataObject::canView() does only seem to accept object<Member>|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
101
    }
102
103
    /**
104
     * Standard SS method.
105
     *
106
     * @param Member $member
0 ignored issues
show
Documentation introduced by
Should the type for parameter $member not be Member|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
107
     *
108
     * @return bool
109
     */
110
    public function canEdit($member = null)
111
    {
112
        return false;
113
    }
114
115
    /**
116
     * Standard SS method
117
     * Queues can be deleted if needed.
118
     *
119
     * @param Member $member
0 ignored issues
show
Documentation introduced by
Should the type for parameter $member not be Member|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
120
     *
121
     * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|string|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...
122
     */
123
    public function canDelete($member = null)
124
    {
125
        return parent::canDelete($member);
126
    }
127
128
    /**
129
     * standard SS variable.
130
     *
131
     * @var string
132
     */
133
    private static $singular_name = 'Order To Be Processed';
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 $singular_name 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...
134
    public function i18n_singular_name()
135
    {
136
        return _t('OrderProcessQueue.SINGULAR_NAME', 'Order In Queue');
137
    }
138
139
    /**
140
     * standard SS variable.
141
     *
142
     * @var string
143
     */
144
    private static $plural_name = 'Orders to be Processed';
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 $plural_name 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...
145
    public function i18n_plural_name()
146
    {
147
        return _t('OrderProcessQueue.PLURAL_NAME', 'Orders In Queue');
148
    }
149
150
151
    /**
152
     * META METHOD: Add an order to the job list if it does not exist already.
153
     *
154
     * @param Order $order
155
     * @param Int   $deferInSeconds
0 ignored issues
show
Bug introduced by
There is no parameter named $deferInSeconds. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
156
     */
157
    public function AddOrderToQueue($order, $deferTimeInSeconds)
158
    {
159
        if(!$order || ! $order->ID) {
160
            user_error('No real order provided.');
161
        }
162
        $filter = array(
163
            'OrderID' => $order->ID,
164
            'OrderStepID' => $order->StatusID
0 ignored issues
show
Documentation introduced by
The property StatusID does not exist on object<Order>. 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...
165
        );
166
        $existingEntry = OrderProcessQueue::get()->filter($filter)->first();
167
        $filter['DeferTimeInSeconds'] = $deferTimeInSeconds;
168
        if (! $existingEntry) {
169
            $existingEntry = OrderProcessQueue::create($filter);
170
        } else {
171
            foreach ($filter as $field => $value) {
172
                $existingEntry->$field = $value;
173
            }
174
        }
175
        $existingEntry->write();
176
177
        return $existingEntry;
178
    }
179
180
    /**
181
     * META METHOD
182
     * processes the order ...
183
     * returns TRUE if SUCCESSFUL and a message if unsuccessful ...
184
     *
185
     *
186
     * @param  Order $order optional
0 ignored issues
show
Documentation introduced by
Should the type for parameter $order not be Order|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
187
     * @return boolean | string
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|string?

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...
188
     */
189
    public function process($order = null)
190
    {
191
        //find variables
192
        if( ! $order) {
193
            $order = $this->Order();
0 ignored issues
show
Documentation Bug introduced by
The method Order does not exist on object<OrderProcessQueue>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
194
            $myQueueObject = $this;
195
        } else {
196
            $myQueueObject = $this->getQueueObject($order);
197
        }
198
        //delete if order is gone ...
199
        if($order) {
200
            //if order has moved already ... delete
201
            if(
202
                $this->OrderStepID > 0
0 ignored issues
show
Documentation introduced by
The property OrderStepID does not exist on object<OrderProcessQueue>. 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...
203
                && (int)$order->StatusID !== (int)$myQueueObject->OrderStepID
204
            ) {
205
                $message = 'Order has already moved on.';
206
                $myQueueObject->delete();
207
            } else {
208
                if($myQueueObject) {
209
                    if ($myQueueObject->isReadyToGo()) {
210
                        $oldOrderStatusID = $order->StatusID;
211
                        $myQueueObject->InProcess = true;
212
                        $myQueueObject->ProcessAttempts = $myQueueObject->ProcessAttempts + 1;
213
                        $myQueueObject->write();
214
                        $order->tryToFinaliseOrder(
215
                            $tryAgain = false,
216
                            $fromOrderQueue = true
217
                        );
218
                        $newOrderStatusID = $order->StatusID;
219
                        if($oldOrderStatusID != $newOrderStatusID) {
220
                            $myQueueObject->delete();
221
                            return true;
222
                        } else {
223
                            $message = 'Attempt to move order was not successful.';
224
                            $myQueueObject->InProcess = false;
225
                            $myQueueObject->write();
226
                        }
227
                    } else  {
228
                        $message = 'Minimum order queue time has not been passed.';
229
                    }
230
231
                } else {
232
                    $message = 'Could not find queue object.';
233
                }
234
            }
235
        } else {
236
            $message = 'Can not find order.';
237
            $myQueueObject->delete();
238
        }
239
        return $message;
240
    }
241
242
    /**
243
     * META METHOD: returns the queue object if it exists
244
     *
245
     * @param  Order $order
246
     *
247
     * @return null |   OrderProcessQueue
0 ignored issues
show
Documentation introduced by
Should the return type not be DataObject|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...
248
     */
249
    public function getQueueObject($order)
250
    {
251
        $filter = array('OrderID' => $order->ID);
252
253
        return OrderProcessQueue::get()->filter($filter)->first();
254
    }
255
256
    /**
257
     * META METHOD: Once you are done, you can remove the item like this ...
258
     *
259
     * @param  Order $order
260
     */
261
    public function removeOrderFromQueue($order)
262
    {
263
        $queueEntries = OrderProcessQueue::get()->filter(array('OrderID' => $order->ID));
264
        foreach($queueEntries as $queueEntry) {
265
            $queueEntry->delete();
266
        }
267
    }
268
269
    /**
270
     * META METHOD: returns a list of orders to be processed
271
     * @param int $id force this Order to be processed
272
     * @param int $limit total number of orders that can be retrieved at any one time
273
     *
274
     * @return DataList (of orders)
275
     */
276
    public function OrdersToBeProcessed($id = 0, $limit = 9999)
277
    {
278
279
        //we sort the order randomly so that we get a nice mixture
280
        //not always the same ones holding up the process
281
        $sql = '
282
            SELECT "OrderID"
283
            FROM "OrderProcessQueue"
284
            WHERE
285
                "InProcess" = 0
286
                AND
287
                (UNIX_TIMESTAMP("Created") + "DeferTimeInSeconds") < UNIX_TIMESTAMP()
288
            ORDER BY '.$this->sortPhrase().'
289
            LIMIT '.$limit.';
290
        ';
291
        $rows = DB::query($sql);
292
        $orderIDs = array($id => $id);
293
        foreach ($rows as $row) {
294
            $orderIDs[$row['OrderID']] = $row['OrderID'];
295
        }
296
297
        return Order::get()
298
            ->filter(array('ID' => $orderIDs))
299
            ->sort($this->sortPhraseForOrderIDs($orderIDs));
0 ignored issues
show
Documentation Bug introduced by
The method sortPhraseForOrderIDs does not exist on object<OrderProcessQueue>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
300
    }
301
302
    /**
303
     * META METHOD: all orders with a queue object
304
     * @param int $id force this Order to be processed
0 ignored issues
show
Bug introduced by
There is no parameter named $id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
305
     * @param int $limit total number of orders that can be retrieved at any one time
306
     *
307
     * @return DataList (of orders)
308
     */
309
    public function AllOrdersInQueue($limit = 9999)
310
    {
311
        $orderIDs = OrderProcessQueue::get()->column('OrderID');
312
        
313
        return Order::get()
314
            ->filter(array('ID' => $orderIDs))
315
            ->sort($this->sortPhraseForOrderIDs($orderIDs))
0 ignored issues
show
Documentation Bug introduced by
The method sortPhraseForOrderIDs does not exist on object<OrderProcessQueue>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
316
            ->limit($limit);
317
    }
318
319
    /**
320
     * META METHOD: returns a list of orders NOT YET to be processed
321
     * @param int $limit total number of orders that can be retrieved at any one time
322
     *
323
     * @return DataList (of orders)
324
     */
325
    public function OrdersInQueueThatAreNotReady($limit = 9999)
326
    {
327
328
        //we sort the order randomly so that we get a nice mixture
329
        //not always the same ones holding up the process
330
        $sql = '
331
            SELECT "OrderID"
332
            FROM "OrderProcessQueue"
333
            WHERE
334
                (UNIX_TIMESTAMP("Created") + "DeferTimeInSeconds") >= UNIX_TIMESTAMP()
335
            ORDER BY '.$this->sortPhrase().'
336
            LIMIT '.$limit.';
337
        ';
338
        $rows = DB::query($sql);
339
        $orderIDs = array(0 => 0);
340
        foreach ($rows as $row) {
341
            $orderIDs[$row['OrderID']] = $row['OrderID'];
342
        }
343
344
        return Order::get()
345
            ->filter(array('ID' => $orderIDs))
346
            ->sort($this->sortPhraseForOrderIDs($orderIDs));
0 ignored issues
show
Documentation Bug introduced by
The method sortPhraseForOrderIDs does not exist on object<OrderProcessQueue>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
347
    }
348
349
    /**
350
     * non-database method of working out if an Order is ready to go.
351
     *
352
     * @return bool
353
     */
354
    public function isReadyToGo()
355
    {
356
        return (strtotime($this->Created) + $this->DeferTimeInSeconds) < time();
0 ignored issues
show
Documentation introduced by
The property DeferTimeInSeconds does not exist on object<OrderProcessQueue>. 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...
357
    }
358
359
    /**
360
     *
361
     * casted variable
362
     * @return SS_DateTime
363
     */
364
    public function ToBeProcessedAt()
365
    {
366
        return $this->getToBeProcessedAt();
367
    }
368
369
    /**
370
     *
371
     * casted variable
372
     * @return SS_DateTime
373
     */
374
    public function getToBeProcessedAt()
375
    {
376
        return DBField::create_field('SS_Datetime', (strtotime($this->Created) + $this->DeferTimeInSeconds));
0 ignored issues
show
Documentation introduced by
The property DeferTimeInSeconds does not exist on object<OrderProcessQueue>. 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...
377
    }
378
379
380
    /**
381
     *
382
     * casted variable
383
     * @return SS_DateTime
384
     */
385
    public function HasBeenInQueueForSince()
386
    {
387
        return $this->getHasBeenInQueueForSince();
388
    }
389
390
    /**
391
     *
392
     * casted variable
393
     * @return SS_DateTime
394
     */
395
    public function getHasBeenInQueueForSince()
396
    {
397
        return DBField::create_field('SS_Datetime', (strtotime($this->Created)));
398
    }
399
400
401
    /**
402
     * CMS Fields
403
     * @return FieldList
404
     */
405
    public function getCMSFields()
406
    {
407
        $fields = parent::getCMSFields();
408
        if($this->exists()) {
409
            $fields->addFieldToTab(
410
                'Root.Main',
411
                ReadonlyField::create(
412
                    'HasBeenInQueueForSinceCompilations',
413
                    _t('OrderProcessQueue.SINCE', 'In the queue since'),
414
                    $this->getHasBeenInQueueForSince()->Nice() . ' - ' . $this->getHasBeenInQueueForSince()->Ago()
415
                ),
416
                'DeferTimeInSeconds'
417
            );
418
            $fields->addFieldToTab(
419
                'Root.Main',
420
                ReadonlyField::create(
421
                    'ToBeProcessedAtCompilations',
422
                    _t('OrderProcessQueue.TO_BE_PROCESSED', 'To Be Processed'),
423
                    $this->getToBeProcessedAt()->Nice() . ' - ' . $this->getToBeProcessedAt()->Ago()
424
                ),
425
                'InProcess'
426
            );
427
            $fields->addFieldToTab(
428
                'Root.Main',
429
                LiteralField::create(
430
                    'processQueueNow',
431
                    '<h2>
432
                        <a href="/dev/tasks/EcommerceTaskProcessOrderQueue/?id='.$this->OrderID.'" target="_blank">'.
0 ignored issues
show
Documentation introduced by
The property OrderID does not exist on object<OrderProcessQueue>. 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...
433
                            _t('OrderProcessQueue.PROCESS', 'Process now').
434
                        '</a>
435
                    </h2>'
436
                )
437
            );
438
            $fields->replaceField(
439
                'OrderID',
440
                CMSEditLinkField::create(
441
                    'OrderID',
442
                    'Order',
443
                    $this->Order()
0 ignored issues
show
Documentation Bug introduced by
The method Order does not exist on object<OrderProcessQueue>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
444
                )
445
            );
446
        }
447
        return $fields;
448
    }
449
450
    public function requireDefaultRecords()
451
    {
452
        parent::requireDefaultRecords();
453
        $errors = OrderProcessQueue::get()->filter(array('OrderID' => 0));
454
        foreach($errors as $error) {
455
            DB::alteration_message(' DELETING ROGUE OrderProcessQueue', 'deleted');
456
            $error->delete();
457
        }
458
    }
459
460
    protected function sortPhrase()
461
    {
462
        return '
463
            "ProcessAttempts" ASC,
464
            (UNIX_TIMESTAMP("Created") + "DeferTimeInSeconds") ASC
465
        ';
466
    }
467
468
    /**
469
     * sort phrase for orders, based in order IDs...
470
     * @param  array $orderIds
471
     * @return string
472
     */
473
    protected function sortPhraseForOrder($orderIds)
0 ignored issues
show
Unused Code introduced by
The parameter $orderIds is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
474
    {
475
        return 'FIELD("Order"."ID", '.implode(",", $orderIDs).')';
0 ignored issues
show
Bug introduced by
The variable $orderIDs does not exist. Did you mean $orderIds?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
476
    }
477
478
}
479