EcommerceTaskProcessOrderQueue   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 0
loc 64
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 27 4
A tryToFinaliseOrders() 0 17 3
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
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: '.$id.'</h3>';
46
            $ordersinQueue = $ordersinQueue->filter(array('ID' => $id));
47
        }
48
        $this->tryToFinaliseOrders($ordersinQueue);
49
        echo '<hr />';
50
        echo '<hr />';
51
        echo 'PROCESSED IN: '.round(((microtime(true) - $now) / 1), 5).' seconds';
52
    }
53
54
55
    /**
56
     *
57
     * @param  DataList $orders orders to be processsed.
58
     */
59
    protected function tryToFinaliseOrders($orders)
60
    {
61
        //limit orders
62
        $orders = $orders->limit($this->limit);
63
        //we sort randomly so it is less likely we get stuck with the same ones
64
        $orders = $orders->sort('RAND()');
65
        $queueObjectSingleton = Injector::inst()->get('OrderProcessQueue');
66
        foreach ($orders as $order) {
67
            echo '<hr />Processing order: '.$order->ID;
68
            $outcome = $queueObjectSingleton->process($order);
69
            if ($outcome === true) {
70
                echo '<br />... Order moved successfully.<hr />';
71
            } else {
72
                echo '<br />... '.$outcome.'<hr />';
73
            }
74
        }
75
    }
76
}
77