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

InvalidateTokensAction   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 97.06%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 10
dl 0
loc 90
ccs 33
cts 34
cp 0.9706
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 10 2
A __construct() 0 13 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\Persistence\EntityManagerRegistry;
15
use SlayerBirden\DataFlowServer\Stdlib\Validation\DataValidationResponseFactory;
16
use SlayerBirden\DataFlowServer\Stdlib\Validation\GeneralErrorResponseFactory;
17
use SlayerBirden\DataFlowServer\Stdlib\Validation\GeneralSuccessResponseFactory;
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 = $request->getParsedBody();
64 6
        if (!is_array($data)) {
65
            return (new DataValidationResponseFactory())('tokens', []);
66
        }
67
68 6
        $users = $data['users'] ?? [];
69 6
        return $this->invalidate($users);
70
    }
71
72
    /**
73
     * @param array $users
74
     * @return ResponseInterface
75
     * @throws \Doctrine\ORM\ORMException
76
     */
77 6
    private function invalidate(array $users = []): ResponseInterface
78
    {
79 6
        $criteria = Criteria::create();
80 6
        $criteria->where(Criteria::expr()->eq('active', true));
81 6
        if (!empty($users)) {
82 4
            $criteria->andWhere(Criteria::expr()->in('owner', $this->getUsers($users)));
83
        }
84
85 6
        $collection = $this->tokenRepository->matching($criteria);
86
87 6
        if ($collection->count() === 0) {
88 2
            $msg = 'No tokens found to invalidate for given criteria.';
89 2
            return (new GeneralErrorResponseFactory())($msg, 'tokens', 400, [], 0);
90
        }
91 4
        $em = $this->managerRegistry->getManagerForClass(Token::class);
92 4
        foreach ($collection as $token) {
93 4
            $token->setActive(false);
94 4
            $em->persist($token);
95
        }
96 4
        $em->flush();
97 4
        $msg = 'Tokens have been deactivated.';
98 4
        $extracted = array_map([$this->hydrator, 'extract'], $collection->toArray());
99 4
        return (new GeneralSuccessResponseFactory())($msg, 'tokens', $extracted, 200, $collection->count());
100
    }
101
102 4
    private function getUsers(array $users): array
103
    {
104 4
        $collection = $this->userRepository->matching(
105 4
            Criteria::create()->where(Criteria::expr()->in('id', $users))
106
        );
107 4
        return $collection->toArray();
108
    }
109
}
110