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 ( e97bb7...5f2eb5 )
by Pascal
10:40 queued 09:38
created

Executor::handleAllowedRetry()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

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