Schedule   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 76.19%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 7
c 2
b 0
f 1
lcom 1
cbo 4
dl 0
loc 80
ccs 16
cts 21
cp 0.7619
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
A add() 0 6 1
A run() 0 11 2
A allEvents() 0 4 1
A dueEvents() 0 6 1
A __construct() 0 5 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