GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( b06f55...54bd50 )
by Pascal
05:48
created

Executor   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 209
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 4
dl 0
loc 209
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A make() 0 4 1
A passes() 0 8 2
A fails() 0 4 1
A getChecker() 0 4 1
A getException() 0 4 1
A handle() 0 19 3
A handleFailedChecker() 0 8 3
A handleAllowedRetry() 0 22 4
A configureRetryJobDefaults() 0 14 4
A addCallbackBeforeRetryCheckerJob() 0 14 3
1
<?php
2
3
namespace Pbmedia\ApiHealth\Checkers;
4
5
use Illuminate\Notifications\Notification;
6
use Illuminate\Queue\Events\JobProcessing;
7
use Illuminate\Support\Facades\Queue;
8
use Pbmedia\ApiHealth\Checkers\Checker;
9
use Pbmedia\ApiHealth\Checkers\CheckerHasFailed;
10
use Pbmedia\ApiHealth\Storage\CheckerState;
11
12
class Executor
13
{
14
    /**
15
     * The checker.
16
     *
17
     * @var \Pbmedia\ApiHealth\Checkers\Checker
18
     */
19
    private $checker;
20
21
    /**
22
     * The state of the checker.
23
     *
24
     * @var \Pbmedia\ApiHealth\Storage\CheckerState
25
     */
26
    private $state;
27
28
    /**
29
     * The caught exception if the checker fails.
30
     *
31
     * @var \Pbmedia\ApiHealth\Checkers\CheckerHasFailed
32
     */
33
    private $exception;
34
35
    /**
36
     * Boolean wether the checker has failed of not.
37
     *
38
     * @var bool
39
     */
40
    private $failed;
41
42
    /**
43
     * Creates an instance with the given checker
44
     *
45
     * @param \Pbmedia\ApiHealth\Checkers\Checker $checker
46
     */
47
    public function __construct(Checker $checker)
48
    {
49
        $this->checker = $checker;
50
        $this->state   = new CheckerState($checker);
51
    }
52
53
    /**
54
     * Shortcut for creating an instance for a checker class.
55
     *
56
     * @param  string $checkerClass
57
     * @return \Pbmedia\ApiHealth\Checkers\Executor
58
     */
59
    public static function make(string $checkerClass)
60
    {
61
        return new static($checkerClass::create());
62
    }
63
64
    /**
65
     * Returns a boolean wether the checker passes.
66
     *
67
     * @return bool
68
     */
69
    public function passes(): bool
70
    {
71
        if (is_null($this->failed)) {
72
            $this->handle();
73
        }
74
75
        return !$this->failed;
76
    }
77
78
    /**
79
     * Returns a boolean wether the checker fails.
80
     *
81
     * @return bool
82
     */
83
    public function fails(): bool
84
    {
85
        return !$this->passes();
86
    }
87
88
    /**
89
     * Returns the checker.
90
     *
91
     * @return \Pbmedia\ApiHealth\Checkers\Checker
92
     */
93
    public function getChecker(): Checker
94
    {
95
        return $this->checker;
96
    }
97
98
    /**
99
     * Returns the caught exception.
100
     *
101
     * @return \Pbmedia\ApiHealth\Checkers\CheckerHasFailed
102
     */
103
    public function getException(): CheckerHasFailed
104
    {
105
        return $this->exception;
106
    }
107
108
    /**
109
     * Runs the checker, stores the state and lets events take
110
     * care of sending the notifications.
111
     *
112
     * @return $this
113
     */
114
    public function handle()
115
    {
116
        $this->failed = false;
117
118
        try {
119
            $this->checker->run();
120
            $this->state->setToPassing();
121
        } catch (CheckerHasFailed $exception) {
122
            if ($this->state->retryIsAllowed()) {
123
                $this->handleAllowedRetry();
124
            } else {
125
                $this->exception = $exception;
126
                $this->failed    = true;
127
                $this->handleFailedChecker();
128
            }
129
        }
130
131
        return $this;
132
    }
133
134
    /**
135
     * Handler for whenever the checker fails. Stores the state or adds a timestamp
136
     * to the state if the checker previously failed.
137
     *
138
     * @return null
139
     */
140
    private function handleFailedChecker()
141
    {
142
        if ($this->state->exists() && $this->state->isFailing()) {
143
            return $this->state->addFailedTimestamp($this->exception);
144
        }
145
146
        $this->state->setToFailed($this->exception);
147
    }
148
149
    /**
150
     * Adds a retry timestamp to the state of checker or dispaches
151
     * the retry job.
152
     *
153
     * @return null
154
     */
155
    private function handleAllowedRetry()
156
    {
157
        if (!$this->state->exists()) {
158
            $this->state->setToPassing();
159
        }
160
161
        if (!$jobClass = $this->checker->retryJob()) {
162
            return $this->state->addRetryTimestamp();
163
        }
164
165
        $this->addCallbackBeforeRetryCheckerJob($jobClass);
166
167
        $job = new $jobClass(get_class($this->checker));
168
169
        $this->configureRetryJobDefaults($job, config('api-health.retries.job'));
170
171
        if (method_exists($this->checker, 'withRetryJob')) {
172
            $this->checker->withRetryJob($job);
173
        }
174
175
        dispatch($job);
176
    }
177
178
    /**
179
     * Sets the default connection, delay and queue
180
     * on the retry job.
181
     *
182
     * @param  mixed $job
183
     * @param  array  $config
184
     */
185
    private function configureRetryJobDefaults($job, array $config)
186
    {
187
        if ($connection = $config['connection'] ?? null) {
188
            $job->onConnection($connection);
189
        }
190
191
        if ($delay = $config['delay'] ?? null) {
192
            $job->delay($delay);
193
        }
194
195
        if ($queue = $config['queue'] ?? null) {
196
            $job->onQueue($queue);
197
        }
198
    }
199
200
    /**
201
     * Adds a callback before the retry job is executed which
202
     * will add a retry timestamp on the checker's state.
203
     *
204
     * @param string $job
205
     */
206
    private function addCallbackBeforeRetryCheckerJob(string $job)
207
    {
208
        Queue::before(function (JobProcessing $event) use ($job) {
209
            $resolvedName = $event->job->resolveName();
210
211
            $checkerClass = unserialize(
212
                $event->job->payload()['data']['command']
213
            )->checkerClass;
214
215
            if ($resolvedName === $job && $checkerClass === get_class($this->checker)) {
216
                $this->state->addRetryTimestamp();
217
            }
218
        });
219
    }
220
}
221