InitializeKernel   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
eloc 13
dl 0
loc 31
c 0
b 0
f 0
ccs 0
cts 14
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A resolveChannel() 0 7 2
A handle() 0 11 3
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