Completed
Push — master ( 9036df...a5c536 )
by Nicolaas
03:28
created

tryToFinaliseOrders()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
eloc 6
nc 2
nop 1
1
<?php
2
3
4
/**
5
 * @description:
6
 *
7
 *
8
 * @authors: Nicolaas [at] Sunny Side Up .co.nz
9
 * @package: ecommerce
10
 * @sub-package: tasks
11
 * @inspiration: Silverstripe Ltd, Jeremy
12
 **/
13
class EcommerceTaskProcessOrderQueue extends BuildTask
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...
14
{
15
    protected $sendEmails = true;
16
17
    protected $limit = 1;
18
19
    protected $title = 'Process The Order Queue';
20
21
    protected $description = 'Go through order queue and try to finalise all the orders in it.';
22
23
    /**
24
     *@return int - number of carts destroyed
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|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...
25
     **/
26
    public function run($request)
27
    {
28
        //as this may run every minute, we have to limit it to fifty seconds.
29
        set_time_limit(50);
30
        $now = microtime(true);
31
        //IMPORTANT!
32
        if ( ! $this->sendEmails) {
33
            Config::inst()->update('Email', 'send_all_emails_to', 'no-one@localhost');
34
            Email::set_mailer(new Ecommerce_Dummy_Mailer());
0 ignored issues
show
Deprecated Code introduced by
The method Email::set_mailer() has been deprecated with message: since version 4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
35
        }
36
        $id = intval($request->getVar('id')) - 0;
37
        $queueObjectSingleton = Injector::inst()->get('OrderProcessQueue');
38
        $ordersinQueue = $queueObjectSingleton->OrdersToBeProcessed($id);
39
        if($ordersinQueue->count() == 0) {
40
            echo 'No orders in queue';
41
            return;
42
        }
43
        echo '<h3>There are '.$ordersinQueue->count().' in the queue, processing '.$this->limit.' now</h3>';
44
        if($id) {
45
            echo '<h3>FORCING Order with ID</h3>';
46
            $ordersinQueue = $ordersinQueue->filter(array('ID' => $id));
47
        }
48
        $this->tryToFinaliseOrders($ordersinQueue);
49
        echo '<hr />';
50
        echo '<hr />';
51
        echo 'PROCECESSED IN: '.round(((microtime(true) - $now) / 1), 5).' seconds';
52
    }
53
54
55
    protected function tryToFinaliseOrders($orders)
56
    {
57
        //limit orders
58
        $orders = $orders->limit($this->limit);
59
        $queueObjectSingleton = Injector::inst()->get('OrderProcessQueue');
60
        foreach ($orders as $order) {
61
            echo '<hr />Processing order: '.$order->ID;
62
            $queueObjectSingleton->process($order);
63
        }
64
    }
65
}
66