Completed
Pull Request — develop (#11)
by jean-marie
05:11
created

Cron::getLogSize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace FOA\CronBundle\Manager;
4
5
use InvalidArgumentException;
6
7
/**
8
 * Cron represents a cron command. It holds:
9
 * - time data
10
 * - command
11
 * - comment
12
 * - log files
13
 * - cron execution status
14
 */
15
class Cron
16
{
17
    const ERROR_MINUTE = 1;
18
    const ERROR_HOUR = 2;
19
    const ERROR_DAY_OF_MONTH = 3;
20
    const ERROR_MONTH = 4;
21
    const ERROR_DAY_OF_WEEK = 5;
22
23
    /**
24
     * @var string
25
     */
26
    protected $minute = '*';
27
28
    /**
29
     * @var string
30
     */
31
    protected $hour = '*';
32
33
    /**
34
     * @var string
35
     */
36
    protected $dayOfMonth = '*';
37
38
    /**
39
     * @var string
40
     */
41
    protected $month = '*';
42
43
    /**
44
     * @var string
45
     */
46
    protected $dayOfWeek = '*';
47
48
    /**
49
     * @var string
50
     */
51
    protected $command;
52
53
    /**
54
     * @var string
55
     */
56
    protected $logFile = null;
57
58
    /**
59
     * The size of the log file
60
     *
61
     * @var string
62
     */
63
    protected $logSize = null;
64
65
    /**
66
     * @var string
67
     */
68
    protected $errorFile = null;
69
70
    /**
71
     * The size of the error file
72
     *
73
     * @var string
74
     */
75
    protected $errorSize = null;
76
77
    /**
78
     * The last run time based on when log files have been written
79
     *
80
     * @var int
81
     */
82
    protected $lastRunTime = null;
83
84
    /**
85
     * The status of the cron, based on the log files
86
     *
87
     * @var string
88
     */
89
    protected $status;
90
91
    /**
92
     * @var string
93
     */
94
    protected $comment;
95
96
    /**
97
     * isSuspended
98
     *
99
     * @var boolean
100
     * @access protected
101
     */
102
    protected $isSuspended = false;
103
104
    /**
105
     * Parses a cron line into a Cron instance
106
     *
107
     * @static
108
     *
109
     * @param $cron string The cron line
110
     *
111
     * @return Cron
112
     */
113
    public static function parse($cron)
114
    {
115
        if (substr($cron, 0, 12) == '#suspended: ') {
116
            $cron = substr($cron, 12);
117
            $isSuspended = true;
118
        }
119
120
        $parts = explode(' ', $cron);
121
122
        $command = implode(' ', array_slice($parts, 5));
123
124
        // extract comment
125
        if (strpos($command, '#')) {
126
            list($command, $comment) = explode('#', $command);
127
            $comment = trim($comment);
128
        }
129
130
        // extract error file
131
        if (strpos($command, '2>')) {
132
            list($command, $errorFile) = explode('2>', $command);
133
            $errorFile = trim($errorFile);
134
        }
135
136
        // extract log file
137
        if (strpos($command, '>')) {
138
            list($command, $logFile) = explode('>', $command);
139
            $logFile = trim($logFile);
140
        }
141
142
        // compute last run time, and file size
143
        $lastRunTime = null;
144
        $logSize = null;
145
        $errorSize = null;
146
        if (isset($logFile) && file_exists($logFile)) {
147
            $lastRunTime = filemtime($logFile);
148
            $logSize = filesize($logFile);
149
        }
150
        if (isset($errorFile) && file_exists($errorFile)) {
151
            $lastRunTime = max($lastRunTime ?: 0, filemtime($errorFile));
152
            $errorSize = filesize($errorFile);
153
        }
154
155
        // compute status
156
        $status = 'error';
157
        if (!$logSize && !$errorSize) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $logSize of type integer|null is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
Bug Best Practice introduced by
The expression $errorSize of type integer|null is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
158
            $status = 'unknown';
159
        } elseif (!$errorSize || $errorSize == 0) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $errorSize of type integer|null is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
160
            $status = 'success';
161
        }
162
163
        // create cron instance
164
        $cron = new self();
165
        $cron->setMinute($parts[0])
166
            ->setHour($parts[1])
167
            ->setDayOfMonth($parts[2])
168
            ->setMonth($parts[3])
169
            ->setDayOfWeek($parts[4])
170
            ->setCommand(\trim($command))
171
            ->setLastRunTime($lastRunTime)
172
            ->setLogSize($logSize)
173
            ->setErrorSize($errorSize)
174
            ->setStatus($status);
175
176
        if (isset($isSuspended)) {
177
            $cron->setSuspended($isSuspended);
178
        }
179
        if (isset($comment)) {
180
            $cron->setComment($comment);
181
        }
182
        if (isset($logFile)) {
183
            $cron->setLogFile($logFile);
184
        }
185
        if (isset($errorFile)) {
186
            $cron->setErrorFile($errorFile);
187
        }
188
189
        return $cron;
190
    }
