Passed
Branch master (a2c9fd)
by Sebastiaan
02:08
created

Schedule::run()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace Basebuilder\Scheduling;
4
use Webmozart\Assert\Assert;
5
6
/**
7
 * This class will allow you to register commands and retrieve all events that are due for processing
8
 */
9
class Schedule
10
{
11
    /**
12
     * Stack of events
13
     * @var Event[]
14
     */
15
    protected $events = [];
16
17
    /**
18
     * @var string|null
19
     */
20
    protected $name;
21
22
    public function __construct(/* string */ $name = null)
23
    {
24
        Assert::nullOrString($name);
25
26
        $this->name = $name;
27
    }
28
29
    /**
30
     * Get the name of this schedule
31
     *
32
     * @return string|null
33
     */
34
    public function getName()
35
    {
36
        return $this->name;
37
    }
38
39
    /**
40
     * Creates a new Event, adds it to the schedule stack and returns you the instance so you can configure it
41
     *
42
     * @param  string $command
43
     * @return Event
44
     */
45
    public function run(/* string */ $command)
46
    {
47
        Assert::stringNotEmpty($command);
48
        $this->events[] = $event = new Event($command);
49
50
        return $event;
51
    }
52
53
    /**
54
     * @return Event[]
55
     */
56
    public function allEvents()
57
    {
58
        return $this->events;
59
    }
60
61
    /**
62
     * Get all of the events on the schedule that are due.
63
     *
64
     * @return Event[]
65
     */
66
    public function dueEvents()
67
    {
68
        return array_filter($this->events, function (Event $event) {
69
            return $event->isDue();
70
        });
71
    }
72
}
73