JwtMiddleware::beforeProcess()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 19
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
nc 5
nop 3
dl 0
loc 19
ccs 0
cts 8
cp 0
crap 20
rs 10
c 2
b 0
f 0
1
<?php
2
3
namespace ByJG\RestServer\Middleware;
4
5
use ByJG\RestServer\Exception\Error401Exception;
6
use ByJG\RestServer\HttpRequest;
7
use ByJG\RestServer\HttpResponse;
8
use ByJG\RestServer\ResponseBag;
9
use ByJG\Util\JwtWrapper;
10
use ByJG\Util\JwtWrapperException;
11
use Exception;
12
13
class JwtMiddleware implements BeforeMiddlewareInterface
14
{
15
16
    protected $ignorePath = [];
17
    protected $jwtWrapper;
18
19
    public function __construct(JwtWrapper $jwtWrapper, $ignorePath = [])
20
    {
21
        $this->jwtWrapper = $jwtWrapper;
22
        $this->ignorePath = $ignorePath;
23
    }
24
25
    /**
26
     * Undocumented function
27
     *
28
     * @param mixed $dispatcherStatus
29
     * @param HttpResponse $response
30
     * @param HttpRequest $request
31
     * @return MiddlewareResult
32
     */
33
    public function beforeProcess(
34
        $dispatcherStatus,
35
        HttpResponse $response,
36
        HttpRequest $request
37
    )
38
    {
39
        foreach ($this->ignorePath as $path) {
40
            if (preg_match("~$path~", $request->getRequestPath())) {
41
                return MiddlewareResult::continue();
42
            }
43
        }
44
45
        try {
46
            $request->appendVars((array)$this->jwtWrapper->extractData());
47
        } catch (JwtWrapperException $ex) {
48
            throw new Error401Exception($ex->getMessage());
49
        }
50
51
        return MiddlewareResult::continue();
52
    }
53
}
54