CleanExpiredEstimatesTask   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 28
c 3
b 1
f 0
dl 0
loc 69
rs 10
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getSilent() 0 3 1
A log() 0 7 3
A setSilent() 0 4 1
A run() 0 25 4
1
<?php
2
3
namespace SilverCommerce\ShoppingCart\Tasks;
4
5
use DateTime;
6
use SilverStripe\Dev\BuildTask;
7
use SilverStripe\Security\Member;
8
use SilverStripe\Control\Director;
9
use SilverCommerce\OrdersAdmin\Model\Estimate;
10
use SilverCommerce\ShoppingCart\Model\ShoppingCart as ShoppingCart;
11
12
/**
13
 * Simple task that removes estimates that have passed their end date
14
 * and are not assigned to a customer.
15
 * 
16
 * @author ilateral (http://www.ilateral.co.uk)
17
 * @package shoppingcart
18
 */
19
class CleanExpiredEstimatesTask extends BuildTask
20
{
21
    protected $title = 'Clean expired estimates';
22
23
    protected $description = 'Clean all estimates that are past their expiration date and have no users assifgned';
24
25
    protected $enabled = true;
26
27
    /**
28
     * Should this task output commands 
29
     *
30
     * @var boolean
31
     */
32
    protected $silent = false;
33
34
    /**
35
     * @return boolean
36
     */
37
    public function getSilent()
38
    {
39
        return $this->silent;
40
    }
41
42
    /**
43
     * set the silent parameter
44
     *
45
     * @param boolean $set
46
     * @return CleanExpiredEstimatesTask
47
     */
48
    public function setSilent($set)
49
    {
50
        $this->silent = $set;
51
        return $this;
52
    }
53
54
    function run($request) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
55
        $now = new DateTime();
56
        $days = Estimate::config()->default_end;
57
        $past = $now->modify("-{$days} days");
58
59
        $all = ShoppingCart::get()->filter([
60
            "StartDate:LessThan" => $past->format('Y-m-d H:i:s')
61
        ]);
62
63
        $i = 0;
64
        foreach ($all as $cart) {
65
            // Is the cart currentyl assigned to a member?
66
            if ($cart->ContactID) {
67
                $curr = $cart->Contact()->Member();
68
            } else {
69
                $curr = false;
70
            }
71
72
            if (empty($curr)) {
73
                $cart->delete();
74
                $i++;
75
            }
76
        }
77
78
        $this->log('removed '.$i.' expired estimates.');
79
    }
80
81
    private function log($message)
82
    {
83
        if (!$this->silent) {
84
            if(Director::is_cli()) {
85
                echo $message . "\n";
86
            } else {
87
                echo $message . "<br/>";
88
            }
89
        }
90
    }
91
}