Schedule::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Basebuilder\Scheduling;
4
5
use Basebuilder\Scheduling\Event\Callback;
6
use Basebuilder\Scheduling\Event\Process;
7
use Webmozart\Assert\Assert;
8
9
/**
10
 * This class will allow you to register commands and retrieve all events that are due for processing
11
 */
12
class Schedule
13
{
14
    /**
15
     * Stack of events
16
     * @var Event[]
17
     */
18
    protected $events = [];
19
20
    /**
21
     * @var string|null
22
     */
23
    protected $name;
24
25 2
    public function __construct(/* string */ $name = '<anonymous schedule>')
26
    {
27 2
        Assert::nullOrString($name);
28 2
        $this->name = $name;
29 2
    }
30
31
    /**
32
     * Get the name of this schedule
33
     *
34
     * @return string|null
35
     */
36
    public function getName()
37
    {
38
        return $this->name;
39
    }
40
41
    /**
42
     * Add a single Event to the stack
43
     *
44
     * @param Event $event
45
     * @return $this
46
     */
47 2
    public function add(Event $event)
48
    {
49 2
        $this->events[] = $event;
50
51 2
        return $this;
52
    }
53
54
    /**
55
     * Creates a new Event, adds it to the schedule stack and returns you the instance so you can configure it
56
     *
57
     * @param  callable|string $event
58
     * @return Event
59
     */
60 2
    public function run($event)
61
    {
62 2
        if(is_callable($event)) {
63 2
            $event = new Callback($event);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $event. This often makes code more readable.
Loading history...
64 2
        } else {
65
            $event = new Process($event);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $event. This often makes code more readable.
Loading history...
66
        }
67
68 2
        $this->add($event);
69 2
        return $event;
70
    }
71
72
    /**
73
     * @return Event[]
74
     */
75
    public function allEvents()
76
    {
77
        return $this->events;
78
    }
79
80
    /**
81
     * Get all of the events on the schedule that are due.
82
     *
83
     * @return Event[]
84
     */
85
    public function dueEvents()
86
    {
87 1
        return array_filter($this->events, function (Event $event) {
88 1
            return $event->isDue();
89 1
        });
90
    }
91
}
92