Completed
Push — master ( 884747...1c7964 )
by ABDULMALIK
11s
created

BaseComponent::saveEntity()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 3
nop 1
1
<?php
2
3
namespace Potievdev\SlimRbac\Component;
4
5
use Doctrine\ORM\EntityManager;
6
use Doctrine\ORM\OptimisticLockException;
7
use Potievdev\SlimRbac\Exception\DatabaseException;
8
use Potievdev\SlimRbac\Exception\InvalidArgumentException;
9
use Potievdev\SlimRbac\Helper\ValidatorHelper;
10
use Potievdev\SlimRbac\Models\RepositoryRegistry;
11
use Potievdev\SlimRbac\Structure\AuthOptions;
12
13
/**
14
 * Class BaseComponent
15
 * @package Potievdev\SlimRbac\Component
16
 */
17
class BaseComponent
18
{
19
    /** @var  AuthOptions $authOptions */
20
    protected $authOptions;
21
22
    /** @var  EntityManager $entityManager */
23
    protected $entityManager;
24
25
    /** @var  RepositoryRegistry $repositoryRegistry */
26
    protected $repositoryRegistry;
27
28
    /**
29
     * AuthManager constructor.
30
     * @param AuthOptions $authOptions
31
     */
32
    public function __construct(AuthOptions $authOptions)
33
    {
34
        $this->authOptions = $authOptions;
35
        $this->entityManager = $authOptions->getEntityManager();
36
        $this->repositoryRegistry = new RepositoryRegistry($this->entityManager);
37
    }
38
39
    /**
40
     * Insert or update entity
41
     * @param  object $entity
42
     * @return object
43
     * @throws DatabaseException
44
     */
45
    protected function saveEntity($entity)
46
    {
47
        try {
48
            $this->entityManager->persist($entity);
49
            $this->entityManager->flush($entity);
50
            return $entity;
51
        } catch (OptimisticLockException $e) {
52
            throw new DatabaseException($e->getMessage());
53
        }
54
    }
55
56
    /**
57
     * Checks access status
58
     * @param integer $userId
59
     * @param string $permissionName
60
     * @return bool
61
     * @throws InvalidArgumentException
62
     * @throws \Doctrine\ORM\Query\QueryException
63
     */
64
    public function checkAccess($userId, $permissionName)
65
    {
66
        if (ValidatorHelper::isInteger($userId) == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
67
            throw new InvalidArgumentException('User identifier must be number.');
68
        }
69
70
        /** @var integer $permissionId */
71
        $permissionId = $this->repositoryRegistry
72
            ->getPermissionRepository()
73
            ->getPermissionIdByName($permissionName);
74
75
        if (ValidatorHelper::isInteger($permissionId)) {
76
77
            /** @var integer[] $rootRoleIds */
78
            $rootRoleIds = $this->repositoryRegistry
79
                ->getUserRoleRepository()
80
                ->getUserRoleIds($userId);
81
82
            if (count($rootRoleIds) > 0) {
83
84
                /** @var integer[] $allRoleIds */
85
                $allRoleIds = $this->repositoryRegistry
86
                    ->getRoleHierarchyRepository()
87
                    ->getAllRoleIdsHierarchy($rootRoleIds);
88
89
                return $this->repositoryRegistry
90
                    ->getRolePermissionRepository()
91
                    ->isPermissionAssigned($permissionId, $allRoleIds);
92
            }
93
        }
94
95
        return false;
96
    }
97
}
98