GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 6956e2...9a55f0 )
by
unknown
02:08
created

ScheduledExecutionExtension::updateCMSFields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 9.264
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Symbiote\QueuedJobs\Extensions;
4
5
use SilverStripe\Forms\DatetimeField;
6
use SilverStripe\Forms\DropdownField;
7
use SilverStripe\Forms\FieldGroup;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\NumericField;
10
use SilverStripe\Forms\ReadonlyField;
11
use SilverStripe\Forms\TextField;
12
use SilverStripe\ORM\DataExtension;
13
use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor;
14
use Symbiote\QueuedJobs\Jobs\ScheduledExecutionJob;
15
use Symbiote\QueuedJobs\Services\QueuedJobService;
16
17
/**
18
 * An extension that can be added to objects that automatically
19
 * adds scheduled execution capabilities to data objects.
20
 *
21
 * Developers who want to use these capabilities can set up
22
 *
23
 * @author [email protected]
24
 * @license BSD License http://silverstripe.org/bsd-license/
25
 */
26
class ScheduledExecutionExtension extends DataExtension
27
{
28
    /**
29
     * @var array
30
     */
31
    private static $db = array(
0 ignored issues
show
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...
32
        'FirstExecution' => 'DBDatetime',
33
        'ExecuteInterval' => 'Int',
34
        'ExecuteEvery' => "Enum(',Minute,Hour,Day,Week,Fortnight,Month,Year')",
35
        'ExecuteFree' => 'Varchar',
36
    );
37
38
    /**
39
     * @var array
40
     */
41
    private static $defaults = array(
0 ignored issues
show
Unused Code introduced by
The property $defaults 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
        'ExecuteInterval' => 1,
43
    );
44
45
    /**
46
     * @var array
47
     */
48
    private static $has_one = array(
0 ignored issues
show
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...
49
        'ScheduledJob' => QueuedJobDescriptor::class,
50
    );
51
52
    /**
53
     * @param FieldSet $fields
54
     */
55
    public function updateCMSFields(FieldList $fields)
56
    {
57
        $fields->findOrMakeTab(
58
            'Root.Schedule',
59
            _t(__CLASS__ . '.ScheduleTabTitle', 'Schedule')
60
        );
61
        $fields->addFieldsToTab('Root.Schedule', array(
62
            $dt = DatetimeField::create('FirstExecution', _t(__CLASS__ . '.FIRST_EXECUTION', 'First Execution')),
63
            FieldGroup::create(
64
                NumericField::create('ExecuteInterval', ''),
65
                DropdownField::create(
66
                    'ExecuteEvery',
67
                    '',
68
                    array(
69
                        '' => '',
70
                        'Minute' => _t(__CLASS__ . '.ExecuteEveryMinute', 'Minute'),
71
                        'Hour' => _t(__CLASS__ . '.ExecuteEveryHour', 'Hour'),
72
                        'Day' => _t(__CLASS__ . '.ExecuteEveryDay', 'Day'),
73
                        'Week' => _t(__CLASS__ . '.ExecuteEveryWeek', 'Week'),
74
                        'Fortnight' => _t(__CLASS__ . '.ExecuteEveryFortnight', 'Fortnight'),
75
                        'Month' => _t(__CLASS__ . '.ExecuteEveryMonth', 'Month'),
76
                        'Year' => _t(__CLASS__ . '.ExecuteEveryYear', 'Year'),
77
                    )
78
                )
79
            )->setTitle(_t(__CLASS__ . '.EXECUTE_EVERY', 'Execute every')),
80
            TextField::create(
81
                'ExecuteFree',
82
                _t(__CLASS__ . '.EXECUTE_FREE', 'Scheduled (in strtotime format from first execution)')
83
            )
84
        ));
85
86
        if ($this->owner->ScheduledJobID) {
87
            $jobTime = $this->owner->ScheduledJob()->StartAfter;
88
            $fields->addFieldsToTab('Root.Schedule', array(
89
                ReadonlyField::create('NextRunDate', _t(__CLASS__ . '.NEXT_RUN_DATE', 'Next run date'), $jobTime)
90
            ));
91
        }
92
93
        $dt->getDateField()->setConfig('showcalendar', true);
94
        $dt->getTimeField()->setConfig('showdropdown', true);
95
    }
96
97
    public function onBeforeWrite()
98
    {
99
        parent::onBeforeWrite();
100
101
        if ($this->owner->FirstExecution) {
102
            $changed = $this->owner->getChangedFields();
103
            $changed = (
104
                isset($changed['FirstExecution'])
105
                || isset($changed['ExecuteInterval'])
106
                || isset($changed['ExecuteEvery'])
107
                || isset($changed['ExecuteFree'])
108
            );
109
110
            if ($changed && $this->owner->ScheduledJobID) {
111
                if ($this->owner->ScheduledJob()->exists()) {
112
                    $this->owner->ScheduledJob()->delete();
113
                }
114
115
                $this->owner->ScheduledJobID = 0;
116
            }
117
118
            if (!$this->owner->ScheduledJobID) {
119
                $job = new ScheduledExecutionJob($this->owner);
120
                $time = date('Y-m-d H:i:s');
121
                if ($this->owner->FirstExecution) {
122
                    $time = date('Y-m-d H:i:s', strtotime($this->owner->FirstExecution));
123
                }
124
125
                $this->owner->ScheduledJobID = singleton(QueuedJobService::class)
126
                    ->queueJob($job, $time);
127
            }
128
        }
129
    }
130
131
    /**
132
     * Define your own version of this method in your data objects to be executed EVERY time
133
     * the scheduled job triggers.
134
     */
135
    public function onScheduledExecution()
136
    {
137
    }
138
}
139