Passed
Push — develop ( b3d985...20907a )
by Mario
04:01
created

SystemLoader::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 16
nc 2
nop 1
dl 0
loc 23
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
namespace AppBundle\Tenant\Loader\Identity;
4
5
use AppBundle\Service\SystemService;
6
use Ds\Component\Tenant\Entity\Tenant;
7
use Ds\Component\Tenant\Loader\Loader;
8
use Symfony\Component\Yaml\Yaml;
9
10
/**
11
 * Class SystemLoader
12
 */
13
class SystemLoader implements Loader
14
{
15
    /**
16
     * @var \AppBundle\Service\SystemService
17
     */
18
    protected $systemService;
19
20
    /**
21
     * Constructor
22
     *
23
     * @param \AppBundle\Service\SystemService $systemService
24
     */
25
    public function __construct(SystemService $systemService)
26
    {
27
        $this->systemService = $systemService;
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function load(Tenant $tenant)
34
    {
35
        $yml = file_get_contents('/srv/api-platform/src/AppBundle/Resources/tenant/identity/system/identities.yml');
36
37
        // @todo Figure out how symfony does parameter binding and use the same technique
38
        $yml = strtr($yml, [
39
            '%identity.system.uuid%' => $tenant->getData()['identity']['system']['uuid'],
40
            '%tenant.uuid%' => $tenant->getUuid()
41
        ]);
42
43
        $systems = Yaml::parse($yml, YAML::PARSE_OBJECT_FOR_MAP);
44
        $manager = $this->systemService->getManager();
45
46
        foreach ($systems->objects as $object) {
47
            $object = (object) array_merge((array) $systems->prototype, (array) $object);
48
            $system = $this->systemService->createInstance();
49
            $system
50
                ->setUuid($object->uuid)
51
                ->setOwner($object->owner)
52
                ->setOwnerUuid($object->owner_uuid)
53
                ->setTenant($object->tenant);
54
            $manager->persist($system);
55
            $manager->flush();
56
        }
57
    }
58
}
59