Completed
Pull Request — master (#98)
by Alejandro
06:29
created

AuthenticateAction   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 68
Duplicated Lines 20.59 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 14
loc 68
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
B process() 14 25 4

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 Interop\Http\ServerMiddleware\DelegateInterface;
6
use Psr\Http\Message\ResponseInterface as Response;
7
use Psr\Http\Message\ServerRequestInterface as Request;
8
use Psr\Log\LoggerInterface;
9
use Shlinkio\Shlink\Rest\Authentication\JWTService;
10
use Shlinkio\Shlink\Rest\Authentication\JWTServiceInterface;
11
use Shlinkio\Shlink\Rest\Service\ApiKeyService;
12
use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface;
13
use Shlinkio\Shlink\Rest\Util\RestUtils;
14
use Zend\Diactoros\Response\JsonResponse;
15
use Zend\I18n\Translator\TranslatorInterface;
16
17
class AuthenticateAction extends AbstractRestAction
18
{
19
    /**
20
     * @var TranslatorInterface
21
     */
22
    private $translator;
23
    /**
24
     * @var ApiKeyService|ApiKeyServiceInterface
25
     */
26
    private $apiKeyService;
27
    /**
28
     * @var JWTServiceInterface
29
     */
30
    private $jwtService;
31
32
    /**
33
     * AuthenticateAction constructor.
34
     * @param ApiKeyServiceInterface|ApiKeyService $apiKeyService
35
     * @param JWTServiceInterface|JWTService $jwtService
36
     * @param TranslatorInterface $translator
37
     * @param LoggerInterface|null $logger
38
     *
39
     * @Inject({ApiKeyService::class, JWTService::class, "translator", "Logger_Shlink"})
40
     */
41 3
    public function __construct(
42
        ApiKeyServiceInterface $apiKeyService,
43
        JWTServiceInterface $jwtService,
44
        TranslatorInterface $translator,
45
        LoggerInterface $logger = null
46
    ) {
47 3
        parent::__construct($logger);
48 3
        $this->translator = $translator;
49 3
        $this->apiKeyService = $apiKeyService;
50 3
        $this->jwtService = $jwtService;
51 3
    }
52
53
    /**
54
     * @param Request $request
55
     * @param DelegateInterface $delegate
56
     * @return null|Response
57
     * @throws \InvalidArgumentException
58
     */
59 3
    public function process(Request $request, DelegateInterface $delegate)
60
    {
61 3
        $authData = $request->getParsedBody();
62 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...
63 1
            return new JsonResponse([
64 1
                'error' => RestUtils::INVALID_ARGUMENT_ERROR,
65 1
                'message' => $this->translator->translate(
66 1
                    'You have to provide a valid API key under the "apiKey" param name.'
67
                ),
68 1
            ], self::STATUS_BAD_REQUEST);
69
        }
70
71
        // Authenticate using provided API key
72 2
        $apiKey = $this->apiKeyService->getByKey($authData['apiKey']);
73 2 View Code Duplication
        if (! isset($apiKey) || ! $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...
74 1
            return new JsonResponse([
75 1
                'error' => RestUtils::INVALID_API_KEY_ERROR,
76 1
                'message' => $this->translator->translate('Provided API key does not exist or is invalid.'),
77 1
            ], self::STATUS_UNAUTHORIZED);
78
        }
79
80
        // Generate a JSON Web Token that will be used for authorization in next requests
81 1
        $token = $this->jwtService->create($apiKey);
82 1
        return new JsonResponse(['token' => $token]);
83
    }
84
}
85