Passed
Push — 1.0 ( 024343...5b8585 )
by Morven
10:07
created

CleanExpiredEstimatesTask::getSilent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverCommerce\ShoppingCart\Tasks;
4
5
use SilverStripe\Dev\BuildTask;
6
use SilverStripe\Control\Director;
7
use SilverCommerce\ShoppingCart\Model\ShoppingCart as ShoppingCart;
8
use SilverCommerce\OrdersAdmin\Model\Estimate;
9
use DateTime;
10
11
/**
12
 * Simple task that removes estimates that have passed their end date
13
 * and are not assigned to a customer.
14
 * 
15
 * @author ilateral (http://www.ilateral.co.uk)
16
 * @package shoppingcart
17
 */
18
class CleanExpiredEstimatesTask extends BuildTask
19
{
20
    protected $title = 'Clean expired estimates';
21
22
    protected $description = 'Clean all estimates that are past their expiration date and have no users assifgned';
23
24
    protected $enabled = true;
25
26
    /**
27
     * Should this task output commands 
28
     *
29
     * @var boolean
30
     */
31
    protected $silent = false;
32
33
    /**
34
     * @return boolean
35
     */
36
    public function getSilent()
37
    {
38
        return $this->silent;
39
    }
40
41
    /**
42
     * set the silent parameter
43
     *
44
     * @param boolean $set
45
     * @return CleanExpiredEstimatesTask
46
     */
47
    public function setSilent($set)
48
    {
49
        $this->silent = $set;
50
        return $this;
51
    }
52
53
    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...
54
        $now = new DateTime();
55
        $days = Estimate::config()->default_end;
56
        $past = $now->modify("-{$days} days");
57
58
        $all = ShoppingCart::get()->filter([
59
            "StartDate:LessThan" => $past->format('Y-m-d H:i:s')
60
        ]);
61
62
        $i = 0;
63
        foreach ($all as $cart) {
64
            // Is the cart currentyl assigned to a member?
65
            $curr = Member::get()->find("CartID", $cart->ID);
0 ignored issues
show
Bug introduced by
The type SilverCommerce\ShoppingCart\Tasks\Member was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
66
67
            if (empty($curr)) {
68
                $cart->delete();
69
                $i++;
70
            }
71
        }
72
73
        $this->log('removed '.$i.' expired estimates.');
74
    }
75
76
    private function log($message)
77
    {
78
        if (!$this->silent) {
79
            if(Director::is_cli()) {
80
                echo $message . "\n";
81
            } else {
82
                echo $message . "<br/>";
83
            }
84
        }
85
    }
86
}