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\Session\SessionEntityInterface; |
16
|
|
|
use Sinergi\Users\Session\SessionRepositoryInterface; |
17
|
|
|
use Sinergi\Users\User\UserEntityInterface; |
18
|
|
|
use Sinergi\Users\User\UserRepositoryInterface; |
19
|
|
|
|
20
|
|
|
class Container implements ContainerInterface |
21
|
|
|
{ |
22
|
|
|
private $container; |
23
|
|
|
private $items = []; |
24
|
|
|
|
25
|
|
|
public function __construct(ContainerInterface $container) |
26
|
|
|
{ |
27
|
|
|
$this->container = $container; |
28
|
|
|
$this->init(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
private function init() |
32
|
|
|
{ |
33
|
|
|
$hasUserEntity = $this->container->has(UserEntityInterface::class); |
34
|
|
|
$hasUserRepository = $this->container->has(UserRepositoryInterface::class); |
35
|
|
|
$hasSessionEntity = $this->container->has(SessionEntityInterface::class); |
36
|
|
|
$hasSessionRepository = $this->container->has(SessionRepositoryInterface::class); |
37
|
|
|
|
38
|
|
View Code Duplication |
if ($hasUserEntity && !$hasUserRepository) { |
|
|
|
|
39
|
|
|
$userEntity = $this->container->get(UserEntityInterface::class); |
40
|
|
|
if ($userEntity instanceof DoctrineUserEntity) { |
41
|
|
|
$em = $this->container->get(EntityManager::class); |
42
|
|
|
$this->items[UserRepositoryInterface::class] = function () use ($em, $userEntity) { |
43
|
|
|
return new DoctrineUserRepository($em, $em->getClassMetadata(get_class($userEntity))); |
44
|
|
|
}; |
45
|
|
|
} elseif ($userEntity instanceof EloquentUserEntity) { |
46
|
|
|
$this->items[UserRepositoryInterface::class] = function () { |
47
|
|
|
return new EloquentUserRepository(); |
48
|
|
|
}; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
View Code Duplication |
if ($hasSessionEntity && !$hasSessionRepository) { |
|
|
|
|
53
|
|
|
$sessionEntity = $this->container->get(SessionEntityInterface::class); |
54
|
|
|
if ($sessionEntity instanceof DoctrineSessionEntity) { |
55
|
|
|
$em = $this->container->get(EntityManager::class); |
56
|
|
|
$this->items[SessionRepositoryInterface::class] = function () use ($em, $sessionEntity) { |
57
|
|
|
return new DoctrineSessionRepository($em, $em->getClassMetadata(get_class($sessionEntity))); |
58
|
|
|
}; |
59
|
|
|
} elseif ($sessionEntity instanceof EloquentSessionEntity) { |
60
|
|
|
$this->items[SessionRepositoryInterface::class] = function () { |
61
|
|
|
return new EloquentSessionRepository(); |
62
|
|
|
}; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function get($id) |
68
|
|
|
{ |
69
|
|
|
if (isset($this->items[$id])) { |
70
|
|
|
$item = $this->items[$id]; |
71
|
|
|
if (is_callable($item)) { |
72
|
|
|
return call_user_func($item); |
73
|
|
|
} else { |
74
|
|
|
return $item; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
return $this->container->get($id); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function has($id): bool |
81
|
|
|
{ |
82
|
|
|
if (isset($this->items[$id])) { |
83
|
|
|
return true; |
84
|
|
|
} |
85
|
|
|
return $this->container->has($id); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
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.