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

HistoryManagement::fromPermission()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 0
cts 21
cp 0
rs 8.9713
c 0
b 0
f 0
cc 3
eloc 18
nc 3
nop 1
crap 12
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DataFlowServer\Authorization\Service;
5
6
use Doctrine\ORM\EntityManager;
7
use Doctrine\ORM\UnitOfWork;
8
use SlayerBirden\DataFlowServer\Authorization\Entities\History;
9
use SlayerBirden\DataFlowServer\Authorization\Entities\Permission;
10
use SlayerBirden\DataFlowServer\Authorization\HistoryManagementInterface;
11
12
class HistoryManagement implements HistoryManagementInterface
13
{
14
    const ACTION_ADD = 'added';
15
    const ACTION_REMOVE = 'removed';
16
    const ACTION_UNKNOWN = 'unknown';
17
18
    /**
19
     * @var EntityManager
20
     */
21
    private $entityManager;
22
23
    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...
24
    {
25
        $this->entityManager = $entityManager;
26
    }
27
28
    public function fromPermission(Permission $permission): History
29
    {
30
        $state = $this->entityManager->getUnitOfWork()->getEntityState($permission);
31
32
        switch ($state) {
33
            case UnitOfWork::STATE_MANAGED:
34
                $action = self::ACTION_ADD;
35
                break;
36
            case UnitOfWork::STATE_REMOVED:
37
                $action = self::ACTION_REMOVE;
38
                break;
39
            default:
40
                $action = self::ACTION_UNKNOWN;
41
        }
42
43
        $history = new History();
44
        $history->setChangeAction($action);
45
        $history->setPermission($permission);
46
        $history->setResource($permission->getResource());
47
        $history->setUser($permission->getUser());
48
        $history->setAt(new \DateTime());
49
50
        return $history;
51
    }
52
}
53