InitializeKernel::resolveChannel()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
c 0
b 0
f 0
ccs 0
cts 4
cp 0
rs 10
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Foundation\Middleware;
6
7
use Closure;
8
use FondBot\FondBot;
9
use Illuminate\Http\Request;
10
use FondBot\Channels\Channel;
11
use FondBot\Channels\ChannelManager;
12
13
class InitializeKernel
14
{
15
    private $kernel;
16
    private $channelManager;
17
18
    public function __construct(FondBot $kernel, ChannelManager $channelManager)
19
    {
20
        $this->kernel = $kernel;
21
        $this->channelManager = $channelManager;
22
    }
23
24
    public function handle(Request $request, Closure $next)
25
    {
26
        $channel = $this->resolveChannel($request->route('channel'));
27
28
        if ($channel->getSecret() !== null && $request->route('secret') !== $channel->getSecret()) {
29
            abort(403);
30
        }
31
32
        $this->kernel->initialize($channel);
33
34
        return $next($request);
35
    }
36
37
    private function resolveChannel($value): Channel
38
    {
39
        if (is_string($value)) {
40
            $value = $this->channelManager->create($value);
41
        }
42
43
        return $value;
44
    }
45
}
46