Passed
Push — master ( 1b561f...b63c2d )
by Daniel
03:23 queued 12s
created

Job   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
eloc 15
c 1
b 0
f 1
dl 0
loc 78
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A isDue() 0 3 1
A getCommand() 0 3 1
A run() 0 13 3
A getCronExpression() 0 3 1
A __construct() 0 6 1
A isRunning() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jellyfish\Scheduler;
6
7
use Cron\CronExpression;
8
use DateTime;
9
use Jellyfish\Process\ProcessInterface;
10
11
class Job implements JobInterface
12
{
13
    /**
14
     * @var \Jellyfish\Process\ProcessInterface
15
     */
16
    protected $process;
17
18
    /**
19
     * @var \Cron\CronExpression
20
     */
21
    protected $cronExpression;
22
23
    /**
24
     * @param \Jellyfish\Process\ProcessInterface $process
25
     * @param \Cron\CronExpression $cronExpression
26
     */
27
    public function __construct(
28
        ProcessInterface $process,
29
        CronExpression $cronExpression
30
    ) {
31
        $this->process = $process;
32
        $this->cronExpression = $cronExpression;
33
    }
34
35
    /**
36
     * @return array
37
     */
38
    public function getCommand(): array
39
    {
40
        return $this->process->getCommand();
41
    }
42
43
    /**
44
     * @return \Cron\CronExpression
45
     */
46
    public function getCronExpression(): CronExpression
47
    {
48
        return $this->cronExpression;
49
    }
50
51
    /**
52
     * @param \DateTime $dateTime
53
     *
54
     * @return bool
55
     */
56
    protected function isDue(DateTime $dateTime): bool
57
    {
58
        return $this->cronExpression->isDue($dateTime);
59
    }
60
61
    /**
62
     * @param \DateTime|null $dateTime
63
     *
64
     * @return \Jellyfish\Scheduler\JobInterface
65
     *
66
     * @throws \Exception
67
     */
68
    public function run(?DateTime $dateTime = null): JobInterface
69
    {
70
        if ($dateTime === null) {
71
            $dateTime = new DateTime();
72
        }
73
74
        if (!$this->isDue($dateTime)) {
75
            return $this;
76
        }
77
78
        $this->process->start();
79
80
        return $this;
81
    }
82
83
    /**
84
     * @return bool
85
     */
86
    public function isRunning(): bool
87
    {
88
        return $this->process->isRunning();
89
    }
90
}
91