Completed
Push — master ( de5c9f...2ef464 )
by Daniel
21s queued 11s
created

Scheduler::run()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 4
c 1
b 0
f 1
dl 0
loc 9
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jellyfish\Scheduler;
6
7
use DateTime;
8
9
class Scheduler implements SchedulerInterface
10
{
11
    /**
12
     * @var JobInterface[]
13
     */
14
    protected $jobs;
15
16
    /**
17
     * Scheduler constructor
18
     */
19
    public function __construct()
20
    {
21
        $this->jobs = [];
22
    }
23
24
    /**
25
     * @param \Jellyfish\Scheduler\JobInterface $job
26
     *
27
     * @return \Jellyfish\Scheduler\SchedulerInterface
28
     */
29
    public function queueJob(JobInterface $job): SchedulerInterface
30
    {
31
        $this->jobs[] = $job;
32
33
        return $this;
34
    }
35
36
    /**
37
     * @return \Jellyfish\Scheduler\SchedulerInterface
38
     */
39
    public function clearJobs(): SchedulerInterface
40
    {
41
        $this->jobs = [];
42
43
        return $this;
44
    }
45
46
    /**
47
     * @return \Jellyfish\Scheduler\SchedulerInterface
48
     *
49
     * @throws \Exception
50
     */
51
    public function run(): SchedulerInterface
52
    {
53
        $dateTime = new DateTime();
54
55
        foreach ($this->jobs as $job) {
56
            $job->run($dateTime);
57
        }
58
59
        return $this;
60
    }
61
62
    /**
63
     * @return \Jellyfish\Scheduler\JobInterface[]
64
     */
65
    public function getQueuedJobs(): array
66
    {
67
        return $this->jobs;
68
    }
69
}
70