Completed
Pull Request — develop (#17)
by Mario
09:34
created

Form   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 4
dl 0
loc 99
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C load() 0 86 15
1
<?php
2
3
namespace App\Fixture;
4
5
use App\Entity\Form as FormEntity;
6
use Doctrine\Common\Persistence\ObjectManager;
7
use Ds\Component\Api\Api\Api;
8
use Ds\Component\Config\Service\ConfigService;
9
use Ds\Component\Database\Fixture\Yaml;
10
use Ds\Component\Formio\Exception\ValidationException;
11
use Ds\Component\Formio\Model\User as FormioUser;
12
13
/**
14
 * Trait Form
15
 */
16
trait Form
17
{
18
    use Yaml;
19
20
    /**
21
     * @var string
22
     */
23
    private $path;
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function load(ObjectManager $manager)
29
    {
30
        $fixtures = array_key_exists('FIXTURES', $_ENV) ? $_ENV['FIXTURES'] : 'dev';
31
        $configService = $this->container->get(ConfigService::class);
32
33
        $service = $this->container->get(Api::class)->get('formio.authentication');
34
        $user = new FormioUser;
35
        $user
36
            ->setEmail($configService->get('ds_api.user.username'))
37
            ->setPassword($configService->get('ds_api.user.password'));
38
        $token = $service->login($user);
39
40
        $service = $this->container->get(Api::class)->get('formio.form');
41
        $service->setHeader('x-jwt-token', $token);
42
        $forms = $service->getList();
43
44
        foreach ($forms as $form) {
45
            if (in_array($form->getName(), ['user', 'admin', 'userLogin', 'userRegister'])) {
46
                // Skip base formio forms
47
                continue;
48
            }
49
50
            try {
51
                $service->delete($form->getPath());
52
            } catch (ValidationException $exception) {
53
                // @todo this is so first time fixtures dont cause an error, handle "Invalid alias" better
54
            }
55
        }
56
57
        $api = $this->container->get(Api::class)->get('formio.role');
58
        $api->setHeader('x-jwt-token', $token);
59
        $roles = $api->getList();
60
        $objects = $this->parse($this->path);
61
62
        foreach ($objects as $object) {
63
            $form = new FormEntity;
64
            $form
65
                ->setUuid($object->uuid)
66
                ->setOwner($object->owner)
67
                ->setOwnerUuid($object->owner_uuid)
68
                ->setTitle((array) $object->title)
69
                ->setDescription((array) $object->description)
70
                ->setType($object->type)
71
                ->setTenant($object->tenant);
72
73
            switch ($object->type) {
74
                case FormEntity::TYPE_FORMIO:
75
                    $config = $object->config;
76
77
                    if (property_exists($config, 'components')) {
78
                        if (is_string($config->components)) {
79
                            $config->components = json_decode(file_get_contents(dirname(str_replace('{fixtures}', $fixtures, $this->path)).'/'.$config->components));
80
                        }
81
                    }
82
83
                    if (property_exists($config, 'submissionAccess')) {
84
                        if (is_string($config->submissionAccess)) {
85
                            $config->submissionAccess = json_decode(file_get_contents(dirname(str_replace('{fixtures}', $fixtures, $this->path)).'/'.$config->submissionAccess));
86
                            $submissionAccess = [];
87
88
                            foreach ($config->submissionAccess as $access) {
89
                                foreach ($access->roles as $key => $value) {
90
                                    foreach ($roles as $role) {
91
                                        if ($role->getMachineName() === $value) {
92
                                            $access->roles[$key] = $role->getId();
93
                                            break;
94
                                        }
95
                                    }
96
                                }
97
98
                                $submissionAccess[] = $access;
99
                            }
100
101
                            $config->submissionAccess = $submissionAccess;
102
                        }
103
                    }
104
105
                    $form->setConfig((array) $config);
106
                    break;
107
            }
108
109
            $manager->persist($form);
110
        }
111
112
        $manager->flush();
113
    }
114
}
115