Passed
Push — develop ( d38868...21087d )
by Mario
01:26
created

UserService::createIdentity()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 54
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 42
nc 4
nop 1
dl 0
loc 54
rs 9.248
c 0
b 0
f 0

How to fix   Long Method   

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\Service;
4
5
use App\Entity\User;
6
use Doctrine\ORM\EntityManagerInterface;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\EntityManagerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use DomainException;
8
use Ds\Component\Api\Api\Api;
0 ignored issues
show
Bug introduced by
The type Ds\Component\Api\Api\Api was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Ds\Component\Api\Model\Individual;
0 ignored issues
show
Bug introduced by
The type Ds\Component\Api\Model\Individual was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Ds\Component\Api\Model\IndividualPersona;
0 ignored issues
show
Bug introduced by
The type Ds\Component\Api\Model\IndividualPersona was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Ds\Component\Api\Model\Organization;
0 ignored issues
show
Bug introduced by
The type Ds\Component\Api\Model\Organization was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Ds\Component\Api\Model\OrganizationPersona;
0 ignored issues
show
Bug introduced by
The type Ds\Component\Api\Model\OrganizationPersona was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Ds\Component\Entity\Service\EntityService;
0 ignored issues
show
Bug introduced by
The type Ds\Component\Entity\Service\EntityService was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Ds\Component\Security\Model\Identity;
0 ignored issues
show
Bug introduced by
The type Ds\Component\Security\Model\Identity was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use FOS\UserBundle\Model\UserManagerInterface;
0 ignored issues
show
Bug introduced by
The type FOS\UserBundle\Model\UserManagerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use LogicException;
17
18
/**
19
 * Class UserService
20
 */
21
final class UserService extends EntityService
22
{
23
    /**
24
     * @var \FOS\UserBundle\Model\UserManagerInterface
25
     */
26
    private $customManager; # region accessor
27
28
    /**
29
     * Get custom manager
30
     *
31
     * @return \FOS\UserBundle\Model\UserManagerInterface
32
     */
33
    public function getCustomManager()
34
    {
35
        return $this->customManager;
36
    }
37
38
    # endregion
39
40
    /**
41
     * @var \Ds\Component\Api\Api\Api
42
     */
43
    private $api;
44
45
    /**
46
     * Constructor
47
     *
48
     * @param \Doctrine\ORM\EntityManagerInterface $manager
49
     * @param \FOS\UserBundle\Model\UserManagerInterface $customManager
50
     * @param \Ds\Component\Api\Api\Api $api
51
     * @param string $entity
52
     */
53
    public function __construct(EntityManagerInterface $manager, UserManagerInterface $customManager, Api $api, string $entity = User::class)
54
    {
55
        parent::__construct($manager, $entity);
56
        $this->customManager = $customManager;
57
        $this->api = $api;
58
    }
59
60
    /**
61
     * Create identity for a user
62
     *
63
     * @param \App\Entity\User $user
64
     * @return \App\Entity\User
65
     * @throws \LogicException
66
     */
67
    public function createIdentity(User $user)
68
    {
69
        if ($user->getIdentityUuid()) {
70
            throw new LogicException('Identity "'.$user->getIdentityUuid().'" already exists.');
71
        }
72
73
        switch ($user->getIdentity()) {
74
            case Identity::INDIVIDUAL:
75
                $identity = new Individual;
76
                $identity
77
                    ->setOwner($user->getOwner())
78
                    ->setOwnerUuid($user->getOwnerUuid());
79
                $identity = $this->api->get('identities.individual')->create($identity);
80
                $persona = new IndividualPersona;
81
                $persona
82
                    ->setOwner($user->getOwner())
83
                    ->setOwnerUuid($user->getOwnerUuid())
84
                    ->setIndividual($identity)
85
                    ->setTitle([ // @todo remove hard-coded titles
86
                        'en' => 'Default',
87
                        'fr' => 'Défaut'
88
                    ])
89
                    ->setData($user->getRegistration()->getData());
90
                $this->api->get('identities.individual_persona')->create($persona);
91
                break;
92
93
            case Identity::ORGANIZATION:
94
                $identity = new Organization;
95
                $identity
96
                    ->setOwner($user->getOwner())
97
                    ->setOwnerUuid($user->getOwnerUuid());
98
                $identity = $this->api->get('identities.organization')->create($identity);
99
100
                $persona = new OrganizationPersona;
101
                $persona
102
                    ->setOwner($user->getOwner())
103
                    ->setOwnerUuid($user->getOwnerUuid())
104
                    ->setOrganization($identity)
105
                    ->setTitle([ // @todo remove hard-coded titles
106
                        'en' => 'Default',
107
                        'fr' => 'Défaut'
108
                    ])
109
                    ->setData($user->getRegistration()->getData());
110
                $this->api->get('identities.organization_persona')->create($persona);
111
                break;
112
113
            default:
114
                throw new DomainException('User identity "'.$user->getIdentity().'" is not supported.');
115
        }
116
117
        $user->setIdentityUuid($identity->getUuid());
118
        $this->customManager->updateUser($user);
119
120
        return $identity;
121
    }
122
}
123