Completed
Push — master ( 298ac7...0024da )
by Oleg
12:58
created

GetTokenAction::process()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 37
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 37
ccs 0
cts 35
cp 0
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 28
nc 6
nop 2
crap 20
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
    public function __construct(
36
        TokenManagerInterface $tokenManager,
37
        ExtractionInterface $extraction,
38
        InputFilterInterface $inputFilter
39
    ) {
40
        $this->tokenManager = $tokenManager;
41
        $this->extraction = $extraction;
42
        $this->inputFilter = $inputFilter;
43
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
49
    {
50
        $data = $request->getParsedBody();
51
        $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
        if ($this->inputFilter->isValid()) {
54
            try {
55
                $token = $this->tokenManager->getToken($data['user'], $data['password'], $data['resources']);
56
                return new JsonResponse([
57
                    'data' => [
58
                        'token' => $this->extraction->extract($token),
59
                        'validation' => [],
60
                    ],
61
                    'success' => true,
62
                    'msg' => new SuccessMessage('Token successfully creaeted'),
63
                ], 200);
64
            } catch (InvalidCredentialsException $exception) {
65
                $status = 401;
66
                $msg = new DangerMessage(
67
                    'Invalid credentials provided. Please double check your user and password.'
68
                );
69
            } catch (PermissionDeniedException $exception) {
70
                $status = 403;
71
                $msg = new DangerMessage('Provided user does not have permission to access requested resources.');
72
            }
73
        } else {
74
            return (new ValidationResponseFactory())('token', $this->inputFilter);
75
        }
76
77
        return new JsonResponse([
78
            'data' => [
79
                'token' => null,
80
            ],
81
            'success' => false,
82
            'msg' => $msg,
83
        ], $status);
84
    }
85
}
86