PermissionManager::getClass()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Eziat\PermissionBundle\Manager\Doctrine;
6
7
use Doctrine\Common\Persistence\ObjectManager;
8
use Eziat\PermissionBundle\Manager\PermissionManager as BasePermissionManager;
9
use Eziat\PermissionBundle\Model\PermissionInterface;
10
11
/**
12
 * @author Tomas Jakl <[email protected]>
13
 */
14
class PermissionManager extends BasePermissionManager
15
{
16
    /**
17
     * @var ObjectManager
18
     */
19
    protected $objectManager;
20
21
    /**
22
     * @var string
23
     */
24
    private $class;
25
26
    public function __construct(ObjectManager $om, string $class)
27
    {
28
        $this->objectManager = $om;
29
        $this->class         = $class;
30
    }
31
32
    /**
33
     * Finds one permission by the given criteria.
34
     */
35
    public function findPermissionBy(array $criteria)
36
    {
37
        return $this->objectManager->getRepository($this->getClass())->findOneBy($criteria);
38
    }
39
40
    /**
41
     * Returns the permission's fully qualified class name.
42
     */
43
    public function findPermissionsBy(array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null) : array
44
    {
45
        return $this->objectManager->getRepository($this->getClass())->findBy($criteria);
46
    }
47
48
    /**
49
     * Updates an permission.
50
     */
51
    public function updatePermission(PermissionInterface $permission, ?bool $andFlush = true)
52
    {
53
        $this->objectManager->persist($permission);
54
        if ( $andFlush === true){
55
            $this->objectManager->flush();
56
        }
57
    }
58
59
    /**
60
     * Deletes an permission.
61
     */
62
    public function deletePermission(PermissionInterface $permission, ?bool $andFlush = true)
63
    {
64
        $this->objectManager->remove($permission);
65
        $this->objectManager->flush();
66
    }
67
68
    /**
69
     * Returns the permission's fully qualified class name.
70
     */
71
    public function getClass() : string
72
    {
73
        if (false !== strpos($this->class, ':')) {
74
            $metadata    = $this->objectManager->getClassMetadata($this->class);
75
            $this->class = $metadata->getName();
76
        }
77
78
        return $this->class;
79
    }
80
}