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

StaffLoader::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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