Completed
Push — master ( 136b8b...5a2345 )
by Julián
02:59
created

src/Janitor.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Effortless maintenance management (http://juliangut.com/janitor)
4
 *
5
 * @link https://github.com/juliangut/janitor for the canonical source repository
6
 * @license https://github.com/juliangut/janitor/blob/master/LICENSE
7
 */
8
9
namespace Janitor;
10
11
use Janitor\Strategy\Render as RenderStrategy;
12
13
class Janitor
14
{
15
    /**
16
     * List of maintenance watchers.
17
     *
18
     * @var array
19
     */
20
    protected $watchers = [];
21
22
    /**
23
     * List of excluders conditions.
24
     *
25
     * @var array
26
     */
27
    protected $excluders = [];
28
29
    /**
30
     * Resolve strategy.
31
     *
32
     * @var Janitor\Strategy
33
     */
34
    protected $strategy;
35
36
    /**
37
     * @param array $watchers
38
     * @param array $excluders
39
     * @param Janitor\Strategy $strategy
40
     */
41
    public function __construct(array $watchers = [], array $excluders = [], Strategy $strategy = null)
42
    {
43
        foreach ($watchers as $watcher) {
44
            $this->addWatcher($watcher);
45
        }
46
47
        foreach ($excluders as $excluder) {
48
            $this->addExcluder($excluder);
49
        }
50
51
        $this->strategy = $strategy;
0 ignored issues
show
Documentation Bug introduced by
It seems like $strategy can also be of type object<Janitor\Strategy>. However, the property $strategy is declared as type object<Janitor\Janitor\Strategy>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
52
    }
53
54
    /**
55
     * Handle maintenance mode.
56
     *
57
     * @return bool
58
     */
59
    public function handle()
60
    {
61
        if (!$this->isExcluded()) {
62
            foreach ($this->watchers as $watcher) {
63
                if ($watcher->isActive()) {
64
                    if (!$this->strategy instanceof Strategy) {
65
                        $this->strategy = new RenderStrategy;
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Janitor\Strategy\Render() of type object<Janitor\Strategy\Render> is incompatible with the declared type object<Janitor\Janitor\Strategy> of property $strategy.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
66
                    }
67
68
                    call_user_func([$this->strategy, 'handle'], $watcher);
69
70
                    return true;
71
                }
72
            }
73
        }
74
75
        return false;
76
    }
77
78
    /**
79
     * Whether maintenance mode is active.
80
     *
81
     * @return bool
82
     */
83
    public function inMaintenance()
84
    {
85
        return $this->getActiveWatcher() instanceof Watcher;
86
    }
87
88
    /**
89
     * Get currenlty active watcher.
90
     *
91
     * @return \Janitor\Watcher
92
     */
93
    public function getActiveWatcher()
94
    {
95
        foreach ($this->watchers as $watcher) {
96
            if ($watcher->isActive()) {
97
                return $watcher;
98
            }
99
        }
100
101
        return null;
102
    }
103
104
    /**
105
     * Whether excluding conditions are met.
106
     *
107
     * @return bool
108
     */
109
    public function isExcluded()
110
    {
111
        foreach ($this->excluders as $excluder) {
112
            if ($excluder->isExcluded()) {
113
                return true;
114
            }
115
        }
116
117
        return false;
118
    }
119
120
    /**
121
     * Get next scheduled time spans.
122
     *
123
     * Returns an array of ['start' => \DateTime, 'end' => \DateTime]
124
     *
125
     * @param  integer $count
126
     * @return array
127
     */
128
    public function getScheduledTimes($count = 5)
129
    {
130
        $scheduledTimes = [];
131
132
        foreach ($this->watchers as $watcher) {
133
            if ($watcher instanceof ScheduledWatcher && $watcher->isScheduled()) {
134
                $scheduledTimes = array_merge($scheduledTimes, $watcher->getScheduledTimes($count));
135
            }
136
        }
137
138
        usort(
139
            $scheduledTimes,
140
            function ($time1, $time2) {
141
                if ($time1['start'] == $time2['start']) {
142
                    return 0;
143
                }
144
145
                return $time1['start'] < $time2['start'] ? -1 : 1;
146
            }
147
        );
148
149
        return array_slice($scheduledTimes, 0, $count);
150
    }
151
152
    /**
153
     * Add maintenance watcher.
154
     *
155
     * @param \Janitor\Watcher $watcher
156
     */
157
    public function addWatcher(Watcher $watcher)
158
    {
159
        $this->watchers[] = $watcher;
160
161
        return $this;
162
    }
163
164
    /**
165
     * Add excluder condition.
166
     *
167
     * @param \Janitor\Excluder $excluder
168
     */
169
    public function addExcluder(Excluder $excluder)
170
    {
171
        $this->excluders[] = $excluder;
172
173
        return $this;
174
    }
175
176
    /**
177
     * Set strategy.
178
     *
179
     * @param \Janitor\Strategy $strategy
180
     */
181
    public function setStrategy(Strategy $strategy)
182
    {
183
        $this->strategy = $strategy;
0 ignored issues
show
Documentation Bug introduced by
It seems like $strategy of type object<Janitor\Strategy> is incompatible with the declared type object<Janitor\Janitor\Strategy> of property $strategy.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
184
185
        return $this;
186
    }
187
}
188