ApiContredanseProfileHandler::handle()   A
last analyzed

Complexity

Conditions 5
Paths 13

Size

Total Lines 42
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 5
eloc 30
c 2
b 0
f 1
nc 13
nop 1
dl 0
loc 42
ccs 0
cts 37
cp 0
crap 30
rs 9.1288
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Handler;
6
7
use App\Middleware\AuthTokenMiddleware;
8
use App\Security\Exception\UserNotFoundException;
9
use App\Security\UserProviderInterface;
10
use App\Service\Token\TokenManager;
11
use Fig\Http\Message\StatusCodeInterface;
12
use Lcobucci\JWT\Token;
13
use Psr\Http\Message\ResponseInterface;
14
use Psr\Http\Message\ServerRequestInterface;
15
use Psr\Http\Server\RequestHandlerInterface;
16
use Zend\Diactoros\Response\JsonResponse;
17
18
class ApiContredanseProfileHandler implements RequestHandlerInterface
19
{
20
    /**
21
     * @var UserProviderInterface
22
     */
23
    private $userProvider;
24
25
    /**
26
     * @var TokenManager
27
     */
28
    private $tokenManager;
29
30
    public function __construct(TokenManager $tokenManager, UserProviderInterface $userProvider)
31
    {
32
        $this->userProvider = $userProvider;
33
        $this->tokenManager = $tokenManager;
34
    }
35
36
    public function handle(ServerRequestInterface $request): ResponseInterface
37
    {
38
        $token = $request->getAttribute(AuthTokenMiddleware::class);
39
        if (!$token instanceof Token) {
40
            return (new JsonResponse([
41
                'success' => false,
42
                'reason'  => 'Missing auth middleware attribute',
43
            ]))->withStatus(StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR);
44
        }
45
46
        $user_id = $token->getClaim('user_id', '');
47
48
        try {
49
            // For demo !
50
51
            if ($user_id === '[email protected]') {
52
                $userData = [
53
                    'user_id'   => 'ilovecontredanse.org',
54
                    'firstname' => 'Demo',
55
                    'name'      => 'Demo',
56
                    'email'     => 'ilovecontredanse.org',
57
                ];
58
            } else {
59
                $userData = $this->userProvider->findUser($user_id);
60
            }
61
            $data = [
62
                'success' => true,
63
                'data'    => [
64
                    'user_id'   => $userData['user_id'],
65
                    'firstname' => $userData['firstname'],
66
                    'name'      => $userData['name'],
67
                    'email'     => $userData['email'],
68
                ]
69
            ];
70
71
            return (new JsonResponse($data))->withStatus(StatusCodeInterface::STATUS_OK);
72
        } catch (UserNotFoundException $e) {
73
            return (new JsonResponse(['success' => false, 'reason' => $e->getMessage()]))
74
                ->withStatus(StatusCodeInterface::STATUS_UNAUTHORIZED);
75
        } catch (\Throwable $e) {
76
            return (new JsonResponse(['success' => false, 'reason' => $e->getMessage()]))
77
                ->withStatus(StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR);
78
        }
79
    }
80
}
81