GdaemonApiAuth::handle()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 2
dl 0
loc 17
ccs 0
cts 9
cp 0
crap 12
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace Gameap\Http\Middleware;
4
5
use Closure;
6
use Gameap\Exceptions\GdaemonAPI\InvalidApiKeyException;
7
use Gameap\Models\DedicatedServer;
8
use Illuminate\Database\Eloquent\ModelNotFoundException;
9
use Symfony\Component\HttpKernel\Exception\HttpException;
10
11
class GdaemonApiAuth
12
{
13
    private $repository;
14
15
    public function __construct(\Gameap\Repositories\NodeRepository $dedicatedServersRepository)
16
    {
17
        $this->repository = $dedicatedServersRepository;
18
    }
19
20
    /**
21
     * Handle an incoming request.
22
     *
23
     * @param  \Illuminate\Http\Request  $request
24
     * @param  \Closure  $next
25
     * @return mixed
26
     */
27
    public function handle($request, Closure $next)
28
    {
29
        $bearerToken = $request->bearerToken();
30
        
31
        if (is_null($bearerToken)) {
32
            throw new HttpException(401, 'Bearer token not set', null, ['WWW-Authenticate' => 'Bearer']);
33
        }
34
35
        try {
36
            $dedicatedServer = DedicatedServer::where('gdaemon_api_key', '=', $bearerToken)->firstOrFail();
37
        } catch (ModelNotFoundException $exception) {
38
            throw new InvalidApiKeyException('Invalid api token');
39
        }
40
41
        app()->instance(DedicatedServer::class, $dedicatedServer);
42
        
43
        return $next($request);
44
    }
45
}
46