Completed
Push — master ( 9ab4b9...065cdd )
by Alejandro
09:44
created

AuthenticateAction   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 65
Duplicated Lines 21.54 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 14
loc 65
ccs 21
cts 21
cp 1
rs 10
wmc 4
lcom 1
cbo 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B dispatch() 14 25 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace Shlinkio\Shlink\Rest\Action;
3
4
use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
5
use Firebase\JWT\JWT;
6
use Psr\Http\Message\ResponseInterface as Response;
7
use Psr\Http\Message\ServerRequestInterface as Request;
8
use Shlinkio\Shlink\Rest\Authentication\JWTService;
9
use Shlinkio\Shlink\Rest\Authentication\JWTServiceInterface;
10
use Shlinkio\Shlink\Rest\Service\ApiKeyService;
11
use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface;
12
use Shlinkio\Shlink\Rest\Util\RestUtils;
13
use Zend\Diactoros\Response\JsonResponse;
14
use Zend\I18n\Translator\TranslatorInterface;
15
16
class AuthenticateAction extends AbstractRestAction
17
{
18
    /**
19
     * @var TranslatorInterface
20
     */
21
    private $translator;
22
    /**
23
     * @var ApiKeyService|ApiKeyServiceInterface
24
     */
25
    private $apiKeyService;
26
    /**
27
     * @var JWTServiceInterface
28
     */
29
    private $jwtService;
30
31
    /**
32
     * AuthenticateAction constructor.
33
     * @param ApiKeyServiceInterface|ApiKeyService $apiKeyService
34
     * @param JWTServiceInterface|JWTService $jwtService
35
     * @param TranslatorInterface $translator
36
     *
37
     * @Inject({ApiKeyService::class, JWTService::class, "translator"})
38
     */
39 3
    public function __construct(
40
        ApiKeyServiceInterface $apiKeyService,
41
        JWTServiceInterface $jwtService,
42
        TranslatorInterface $translator
43
    ) {
44 3
        $this->translator = $translator;
45 3
        $this->apiKeyService = $apiKeyService;
46 3
        $this->jwtService = $jwtService;
47 3
    }
48
49
    /**
50
     * @param Request $request
51
     * @param Response $response
52
     * @param callable|null $out
53
     * @return null|Response
54
     */
55 3
    public function dispatch(Request $request, Response $response, callable $out = null)
56
    {
57 3
        $authData = $request->getParsedBody();
58 3 View Code Duplication
        if (! isset($authData['apiKey'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59 1
            return new JsonResponse([
60 1
                'error' => RestUtils::INVALID_ARGUMENT_ERROR,
61 1
                'message' => $this->translator->translate(
62
                    'You have to provide a valid API key under the "apiKey" param name.'
63 1
                ),
64 1
            ], 400);
65
        }
66
67
        // Authenticate using provided API key
68 2
        $apiKey = $this->apiKeyService->getByKey($authData['apiKey']);
69 2 View Code Duplication
        if (! $apiKey->isValid()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70 1
            return new JsonResponse([
71 1
                'error' => RestUtils::INVALID_API_KEY_ERROR,
72 1
                'message' => $this->translator->translate('Provided API key does not exist or is invalid.'),
73 1
            ], 401);
74
        }
75
76
        // Generate a JSON Web Token that will be used for authorization in next requests
77 1
        $token = $this->jwtService->create($apiKey);
78 1
        return new JsonResponse(['token' => $token]);
79
    }
80
}
81