Container::init()   D
last analyzed

Complexity

Conditions 18
Paths 257

Size

Total Lines 70
Code Lines 45

Duplication

Lines 39
Ratio 55.71 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 39
loc 70
rs 4.1907
cc 18
eloc 45
nc 257
nop 0

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 Sinergi\Users;
4
5
use Doctrine\ORM\EntityManager;
6
use Interop\Container\ContainerInterface;
7
use Sinergi\Users\Doctrine\User\UserEntity as DoctrineUserEntity;
8
use Sinergi\Users\Doctrine\User\UserRepository as DoctrineUserRepository;
9
use Sinergi\Users\Eloquent\User\UserEntity as EloquentUserEntity;
10
use Sinergi\Users\Eloquent\User\UserRepository as EloquentUserRepository;
11
use Sinergi\Users\Doctrine\Session\SessionEntity as DoctrineSessionEntity;
12
use Sinergi\Users\Doctrine\Session\SessionRepository as DoctrineSessionRepository;
13
use Sinergi\Users\Eloquent\Session\SessionEntity as EloquentSessionEntity;
14
use Sinergi\Users\Eloquent\Session\SessionRepository as EloquentSessionRepository;
15
use Sinergi\Users\Doctrine\Group\GroupEntity as DoctrineGroupEntity;
16
use Sinergi\Users\Doctrine\Group\GroupRepository as DoctrineGroupRepository;
17
use Sinergi\Users\Eloquent\Group\GroupEntity as EloquentGroupEntity;
18
use Sinergi\Users\Eloquent\Group\GroupRepository as EloquentGroupRepository;
19
use Sinergi\Users\Group\GroupEntityInterface;
20
use Sinergi\Users\Group\GroupRepositoryInterface;
21
use Sinergi\Users\Group\GroupValidator;
22
use Sinergi\Users\Group\GroupValidatorInterface;
23
use Sinergi\Users\Session\SessionEntityInterface;
24
use Sinergi\Users\Session\SessionRepositoryInterface;
25
use Sinergi\Users\User\UserEntityInterface;
26
use Sinergi\Users\User\UserRepositoryInterface;
27
use Sinergi\Users\User\UserValidator;
28
use Sinergi\Users\User\UserValidatorInterface;
29
30
class Container implements ContainerInterface
31
{
32
    private $container;
33
    private $items = [];
34
35
    public function __construct(ContainerInterface $container)
36
    {
37
        $this->container = $container;
38
        $this->init();
39
    }
40
41
    private function init()
42
    {
43
        $self = $this;
44
        $hasUserEntity = $this->container->has(UserEntityInterface::class);
45
        $hasUserRepository = $this->container->has(UserRepositoryInterface::class);
46
        $hasSessionEntity = $this->container->has(SessionEntityInterface::class);
47
        $hasSessionRepository = $this->container->has(SessionRepositoryInterface::class);
48
        $hasGroupEntity = $this->container->has(GroupEntityInterface::class);
49
        $hasGroupRepository = $this->container->has(GroupRepositoryInterface::class);
50
        $hasUserValidator = $this->container->has(UserValidatorInterface::class);
51
        $hasGroupValidator = $this->container->has(GroupValidatorInterface::class);
52
53
        if (!$hasUserEntity || !$hasSessionEntity || !$hasGroupEntity) {
54
            throw new \Exception('Container passed to Sinergi\\Users must contain user, group and session\'s entities');
55
        }
56
57 View Code Duplication
        if ($hasUserEntity && !$hasUserRepository) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
            $userEntity = $this->container->get(UserEntityInterface::class);
59
            if ($userEntity instanceof DoctrineUserEntity) {
60
                $em = $this->container->get(EntityManager::class);
61
                $this->items[UserRepositoryInterface::class] = function () use ($em, $userEntity, $self) {
62
                    return new DoctrineUserRepository($em, $em->getClassMetadata(get_class($userEntity)), $self);
63
                };
64
            } elseif ($userEntity instanceof EloquentUserEntity) {
65
                $this->items[UserRepositoryInterface::class] = function () {
66
                    return new EloquentUserRepository();
67
                };
68
            }
69
        }
70
71 View Code Duplication
        if ($hasSessionEntity && !$hasSessionRepository) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
            $sessionEntity = $this->container->get(SessionEntityInterface::class);
73
            if ($sessionEntity instanceof DoctrineSessionEntity) {
74
                $em = $this->container->get(EntityManager::class);
75
                $this->items[SessionRepositoryInterface::class] = function () use ($em, $sessionEntity, $self) {
76
                    return new DoctrineSessionRepository($em, $em->getClassMetadata(get_class($sessionEntity)), $self);
77
                };
78
            } elseif ($sessionEntity instanceof EloquentSessionEntity) {
79
                $this->items[SessionRepositoryInterface::class] = function () {
80
                    return new EloquentSessionRepository();
81
                };
82
            }
83
        }
84
85 View Code Duplication
        if ($hasGroupEntity && !$hasGroupRepository) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86
            $groupEntity = $this->container->get(GroupEntityInterface::class);
87
            if ($groupEntity instanceof DoctrineGroupEntity) {
88
                $em = $this->container->get(EntityManager::class);
89
                $this->items[GroupRepositoryInterface::class] = function () use ($em, $groupEntity, $self) {
90
                    return new DoctrineGroupRepository($em, $em->getClassMetadata(get_class($groupEntity)), $self);
91
                };
92
            } elseif ($groupEntity instanceof EloquentGroupEntity) {
93
                $this->items[GroupRepositoryInterface::class] = function () {
94
                    return new EloquentGroupRepository();
95
                };
96
            }
97
        }
98
99
        if (!$hasUserValidator) {
100
            $this->items[UserValidatorInterface::class] = function () use ($self) {
101
                return new UserValidator($self);
102
            };
103
        }
104
105
        if (!$hasGroupValidator) {
106
            $this->items[GroupValidatorInterface::class] = function () use ($self) {
107
                return new GroupValidator($self);
108
            };
109
        }
110
    }
111
112
    public function get($id)
113
    {
114
        if (isset($this->items[$id])) {
115
            $item = $this->items[$id];
116
            if (is_callable($item)) {
117
                return call_user_func($item);
118
            } else {
119
                return $item;
120
            }
121
        }
122
        return $this->container->get($id);
123
    }
124
125
    public function has($id): bool
126
    {
127
        if (isset($this->items[$id])) {
128
            return true;
129
        }
130
        return $this->container->has($id);
131
    }
132
}
133