Completed
Pull Request — develop (#37)
by Mario
01:23
created

Form::load()   D

Complexity

Conditions 16
Paths 168

Size

Total Lines 93

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 93
rs 4.1527
c 0
b 0
f 0
cc 16
nc 168
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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