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