InvalidateTokensAction   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 9
dl 0
loc 87
ccs 32
cts 32
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A process() 0 7 1
A invalidate() 0 24 4
A getUsers() 0 7 1
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DataFlowServer\Authentication\Controller;
5
6
use Doctrine\Common\Collections\Criteria;
7
use Doctrine\Common\Collections\Selectable;
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\Authentication\Entities\Token;
14
use SlayerBirden\DataFlowServer\Doctrine\Hydrator\ListExtractor;
15
use SlayerBirden\DataFlowServer\Doctrine\Persistence\EntityManagerRegistry;
16
use SlayerBirden\DataFlowServer\Stdlib\Request\Parser;
17
use SlayerBirden\DataFlowServer\Stdlib\ResponseFactory;
18
use Zend\Hydrator\HydratorInterface;
19
20
final class InvalidateTokensAction implements MiddlewareInterface
21
{
22
    /**
23
     * @var LoggerInterface
24
     */
25
    private $logger;
26
    /**
27
     * @var HydratorInterface
28
     */
29
    private $hydrator;
30
    /**
31
     * @var EntityManagerRegistry
32
     */
33
    private $managerRegistry;
34
    /**
35
     * @var Selectable
36
     */
37
    private $tokenRepository;
38
    /**
39
     * @var Selectable
40
     */
41
    private $userRepository;
42
43 6
    public function __construct(
44
        EntityManagerRegistry $managerRegistry,
45
        Selectable $tokenRepository,
46
        Selectable $userRepository,
47
        LoggerInterface $logger,
48
        HydratorInterface $hydrator
49
    ) {
50 6
        $this->managerRegistry = $managerRegistry;
51 6
        $this->tokenRepository = $tokenRepository;
52 6
        $this->userRepository = $userRepository;
53 6
        $this->logger = $logger;
54 6
        $this->hydrator = $hydrator;
55 6
    }
56
57
    /**
58
     * {@inheritdoc}
59
     * @throws \Doctrine\ORM\ORMException
60
     */
61 6
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
62
    {
63 6
        $data = Parser::getRequestBody($request);
64
65 6
        $users = $data['users'] ?? [];
66 6
        return $this->invalidate($users);
67
    }
68
69
    /**
70
     * @param array $users
71
     * @return ResponseInterface
72
     * @throws \Doctrine\ORM\ORMException
73
     */
74 6
    private function invalidate(array $users = []): ResponseInterface
75
    {
76 6
        $criteria = Criteria::create();
77 6
        $criteria->where(Criteria::expr()->eq('active', true));
78 6
        if (!empty($users)) {
79 4
            $criteria->andWhere(Criteria::expr()->in('owner', $this->getUsers($users)));
80
        }
81
82 6
        $collection = $this->tokenRepository->matching($criteria);
83
84 6
        if ($collection->count() === 0) {
85 2
            $msg = 'No tokens found to invalidate for given criteria.';
86 2
            return (new ResponseFactory())($msg, 400, 'tokens', [], 0);
87
        }
88 4
        $em = $this->managerRegistry->getManagerForClass(Token::class);
89 4
        foreach ($collection as $token) {
90 4
            $token->setActive(false);
91 4
            $em->persist($token);
92
        }
93 4
        $em->flush();
94 4
        $msg = 'Tokens have been deactivated.';
95 4
        $extracted = (new ListExtractor())($this->hydrator, $collection->toArray());
96 4
        return (new ResponseFactory())($msg, 200, 'tokens', $extracted, $collection->count());
97
    }
98
99 4
    private function getUsers(array $users): array
100
    {
101 4
        $collection = $this->userRepository->matching(
102 4
            Criteria::create()->where(Criteria::expr()->in('id', $users))
103
        );
104 4
        return $collection->toArray();
105
    }
106
}
107