Completed
Push — master ( 178a08...5c0b6f )
by Oleg
05:22
created

GetTokenAction   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 8
dl 0
loc 72
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A process() 0 43 4
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DataFlowServer\Authentication\Controller;
5
6
use Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
use Psr\Http\Server\MiddlewareInterface;
9
use Psr\Http\Server\RequestHandlerInterface;
10
use SlayerBirden\DataFlowServer\Authentication\Exception\InvalidCredentialsException;
11
use SlayerBirden\DataFlowServer\Authentication\Exception\PermissionDeniedException;
12
use SlayerBirden\DataFlowServer\Authentication\TokenManagerInterface;
13
use SlayerBirden\DataFlowServer\Notification\DangerMessage;
14
use SlayerBirden\DataFlowServer\Notification\SuccessMessage;
15
use SlayerBirden\DataFlowServer\Stdlib\Validation\ValidationResponseFactory;
16
use Zend\Diactoros\Response\JsonResponse;
17
use Zend\Hydrator\ExtractionInterface;
18
use Zend\InputFilter\InputFilterInterface;
19
20
class GetTokenAction implements MiddlewareInterface
21
{
22
    /**
23
     * @var TokenManagerInterface
24
     */
25
    private $tokenManager;
26
    /**
27
     * @var ExtractionInterface
28
     */
29
    private $extraction;
30
    /**
31
     * @var InputFilterInterface
32
     */
33
    private $inputFilter;
34
35 4
    public function __construct(
36
        TokenManagerInterface $tokenManager,
37
        ExtractionInterface $extraction,
38
        InputFilterInterface $inputFilter
39
    ) {
40 4
        $this->tokenManager = $tokenManager;
41 4
        $this->extraction = $extraction;
42 4
        $this->inputFilter = $inputFilter;
43 4
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48 4
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
49
    {
50 4
        $data = $request->getParsedBody();
51 4
        $this->inputFilter->setData($data);
0 ignored issues
show
Bug introduced by
It seems like $data defined by $request->getParsedBody() on line 50 can also be of type null or object; however, Zend\InputFilter\InputFilterInterface::setData() does only seem to accept array|object<Traversable>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
52
53 4
        if (!$this->inputFilter->isValid()) {
54 1
            return (new ValidationResponseFactory())('token', $this->inputFilter);
55
        }
56
57
        try {
58 3
            $token = $this->tokenManager->getToken($data['user'], $data['password'], $data['resources']);
59 1
            return new JsonResponse([
60 1
                'data' => [
61 1
                    'token' => $this->extraction->extract($token),
62
                    'validation' => [],
63
                ],
64
                'success' => true,
65 1
                'msg' => new SuccessMessage('Token successfully creaeted'),
66 1
            ], 200);
67 2
        } catch (InvalidCredentialsException $exception) {
68 1
            return new JsonResponse([
69 1
                'data' => [
70
                    'token' => null,
71
                    'validation' => [],
72
                ],
73
                'success' => false,
74 1
                'msg' => new DangerMessage(
75 1
                    'Invalid credentials provided. Please double check your user and password.'
76
                ),
77 1
            ], 401);
78 1
        } catch (PermissionDeniedException $exception) {
79 1
            return new JsonResponse([
80 1
                'data' => [
81
                    'token' => null,
82
                    'validation' => [],
83
                ],
84
                'success' => false,
85 1
                'msg' => new DangerMessage(
86 1
                    'Provided user does not have permission to access requested resources.'
87
                ),
88 1
            ], 403);
89
        }
90
    }
91
}
92