SessionAuth::process()   B
last analyzed

Complexity

Conditions 6
Paths 9

Size

Total Lines 35
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 6

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 22
c 5
b 0
f 0
dl 0
loc 35
ccs 22
cts 22
cp 1
rs 8.9457
cc 6
nc 9
nop 2
crap 6
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 6
    public function __construct(SessionManager $sessionManager, UserService $userService, PasetoService $pasetoService, $redirectUrl = '/user/home')
33
    {
34 6
        $this->setSession($sessionManager);
35 6
        $this->userService = $userService;
36 6
        $this->pasetoService = $pasetoService;
37 6
        $this->redirectUrl = $redirectUrl;
38
    }
39
40
    /**
41
     * @param ServerRequestInterface $request
42
     * @param RequestHandlerInterface $handler
43
     * @return ResponseInterface
44
     */
45 4
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
46
    {
47 4
        $cookies = $request->getCookieParams();
48 4
        $id = $this->getSession()->get('user');
49
50 4
        if (!$id && isset($cookies['resu'])) {
51 2
            $string = $cookies['resu'];
52
53
            try {
54 2
                $token = $this->pasetoService->decryptToken($string);
55 1
                $id = $token->getClaims()['user'];
56 1
                $this->getSession()->set('user', $id);
57 1
            } catch (Exception $e) {
58 1
                return new RedirectResponse($this->redirectUrl);
59
            }
60
        }
61
62 3
        if ($id) {
63 2
            $user = $this->userService->findUserById($id);
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);
0 ignored issues
show
Deprecated Code introduced by
The function Del\Service\UserService::getPersonSvc() has been deprecated: use getPersonService() instead ( Ignorable by Annotation )

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

72
            $person = /** @scrutinizer ignore-deprecated */ $this->userService->getPersonSvc()->toArray($person);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
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));
0 ignored issues
show
Bug Best Practice introduced by
The expression return $response->withHe...son_encode($userArray)) returns the type Psr\Http\Message\MessageInterface which includes types incompatible with the type-hinted return Psr\Http\Message\ResponseInterface.
Loading history...
77
        }
78
79 1
        throw new UserException(UserException::UNAUTHORISED, 401);
80
    }
81
}