Completed
Push — develop ( 22307e...eb2da7 )
by Mario
01:42
created

AccessLoader::load()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 9.296
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
namespace AppBundle\Tenant\Loader;
4
5
use Ds\Component\Security\Service\AccessService;
6
use Ds\Component\Security\Service\PermissionService;
7
use Ds\Component\Tenant\Entity\Tenant;
8
use Ds\Component\Tenant\Loader\Loader;
9
use Symfony\Component\Yaml\Yaml;
10
11
/**
12
 * Class AccessLoader
13
 */
14
class AccessLoader implements Loader
15
{
16
    /**
17
     * @var \Ds\Component\Security\Service\AccessService
18
     */
19
    protected $accessService;
20
21
    /**
22
     * @var \Ds\Component\Security\Service\PermissionService
23
     */
24
    protected $permissionService;
25
26
    /**
27
     * Constructor
28
     *
29
     * @param \Ds\Component\Security\Service\AccessService $accessService
30
     * @param \Ds\Component\Security\Service\PermissionService $permissionService
31
     */
32
    public function __construct(AccessService $accessService, PermissionService $permissionService)
33
    {
34
        $this->accessService = $accessService;
35
        $this->permissionService = $permissionService;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function load(Tenant $tenant)
42
    {
43
        $yml = file_get_contents('/srv/api-platform/src/AppBundle/Resources/tenant/accesses.yml');
44
45
        // @todo Figure out how symfony does parameter binding and use the same technique
46
        $yml = strtr($yml, [
47
            '%identity.system.uuid%' => $tenant->getData()['identity']['system']['uuid'],
48
            '%business_unit.administration.uuid%' => $tenant->getData()['business_unit']['administration']['uuid'],
49
            '%identity.admin.uuid%' => $tenant->getData()['identity']['admin']['uuid'],
50
            '%tenant.uuid%' => $tenant->getUuid()
51
        ]);
52
53
        $accesses = Yaml::parse($yml, YAML::PARSE_OBJECT_FOR_MAP);
54
        $manager = $this->accessService->getManager();
55
56
        foreach ($accesses->objects as $object) {
57
            $object = (object) array_merge((array) $accesses->prototype, (array) $object);
58
            $access = $this->accessService->createInstance();
59
            $access
60
                ->setOwner($object->owner)
61
                ->setOwnerUuid($object->owner_uuid)
62
                ->setAssignee($object->assignee)
63
                ->setAssigneeUuid($object->assignee_uuid)
64
                ->setTenant($object->tenant);
65
66
            foreach ($object->permissions as $subObject) {
67
                $permission = $this->permissionService->createInstance();
68
                $permission
69
                    ->setScope($subObject->scope)
70
                    ->setKey($subObject->key)
71
                    ->setAttributes($subObject->attributes)
72
                    ->setTenant($object->tenant);
73
                $access->addPermission($permission);
74
            }
75
76
            $manager->persist($access);
77
            $manager->flush();
78
        }
79
    }
80
}
81