Completed
Push — develop ( 993e3b...9d411d )
by Mario
16:12
created

FormFixture::load()   C

Complexity

Conditions 14
Paths 44

Size

Total Lines 81

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 81
rs 5.4278
c 0
b 0
f 0
cc 14
nc 44
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 AppBundle\Fixture;
4
5
use AppBundle\Entity\Form;
6
use Doctrine\Common\Persistence\ObjectManager;
7
use Ds\Component\Database\Fixture\ResourceFixture;
8
use Ds\Component\Formio\Exception\ValidationException;
9
use Ds\Component\Formio\Model\User as FormioUser;
10
11
/**
12
 * Class FormFixture
13
 */
14
abstract class FormFixture extends ResourceFixture
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19
    public function load(ObjectManager $manager)
20
    {
21
        $env = $this->container->get('kernel')->getEnvironment();
22
        $configService = $this->container->get('ds_config.service.config');
23
        $service = $this->container->get('ds_api.api')->get('formio.authentication');
24
        $user = new FormioUser;
25
        $user
26
            ->setEmail($configService->get('ds_api.user.username'))
27
            ->setPassword($configService->get('ds_api.user.password'));
28
        $token = $service->login($user);
29
        $service = $this->container->get('ds_api.api')->get('formio.form');
30
        $service->setHeader('x-jwt-token', $token);
31
        $forms = $service->getList();
32
33
        foreach ($forms as $form) {
34
            if (in_array($form->getName(), ['user', 'admin', 'userLogin', 'userRegister'])) {
35
                // Skip base formio forms.
36
                continue;
37
            }
38
39
            try {
40
                $service->delete($form->getPath());
41
            } catch (ValidationException $exception) {
42
                // @todo this is so first time fixtures dont cause an error, handle "Invalid alias" better
43
            }
44
        }
45
46
        $api = $this->container->get('ds_api.api')->get('formio.role');
47
        $api->setHeader('x-jwt-token', $token);
48
        $roles = $api->getList();
49
        $objects = $this->parse($this->getResource());
50
51
        foreach ($objects as $object) {
52
            $form = new Form;
53
            $form
54
                ->setUuid($object->uuid)
55
                ->setOwner($object->owner)
56
                ->setOwnerUuid($object->owner_uuid)
57
                ->setType($object->type)
58
                ->setTenant($object->tenant);
59
60
            switch ($object->type) {
61
                case Form::TYPE_FORMIO:
62
                    $config = $object->config;
63
64
                    if (property_exists($config, 'components')) {
65
                        if (is_string($config->components)) {
66
                            $config->components = json_decode(file_get_contents(dirname(str_replace('{env}', $env, $this->getResource())).'/'.$config->components));
67
                        }
68
                    }
69
70
                    if (property_exists($config, 'submission_access')) {
71
                        if (is_string($config->submission_access)) {
72
                            $config->submission_access = json_decode(file_get_contents(dirname(str_replace('{env}', $env, $this->getResource())).'/'.$config->submission_access));
73
                            $submissionAccess = [];
74
75
                            foreach ($config->submission_access as $access) {
76
                                foreach ($access->roles as $key => $value) {
77
                                    foreach ($roles as $role) {
78
                                        if ($role->getMachineName() === $value) {
79
                                            $access->roles[$key] = $role->getId();
80
                                            break;
81
                                        }
82
                                    }
83
                                }
84
85
                                $submissionAccess[] = $access;
86
                            }
87
88
                            $config->submission_access = $submissionAccess;
89
                        }
90
                    }
91
92
                    $form->setConfig($config);
93
                    break;
94
            }
95
96
            $manager->persist($form);
97
            $manager->flush();
98
        }
99
    }
100
}
101