191
192
    /**
193
     * @param string $command
194
     *
195
     * @return $this
196
     */
197
    public function setCommand($command)
198
    {
199
        $this->command = $command;
200
201
        return $this;
202
    }
203
204
    /**
205
     * @return string
206
     */
207
    public function getCommand()
208
    {
209
        return $this->command;
210
    }
211
212
    /**
213
     * @param string $dayOfMonth
214
     *
215
     * @return $this
216
     */
217
    public function setDayOfMonth($dayOfMonth)
218
    {
219
        $this->dayOfMonth = $dayOfMonth;
220
221
        return $this;
222
    }
223
224
    /**
225
     * @return string
226
     */
227
    public function getDayOfMonth()
228
    {
229
        return $this->dayOfMonth;
230
    }
231
232
    /**
233
     * @param string $dayOfWeek
234
     * @param        $dayOfWeek
235
     *
236
     * @return $this
237
     */
238
    public function setDayOfWeek($dayOfWeek)
239
    {
240
        $this->dayOfWeek = $dayOfWeek;
241
242
        return $this;
243
    }
244
245
    /**
246
     * @return string
247
     */
248
    public function getDayOfWeek()
249
    {
250
        return $this->dayOfWeek;
251
    }
252
253
    /**
254
     * @param string $hour
255
     * @param        $hour
256
     *
257
     * @return $this
258
     */
259 View Code Duplication
    public function setHour($hour)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
260
    {
261
        if (is_numeric($hour) &&
262
            $hour < 0 || $hour > 23
263
        ) {
264
            throw new InvalidArgumentException('Invalid hour format', self::ERROR_HOUR);
265
        }
266
267
        $this->hour = $hour;
0 ignored issues
show
Documentation Bug introduced by
It seems like $hour can also be of type integer or double. However, the property $hour is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
268
269
        return $this;
270
    }
271
272
    /**
273
     * @return string
274
     */
275
    public function getHour()
276
    {
277
        return $this->hour;
278
    }
279
280
    /**
281
     * @param string $minute
282
     *
283
     * @return $this
284
     */
285 View Code Duplication
    public function setMinute($minute)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
286
    {
287
        if (is_numeric($minute) &&
288
            $minute < 0 || $minute > 23
289
        ) {
290
            throw new InvalidArgumentException('Invalid minute format', self::ERROR_MINUTE);
291
        }
292
293
        $this->minute = $minute;
0 ignored issues
show
Documentation Bug introduced by
It seems like $minute can also be of type integer or double. However, the property $minute is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
294
295
        return $this;
296
    }
297
298
    /**
299
     * @return string
300
     */
301
    public function getMinute()
302
    {
303
        return $this->minute;
304
    }
305
306
    /**
307
     * @param string $month
308
     *
309
     * @return $this
310
     */
311 View Code Duplication
    public function setMonth($month)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
312
    {
313
        if (is_numeric($month) &&
314
            $month < 1 || $month > 12
315
        ) {
316
            throw new InvalidArgumentException('Invalid month format', self::ERROR_MONTH);
317
        }
318
319
        $this->month = $month;
0 ignored issues
show
Documentation Bug introduced by
It seems like $month can also be of type integer or double. However, the property $month is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
320
321
        return $this;
322
    }
323
324
    /**
325
     * @return string
326
     */
327
    public function getMonth()
328
    {
329
        return $this->month;
330
    }
331
332
    /**
333
     * @param string $comment
334
     *
335
     * @return $this
336
     */
