Completed
Push — master ( 178a08...5c0b6f )
by Oleg
05:22
created

ActivePasswordMiddleware::process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 8
cts 8
cp 1
rs 9.7
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DataFlowServer\Authentication\Middleware;
5
6
use Doctrine\Common\Collections\Criteria;
7
use Doctrine\ORM\EntityManager;
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 SlayerBirden\DataFlowServer\Authentication\Entities\Password;
13
use SlayerBirden\DataFlowServer\Domain\Entities\User;
14
use SlayerBirden\DataFlowServer\Notification\DangerMessage;
15
use Zend\Diactoros\Response\JsonResponse;
16
17
class ActivePasswordMiddleware implements MiddlewareInterface
18
{
19
    /**
20
     * @var EntityManager
21
     */
22
    private $entityManager;
23
24 4
    public function __construct(EntityManager $entityManager)
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...
25
    {
26 4
        $this->entityManager = $entityManager;
27 4
    }
28
29
    /**
30
     * @inheritdoc
31
     */
32 4
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
33
    {
34 4
        $user = $request->getAttribute(TokenMiddleware::USER_PARAM);
35
36 4
        if ($this->hasActivePassword($user)) {
37 1
            return new JsonResponse([
38 1
                'data' => [
39
                    'validation' => [],
40
                    'password' => null,
41
                ],
42
                'success' => false,
43 1
                'msg' => new DangerMessage('You already have an active password. Please use "update" routine.'),
44 1
            ], 412);
45
        }
46
47 3
        return $handler->handle($request);
48
    }
49
50 4
    private function hasActivePassword(User $user): bool
51
    {
52 4
        $collection = $this->entityManager
53 4
            ->getRepository(Password::class)
54 4
            ->matching(
55 4
                Criteria::create()
56 4
                    ->where(Criteria::expr()->eq('owner', $user))
57 4
                    ->andWhere(Criteria::expr()->eq('active', true))
58
            );
59
60 4
        return !$collection->isEmpty();
61
    }
62
}
63