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

BusinessUnitLoader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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