Passed
Push — develop ( 2b0133...275e57 )
by Mario
02:37
created

RoleService::deleteAccess()   B

Complexity

Conditions 8
Paths 5

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 18
nc 5
nop 1
dl 0
loc 29
rs 8.4444
c 0
b 0
f 0
1
<?php
2
3
namespace AppBundle\Service;
4
5
use AppBundle\Entity\Role;
6
use Doctrine\ORM\EntityManager;
7
use Ds\Component\Api\Api\Api;
8
use Ds\Component\Api\Model\Access;
9
use Ds\Component\Api\Model\Permission;
10
use Ds\Component\Api\Query\AccessParameters;
11
use Ds\Component\Discovery\Service\DiscoveryService;
12
use Ds\Component\Entity\Service\EntityService;
13
use LogicException;
14
15
/**
16
 * Class RoleService
17
 */
18
class RoleService extends EntityService
19
{
20
    /**
21
     * @var \Ds\Component\Api\Api\Api
22
     */
23
    protected $api;
24
25
    /**
26
     * @var \Ds\Component\Discovery\Service\DiscoveryService
27
     */
28
    protected $discoveryService;
29
30
    /**
31
     * Constructor
32
     *
33
     * @param \Doctrine\ORM\EntityManager $manager
34
     * @param \Ds\Component\Api\Api\Api $api
35
     * @param \Ds\Component\Discovery\Service\DiscoveryService $discoveryService
36
     * @param string $entity
37
     */
38
    public function __construct(EntityManager $manager, Api $api, DiscoveryService $discoveryService, $entity = Role::class)
39
    {
40
        parent::__construct($manager, $entity);
41
        $this->api = $api;
42
        $this->discoveryService = $discoveryService;
43
    }
44
45
    /**
46
     * Create access across all microservices
47
     *
48
     * @param \AppBundle\Entity\Role $role
49
     * @return \AppBundle\Service\RoleService
50
     * @throws \LogicException
51
     */
52
    public function createAccess(Role $role)
53
    {
54
        if (null === $role->getUuid()) {
0 ignored issues
show
introduced by
The condition null === $role->getUuid() is always false.
Loading history...
55
            throw new LogicException('Role does not have a uuid.');
56
        }
57
58
        $services = $this->discoveryService->get();
59
60
        foreach ($role->getPermissions() as $service => $scopes) {
61
            if (
62
                property_exists($services, $service) &&
63
                $services->$service->enabled &&
64
                property_exists($services->$service, 'attributes') &&
65
                property_exists($services->$service->attributes, 'access') &&
66
                $services->$service->attributes->access
67
            ) {
68
                $access = new Access;
69
                $access
70
                    ->setOwner($role->getOwner())
71
                    ->setOwnerUuid($role->getOwnerUuid())
72
                    ->setAssignee('Role')
73
                    ->setAssigneeUuid($role->getUuid())
74
                    ->setVersion(1);
75
76
                foreach ($scopes as $scope) {
77
                    foreach ($scope['permissions'] as $entry) {
78
                        $permission = new Permission;
79
                        $permission
80
                            ->setScope($scope['scope'])
81
                            ->setEntity($scope['entity'])
82
                            ->setEntityUuid($scope['entityUuid'])
83
                            ->setKey($entry['key'])
84
                            ->setAttributes($entry['attributes']);
85
                        $access->addPermission($permission);
86
                    }
87
                }
88
89
                $this->api->get($service.'.access')->create($access);
0 ignored issues
show
Bug introduced by
The method create() does not exist on Ds\Component\Api\Service\Service. It seems like you code against a sub-type of Ds\Component\Api\Service\Service such as Ds\Component\Api\Service\IndividualService or Ds\Component\Api\Service...anizationPersonaService or Ds\Component\Api\Service\OrganizationService or Ds\Component\Api\Service\AccessService or Ds\Component\Api\Service\IndividualPersonaService. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

89
                $this->api->get($service.'.access')->/** @scrutinizer ignore-call */ create($access);
Loading history...
90
            }
91
        }
92
93
        return $this;
94
    }
95
96
    /**
97
     * Delete access across all microservices
98
     *
99
     * @param \AppBundle\Entity\Role $role
100
     * @return \AppBundle\Service\RoleService
101
     * @throws \LogicException
102
     */
103
    public function deleteAccess(Role $role)
104
    {
105
        if (null === $role->getUuid()) {
0 ignored issues
show
introduced by
The condition null === $role->getUuid() is always false.
Loading history...
106
            throw new LogicException('Role does not have a uuid.');
107
        }
108
109
        $services = $this->discoveryService->get();
110
111
        foreach ($services as $service => $config) {
112
            if (
113
                $config->enabled &&
114
                property_exists($config, 'attributes') &&
115
                property_exists($config->attributes, 'access') &&
116
                $config->attributes->access
117
            ) {
118
                $service = $this->api->get($service.'.access');
119
                $parameters = new AccessParameters;
120
                $parameters
121
                    ->setAssignee('Role')
122
                    ->setAssigneeUuid($role->getUuid());
123
                $accesses = $service->getList($parameters);
0 ignored issues
show
Bug introduced by
The method getList() does not exist on Ds\Component\Api\Service\Service. It seems like you code against a sub-type of said class. However, the method does not exist in Ds\Component\Api\Service\AbstractService. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

123
                /** @scrutinizer ignore-call */ 
124
                $accesses = $service->getList($parameters);
Loading history...
124
125
                foreach ($accesses as $access) {
126
                    $service->delete($access);
0 ignored issues
show
Bug introduced by
The method delete() does not exist on Ds\Component\Api\Service\Service. It seems like you code against a sub-type of Ds\Component\Api\Service\Service such as Ds\Component\Api\Service\AccessService. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

126
                    $service->/** @scrutinizer ignore-call */ 
127
                              delete($access);
Loading history...
127
                }
128
            }
129
        }
130
131
        return $this;
132
    }
133
}
134