VerifyGdaemonApiToken   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
eloc 12
dl 0
loc 34
ccs 0
cts 12
cp 0
rs 10
c 0
b 0
f 0

2 Methods

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