Completed
Push — master ( 3bc7c8...a199f7 )
by Oleg
03:54
created

InvalidateTokenAction   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 6
dl 0
loc 42
ccs 13
cts 13
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 12 1
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 Psr\Log\LoggerInterface;
11
use SlayerBirden\DataFlowServer\Authentication\Entities\Token;
12
use SlayerBirden\DataFlowServer\Doctrine\Middleware\ResourceMiddlewareInterface;
13
use SlayerBirden\DataFlowServer\Doctrine\Persistence\EntityManagerRegistry;
14
use SlayerBirden\DataFlowServer\Stdlib\Validation\GeneralSuccessResponseFactory;
15
use Zend\Hydrator\HydratorInterface;
16
17
final class InvalidateTokenAction implements MiddlewareInterface
18
{
19
    /**
20
     * @var LoggerInterface
21
     */
22
    private $logger;
23
    /**
24
     * @var HydratorInterface
25
     */
26
    private $hydrator;
27
    /**
28
     * @var EntityManagerRegistry
29
     */
30
    private $managerRegistry;
31
32 2
    public function __construct(
33
        EntityManagerRegistry $managerRegistry,
34
        LoggerInterface $logger,
35
        HydratorInterface $hydrator
36
    ) {
37 2
        $this->managerRegistry = $managerRegistry;
38 2
        $this->logger = $logger;
39 2
        $this->hydrator = $hydrator;
40 2
    }
41
42
    /**
43
     * @inheritdoc
44
     * @throws \Doctrine\ORM\ORMException
45
     */
46 2
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
47
    {
48
        /** @var Token $token */
49 2
        $token = $request->getAttribute(ResourceMiddlewareInterface::DATA_RESOURCE);
50 2
        $token->setActive(false);
51
52 2
        $em = $this->managerRegistry->getManagerForClass(get_class($token));
53 2
        $em->persist($token);
54 2
        $em->flush();
55 2
        $msg = 'Token invalidated.';
56 2
        return (new GeneralSuccessResponseFactory())($msg, 'token', $this->hydrator->extract($token));
57
    }
58
}
59