337
    public function setComment($comment)
338
    {
339
        $this->comment = $comment;
340
341
        return $this;
342
    }
343
344
    /**
345
     * @return string
346
     */
347
    public function getComment()
348
    {
349
        return $this->comment;
350
    }
351
352
    /**
353
     * @param string $logFile
354
     *
355
     * @return $this
356
     */
357
    public function setLogFile($logFile)
358
    {
359
        $this->logFile = $logFile;
360
361
        return $this;
362
    }
363
364
    /**
365
     * @return string
366
     */
367
    public function getLogFile()
368
    {
369
        return $this->logFile;
370
    }
371
372
    /**
373
     * @param string $errorFile
374
     *
375
     * @return $this
376
     */
377
    public function setErrorFile($errorFile)
378
    {
379
        $this->errorFile = $errorFile;
380
381
        return $this;
382
    }
383
384
    /**
385
     * @return string
386
     */
387
    public function getErrorFile()
388
    {
389
        return $this->errorFile;
390
    }
391
392
    /**
393
     * @param int $lastRunTime
394
     *
395
     * @return $this
396
     */
397
    public function setLastRunTime($lastRunTime)
398
    {
399
        $this->lastRunTime = $lastRunTime;
400
401
        return $this;
402
    }
403
404
    /**
405
     * @return int
406
     */
407
    public function getLastRunTime()
408
    {
409
        return $this->lastRunTime;
410
    }
411
412
    /**
413
     * @param string $errorSize
414
     *
415
     * @return $this
416
     */
417
    public function setErrorSize($errorSize)
418
    {
419
        $this->errorSize = $errorSize;
420
421
        return $this;
422
    }
423
424
    /**
425
     * @return string
426
     */
427
    public function getErrorSize()
428
    {
429
        return $this->errorSize;
430
    }
431
432
    /**
433
     * @param string $logSize
434
     *
435
     * @return $this
436
     */
437
    public function setLogSize($logSize)
438
    {
439
        $this->logSize = $logSize;
440
441
        return $this;
442
    }
443
444
    /**
445
     * @return string
446
     */
447
    public function getLogSize()
448
    {
449
        return $this->logSize;
450
    }
451
452
    /**
453
     * @param string $status
454
     *
455
     * @return $this
456
     */
457
    public function setStatus($status)
458
    {
459
        $this->status = $status;
460
461
        return $this;
462
    }
463
464
    /**
465
     * @return string
466
     */
467
    public function getStatus()
468
    {
469
        return $this->status;
470
    }
471
472
    /**
473
     * Concatenate time data to get the time expression
474
     *
475
     * @return string
476
     */
477
    public function getExpression()
478
    {
479
        return sprintf('%s %s %s %s %s', $this->minute, $this->hour, $this->dayOfMonth, $this->month, $this->dayOfWeek);
480
    }
481
482
    /**
483
     * Gets the value of isSuspended
484
     *
485
     * @return boolean
486
     */
487
    public function isSuspended()
488
    {
489
        return $this->isSuspended;
490
    }
491
492
    /**
493
     * Sets the value of isSuspended
494
     *
495
     * @param boolean $isSuspended status
496
     *
497
     * @return Cron
498
     */
499
    public function setSuspended($isSuspended = true)
500
    {
501
        if ($this->isSuspended != $isSuspended) {
502
            $this->isSuspended = $isSuspended;
503
        }
504
505
        return $this;
506
    }
507
508
    /**
509
     * Transforms the cron instance into a cron line
510
     *
511
     * @return string
512
     */
513
    public function __toString()
514
    {
515
        $cronLine = '';
516
        if ($this->isSuspended()) {
517
            $cronLine .= '#suspended: ';
518
        }
519
520
        $cronLine .= $this->getExpression() . ' ' . $this->command;
521
        if ('' != $this->logFile) {
522
            $cronLine .= ' > ' . $this->logFile;
523
        }
524
        if ('' != $this->errorFile) {
525
            $cronLine .= ' 2> ' . $this->errorFile;
526
        }
527
        if ('' != $this->comment) {
528
            $cronLine .= ' #' . $this->comment;
529
        }
530
531
        return $cronLine;
532
    }
533
}
534