Passed
Push — master ( a2c9fd...d26710 )
by Sebastiaan
02:39
created

Schedule::run()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
cc 1
eloc 5
nc 1
nop 1
crap 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 2
    public function __construct(/* string */ $name = null)
23
    {
24 2
        Assert::nullOrString($name);
25
26 2
        $this->name = $name;
27 2
    }
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
     * Add a single Event to the stack
41
     *
42
     * @param Event $event
43
     * @return $this
44
     */
45 2
    public function add(Event $event)
46
    {
47 2
        $this->events[] = $event;
48
49 2
        return $this;
50
    }
51
52
    /**
53
     * Creates a new Event, adds it to the schedule stack and returns you the instance so you can configure it
54
     *
55
     * @param  string $command
56
     * @return Event
57
     */
58 2
    public function run(/* string */ $command)
59
    {
60 2
        Assert::stringNotEmpty($command);
61 2
        $event = new Event($command);
62
63 2
        $this->add($event);
64
65 2
        return $event;
66
    }
67
68
    /**
69
     * @return Event[]
70
     */
71
    public function allEvents()
72
    {
73
        return $this->events;
74
    }
75
76
    /**
77
     * Get all of the events on the schedule that are due.
78
     *
79
     * @return Event[]
80
     */
81
    public function dueEvents()
82
    {
83 1
        return array_filter($this->events, function (Event $event) {
84 1
            return $event->isDue();
85 1
        });
86
    }
87
}
88