Form::load()   C
last analyzed

Complexity

Conditions 15
Paths 88

Size

Total Lines 87

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 87
rs 5.0169
c 0
b 0
f 0
cc 15
nc 88
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 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
                ->setData((array) $object->data)
71
                ->setType($object->type)
72
                ->setTenant($object->tenant);
73
74
            switch ($object->type) {
75
                case FormEntity::TYPE_FORMIO:
76
                    $config = $object->config;
77
78
                    if (property_exists($config, 'components')) {
79
                        if (is_string($config->components)) {
80
                            $config->components = json_decode(file_get_contents(dirname(str_replace('{fixtures}', $fixtures, $this->path)).'/'.$config->components));
81
                        }
82
                    }
83
84
                    if (property_exists($config, 'submissionAccess')) {
85
                        if (is_string($config->submissionAccess)) {
86
                            $config->submissionAccess = json_decode(file_get_contents(dirname(str_replace('{fixtures}', $fixtures, $this->path)).'/'.$config->submissionAccess));
87
                            $submissionAccess = [];
88
89
                            foreach ($config->submissionAccess as $access) {
90
                                foreach ($access->roles as $key => $value) {
91
                                    foreach ($roles as $role) {
92
                                        if ($role->getMachineName() === $value) {
93
                                            $access->roles[$key] = $role->getId();
94
                                            break;
95
                                        }
96
                                    }
97
                                }
98
99
                                $submissionAccess[] = $access;
100
                            }
101
102
                            $config->submissionAccess = $submissionAccess;
103
                        }
104
                    }
105
106
                    $form->setConfig((array) $config);
107
                    break;
108
            }
109
110
            $manager->persist($form);
111
        }
112
113
        $manager->flush();
114
    }
115
}
116