Passed
Push — master ( 49242a...606e42 )
by Derek Stephen
03:08
created

SessionAuth   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 6
eloc 26
c 4
b 0
f 0
dl 0
loc 52
ccs 25
cts 25
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A process() 0 30 5
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 Psr\Http\Message\ResponseInterface;
13
use Psr\Http\Message\ServerRequestInterface;
14
use Psr\Http\Server\MiddlewareInterface;
15
use Psr\Http\Server\RequestHandlerInterface;
16
17
class SessionAuth implements MiddlewareInterface, SessionAwareInterface
18
{
19
    use HasSessionTrait;
20
21
    /** @var UserService $userService */
22
    private $userService;
23
24
    /** @var PasetoService $pasetoService */
25
    private $pasetoService;
26
27 5
    public function __construct(SessionManager $sessionManager, UserService $userService, PasetoService $pasetoService)
28
    {
29 5
        $this->setSession($sessionManager);
30 5
        $this->userService = $userService;
31 5
        $this->pasetoService = $pasetoService;
32 5
    }
33
34
    /**
35
     * @param ServerRequestInterface $request
36
     * @param RequestHandlerInterface $handler
37
     * @return ResponseInterface
38
     */
39 3
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
40
    {
41 3
        $cookies = $request->getCookieParams();
42 3
        $id = $this->getSession()->get('user');
43
44 3
        if (!$id && isset($cookies['resu'])) {
45 1
            $string = $cookies['resu'];
46 1
            $token = $this->pasetoService->decryptToken($string);
47 1
            $id = $token->getClaims()['user'];
48 1
            $this->getSession()->set('user', $id);
49
        }
50
51 3
        if ($id) {
52 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

52
            $user = $this->userService->findUserById(/** @scrutinizer ignore-type */ $id);
Loading history...
53 2
            $request = $request->withAttribute('user', $user);
54 2
            $response = $handler->handle($request);
55
56 2
            if ($response instanceof Response) {
57 2
                $response->setAttribute('user', $user);
58
            }
59
60 2
            $person = $user->getPerson();
61 2
            $person = $this->userService->getPersonSvc()->toArray($person);
62 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

62
            $userArray = $this->userService->toArray(/** @scrutinizer ignore-type */ $user);
Loading history...
63 2
            $userArray['person'] = $person;
64
65 2
            return $response->withHeader('user', json_encode($userArray));
66
        }
67
68 1
        throw new UserException(UserException::UNAUTHORISED, 401);
69
    }
70
}