Completed
Push — 2.0 ( 4129d9...bcdb61 )
by Marco
02:16
created

Schedule::merge()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 6
nc 1
nop 1
1
<?php namespace Comodojo\Extender\Orm\Entities;
2
3
use \Doctrine\ORM\Mapping as ORM;
4
use \Comodojo\Extender\Traits\BaseEntityTrait;
5
use \Comodojo\Extender\Traits\RequestEntityTrait;
6
use \Cron\CronExpression;
7
use \Comodojo\Foundation\Validation\DataFilter;
8
use \DateTime;
9
10
/**
11
 * @package     Comodojo Extender
12
 * @author      Marco Giovinazzi <[email protected]>
13
 * @license     MIT
14
 *
15
 * LICENSE:
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
 * THE SOFTWARE.
24
 *
25
 * @ORM\Table(name="extender_schedule")
26
 * @ORM\Entity
27
 */
28
29
class Schedule {
30
31
    use BaseEntityTrait;
32
    use RequestEntityTrait;
33
34
    /**
35
     * @var string
36
     *
37
     * @ORM\Column(name="name", type="string", length=256, nullable=false, unique=true)
38
     */
39
    protected $name;
40
41
    /**
42
     * @var string
43
     *
44
     * @ORM\Column(name="description", type="text", length=65535, nullable=true)
45
     */
46
    protected $description;
47
48
    /**
49
     * @var string
50
     *
51
     * @ORM\Column(name="minute", type="string", length=16, nullable=false)
52
     */
53
    protected $minute;
54
55
    /**
56
     * @var string
57
     *
58
     * @ORM\Column(name="hour", type="string", length=16, nullable=false)
59
     */
60
    protected $hour;
61
62
    /**
63
     * @var string
64
     *
65
     * @ORM\Column(name="day", type="string", length=16, nullable=false)
66
     */
67
    protected $day;
68
69
    /**
70
     * @var string
71
     *
72
     * @ORM\Column(name="month", type="string", length=16, nullable=false)
73
     */
74
    protected $month;
75
76
    /**
77
     * @var string
78
     *
79
     * @ORM\Column(name="weekday", type="string", length=16, nullable=false)
80
     */
81
    protected $weekday;
82
83
    /**
84
     * @var string
85
     *
86
     * @ORM\Column(name="year", type="string", length=16, nullable=true)
87
     */
88
    protected $year;
89
90
    /**
91
     * @var boolean
92
     *
93
     * @ORM\Column(name="enabled", type="boolean", nullable=false)
94
     */
95
    protected $enabled = false;
96
97
    /**
98
     * @var datetime
99
     *
100
     * @ORM\Column(name="firstrun", type="datetime", nullable=true)
101
     */
102
    protected $firstrun;
103
104
    /**
105
     * @var datetime
106
     *
107
     * @ORM\Column(name="lastrun", type="datetime", nullable=true)
108
     */
109
    protected $lastrun;
110
111
    /**
112
     * Get a brief job description
113
     *
114
     * @return string
115
     */
116
    public function getDescription() {
117
118
        return $this->description;
119
120
    }
121
122
    /**
123
     * Set brief job description
124
     *
125
     * @param string $description
126
     * @return Schedule
127
     */
128
    public function setDescription($description) {
129
130
        $this->description = $description;
131
132
        return $this;
133
134
    }
135
136
    /**
137
     * Get cron expression of this schedule
138
     *
139
     * @return string
140
     */
141
    public function getExpression() {
142
143
        return $this->buildExpression();
144
145
    }
146
147
    /**
148
     * set cron expression for this schedule
149
     *
150
     * @param srting $expression A cron-compatible expression
151
     * @return Schedule
152
     */
153
    public function setExpression(CronExpression $expression) {
154
155
        $this->minute = $expression->getExpression(0);
156
        $this->hour = $expression->getExpression(1);
157
        $this->day = $expression->getExpression(2);
158
        $this->month = $expression->getExpression(3);
159
        $this->weekday = $expression->getExpression(4);
160
        $this->year = $expression->getExpression(5);
161
162
        return $this;
163
164
    }
165
166
    /**
167
     * True if job is currently enabled
168
     *
169
     * @return bool
170
     */
171
    public function getEnabled() {
172
173
        return $this->enabled;
174
175
    }
176
177
    /**
178
     * Set enable/disable status
179
     *
180
     * @param bool $enable
0 ignored issues
show
Documentation introduced by
There is no parameter named $enable. Did you maybe mean $enabled?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
181
     * @return Schedule
182
     */
183
    public function setEnabled($enabled) {
184
185
        $ena = DataFilter::filterBoolean($enabled);
186
187
        $this->enabled = $ena;
188
189
        return $this;
190
191
    }
192
193
    /**
194
     * Get the first-run-date of job
195
     *
196
     * @return DateTime
197
     */
198
    public function getFirstrun() {
199
200
        return $this->firstrun;
201
202
    }
203
204
    /**
205
     * Set the first-run-date of job
206
     *
207
     * @param DateTime $datetime
208
     * @return Schedule
209
     */
210
    public function setFirstrun(DateTime $datetime) {
211
212
        $this->firstrun = $datetime;
0 ignored issues
show
Documentation Bug introduced by
It seems like $datetime of type object<DateTime> is incompatible with the declared type object<Comodojo\Extender\Orm\Entities\datetime> of property $firstrun.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
213
214
        return $this;
215
216
    }
217
218
    /**
219
     * Get the first-run-date of job
220
     *
221
     * @return DateTime
222
     */
223
    public function getLastrun() {
224
225
        return $this->lastrun;
226
227
    }
228
229
    /**
230
     * Set the first-run-date of job
231
     *
232
     * @param DateTime $datetime
233
     * @return Schedule
234
     */
235
    public function setLastrun(DateTime $datetime) {
236
237
        $this->lastrun = $datetime;
0 ignored issues
show
Documentation Bug introduced by
It seems like $datetime of type object<DateTime> is incompatible with the declared type object<Comodojo\Extender\Orm\Entities\datetime> of property $lastrun.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
238
239
        return $this;
240
241
    }
242
243
    public function shouldRunJob(DateTime $time) {
244
245
        $first_run = $this->getFirstrun();
246
        $last_run = $this->getLastrun();
247
248
        $next_run = $this->buildExpression()->getNextRunDate(is_null($last_run) ? $first_run : $last_run);
249
250
        return $next_run <= $time ? true : false;
251
252
        // return $time <= $next_run;
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
253
254
    }
255
256
    public function getNextPlannedRun(DateTime $time) {
257
258
        // $first_run = $this->getFirstrun();
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
259
        // $last_run = $this->getLastrun();
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
260
        //
261
        // if ( is_null($last_run) ) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
262
        //     $next_run = $this->getFirstrun();
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
263
        // } else {
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
264
        //     $next_run = $this->buildExpression()->getNextRunDate($time);
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
265
        // }
266
        //
267
        // return $next_run;
268
269
        return $this->buildExpression()->getNextRunDate($time);
270
271
    }
272
273
    public function merge(Schedule $schedule) {
274
275
        $this->setName($schedule->getName())
276
            ->setDescription($schedule->getDescription())
277
            ->setExpression($schedule->getExpression())
278
            ->setEnabled($schedule->getEnabled());
279
280
        return $this;
281
282
    }
283
284
    protected function buildExpression() {
285
286
        $exp_string = implode(" ", [
287
            $this->minute,
288
            $this->hour,
289
            $this->day,
290
            $this->month,
291
            $this->weekday,
292
            $this->year
293
        ]);
294
295
        return CronExpression::factory($exp_string);
296
297
    }
298
}
299