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

InvalidateTokenAction::process()   B

Complexity

Conditions 2
Paths 5

Size

Total Lines 24
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 0
cts 23
cp 0
rs 8.9713
c 0
b 0
f 0
cc 2
eloc 19
nc 5
nop 2
crap 6
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DataFlowServer\Authentication\Controller;
5
6
use Doctrine\ORM\EntityManager;
7
use Doctrine\ORM\ORMException;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Psr\Http\Server\MiddlewareInterface;
11
use Psr\Http\Server\RequestHandlerInterface;
12
use Psr\Log\LoggerInterface;
13
use SlayerBirden\DataFlowServer\Doctrine\Middleware\ResourceMiddlewareInterface;
14
use SlayerBirden\DataFlowServer\Notification\DangerMessage;
15
use SlayerBirden\DataFlowServer\Notification\SuccessMessage;
16
use Zend\Diactoros\Response\JsonResponse;
17
use Zend\Hydrator\ExtractionInterface;
18
19
class InvalidateTokenAction implements MiddlewareInterface
20
{
21
    /**
22
     * @var EntityManager
23
     */
24
    private $entityManager;
25
    /**
26
     * @var LoggerInterface
27
     */
28
    private $logger;
29
    /**
30
     * @var ExtractionInterface
31
     */
32
    private $extraction;
33
34
    public function __construct(EntityManager $entityManager, LoggerInterface $logger, ExtractionInterface $extraction)
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $entityManager. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
35
    {
36
        $this->entityManager = $entityManager;
37
        $this->logger = $logger;
38
        $this->extraction = $extraction;
39
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
45
    {
46
        $token = $request->getAttribute(ResourceMiddlewareInterface::DATA_RESOURCE);
47
        try {
48
            $token->setActive(false);
49
50
            $this->entityManager->persist($token);
51
            $this->entityManager->flush();
52
            return new JsonResponse([
53
                'data' => [
54
                    'token' => $this->extraction->extract($token),
55
                ],
56
                'success' => true,
57
                'msg' => new SuccessMessage('Token invalidated.'),
58
            ], 200);
59
        } catch (ORMException $exception) {
60
            $this->logger->error((string)$exception);
61
            return new JsonResponse([
62
                'data' => [],
63
                'success' => false,
64
                'msg' => new DangerMessage('There was an error while invalidating token.'),
65
            ], 400);
66
        }
67
    }
68
}
69