Passed
Push — master ( baf006...043cba )
by Derek Stephen
02:43
created

SessionAuth::process()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 35
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 6.027

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 22
c 5
b 0
f 0
dl 0
loc 35
ccs 20
cts 22
cp 0.9091
rs 8.9457
cc 6
nc 9
nop 2
crap 6.027
1
<?php
2
3
namespace Bone\User\Http\Middleware;
4
5
use Bone\Http\Response;
6
use Bone\Paseto\PasetoService;
7
use Bone\Server\SessionAwareInterface;
8
use Bone\Server\Traits\HasSessionTrait;
9
use Del\Exception\UserException;
10
use Del\Service\UserService;
11
use Del\SessionManager;
12
use Exception;
13
use Laminas\Diactoros\Response\RedirectResponse;
14
use Psr\Http\Message\ResponseInterface;
15
use Psr\Http\Message\ServerRequestInterface;
16
use Psr\Http\Server\MiddlewareInterface;
17
use Psr\Http\Server\RequestHandlerInterface;
18
19
class SessionAuth implements MiddlewareInterface, SessionAwareInterface
20
{
21
    use HasSessionTrait;
22
23
    /** @var UserService $userService */
24
    private $userService;
25
26
    /** @var PasetoService $pasetoService */
27
    private $pasetoService;
28
29
    /** @var string $redirectUrl */
30
    private $redirectUrl;
31
32 5
    public function __construct(SessionManager $sessionManager, UserService $userService, PasetoService $pasetoService, $redirectUrl = '/user/home')
33
    {
34 5
        $this->setSession($sessionManager);
35 5
        $this->userService = $userService;
36 5
        $this->pasetoService = $pasetoService;
37 5
        $this->redirectUrl = $redirectUrl;
38 5
    }
39
40
    /**
41
     * @param ServerRequestInterface $request
42
     * @param RequestHandlerInterface $handler
43
     * @return ResponseInterface
44
     */
45 3
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
46
    {
47 3
        $cookies = $request->getCookieParams();
48 3
        $id = $this->getSession()->get('user');
49
50 3
        if (!$id && isset($cookies['resu'])) {
51 1
            $string = $cookies['resu'];
52
53
            try {
54 1
                $token = $this->pasetoService->decryptToken($string);
55 1
                $id = $token->getClaims()['user'];
56 1
                $this->getSession()->set('user', $id);
57
            } catch (Exception $e) {
58
                return new RedirectResponse($this->redirectUrl);
59
            }
60
        }
61
62 3
        if ($id) {
63 2
            $user = $this->userService->findUserById($id);
0 ignored issues
show
Bug introduced by
It seems like $id can also be of type string; however, parameter $id of Del\Service\UserService::findUserById() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

63
            $user = $this->userService->findUserById(/** @scrutinizer ignore-type */ $id);
Loading history...
64 2
            $request = $request->withAttribute('user', $user);
65 2
            $response = $handler->handle($request);
66
67 2
            if ($response instanceof Response) {
68 2
                $response->setAttribute('user', $user);
69
            }
70
71 2
            $person = $user->getPerson();
72 2
            $person = $this->userService->getPersonSvc()->toArray($person);
73 2
            $userArray = $this->userService->toArray($user);
0 ignored issues
show
Bug introduced by
It seems like $user can also be of type null; however, parameter $user of Del\Service\UserService::toArray() does only seem to accept Del\Entity\UserInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
            $userArray = $this->userService->toArray(/** @scrutinizer ignore-type */ $user);
Loading history...
74 2
            $userArray['person'] = $person;
75
76 2
            return $response->withHeader('user', json_encode($userArray));
77
        }
78
79 1
        throw new UserException(UserException::UNAUTHORISED, 401);
80
    }
81
}