Completed
Push — develop ( 7c78e9...3f0ad5 )
by Mario
05:13
created

FormFixture   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 16
lcom 0
cbo 5
dl 0
loc 102
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
D load() 0 96 16
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
        $connection = $manager->getConnection();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Doctrine\Common\Persistence\ObjectManager as the method getConnection() does only exist in the following implementations of said interface: Doctrine\ORM\Decorator\EntityManagerDecorator, Doctrine\ORM\EntityManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
22
        $platform = $connection->getDatabasePlatform()->getName();
23
24
        switch ($platform) {
25
            case 'postgresql':
26
                $connection->exec('ALTER SEQUENCE app_form_id_seq RESTART WITH 1');
27
                break;
28
        }
29
30
        $env = $this->container->get('kernel')->getEnvironment();
31
32
        // @todo create mock server instead of skipping fixture
33
        if ('test' === $env) {
34
            return;
35
        }
36
37
        $configService = $this->container->get('ds_config.service.config');
38
        $service = $this->container->get('ds_api.api')->get('formio.authentication');
39
        $user = new FormioUser;
40
        $user
41
            ->setEmail($configService->get('ds_api.user.username'))
42
            ->setPassword($configService->get('ds_api.user.password'));
43
        $token = $service->login($user);
44
        $service = $this->container->get('ds_api.api')->get('formio.form');
45
        $service->setHeader('x-jwt-token', $token);
46
        $forms = $service->getList();
47
48
        foreach ($forms as $form) {
49
            if (in_array($form->getName(), ['user', 'admin', 'userLogin', 'userRegister'])) {
50
                // Skip base formio forms.
51
                continue;
52
            }
53
54
            try {
55
                $service->delete($form->getPath());
56
            } catch (ValidationException $exception) {
57
                // @todo this is so first time fixtures dont cause an error, handle "Invalid alias" better
58
            }
59
        }
60
61
        $api = $this->container->get('ds_api.api')->get('formio.role');
62
        $api->setHeader('x-jwt-token', $token);
63
        $roles = $api->getList();
64
        $objects = $this->parse($this->getResource());
65
66
        foreach ($objects as $object) {
67
            $form = new Form;
68
            $form
69
                ->setUuid($object->uuid)
70
                ->setOwner($object->owner)
71
                ->setOwnerUuid($object->owner_uuid)
72
                ->setType($object->type)
73
                ->setTenant($object->tenant);
74
75
            switch ($object->type) {
76
                case Form::TYPE_FORMIO:
77
                    $config = $object->config;
78
79
                    if (property_exists($config, 'components')) {
80
                        if (is_string($config->components)) {
81
                            $config->components = json_decode(file_get_contents(dirname(str_replace('{env}', $env, $this->getResource())).'/'.$config->components));
82
                        }
83
                    }
84
85
                    if (property_exists($config, 'submissionAccess')) {
86
                        if (is_string($config->submissionAccess)) {
87
                            $config->submissionAccess = json_decode(file_get_contents(dirname(str_replace('{env}', $env, $this->getResource())).'/'.$config->submissionAccess));
88
                            $submissionAccess = [];
89
90
                            foreach ($config->submissionAccess as $access) {
91
                                foreach ($access->roles as $key => $value) {
92
                                    foreach ($roles as $role) {
93
                                        if ($role->getMachineName() === $value) {
94
                                            $access->roles[$key] = $role->getId();
95
                                            break;
96
                                        }
97
                                    }
98
                                }
99
100
                                $submissionAccess[] = $access;
101
                            }
102
103
                            $config->submissionAccess = $submissionAccess;
104
                        }
105
                    }
106
107
                    $form->setConfig($config);
108
                    break;
109
            }
110
111
            $manager->persist($form);
112
            $manager->flush();
113
        }
114
    }
115
}
116