-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDoctrineUserManager.php
More file actions
103 lines (85 loc) · 2.55 KB
/
Copy pathDoctrineUserManager.php
File metadata and controls
103 lines (85 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
declare(strict_types=1);
namespace Yokai\SecurityTokenBundle\Manager;
use Doctrine\Common\Util\ClassUtils;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\ObjectManager;
/**
* User manager for doctrine entities.
*
* @author Yann Eugoné <eugone.yann@gmail.com>
*/
class DoctrineUserManager implements UserManagerInterface
{
/**
* @var ManagerRegistry
*/
private $doctrine;
/**
* @param ManagerRegistry $doctrine The doctrine registry
*/
public function __construct(ManagerRegistry $doctrine)
{
$this->doctrine = $doctrine;
}
public function supportsClass(string $class): bool
{
try {
$this->getManagerFor($class);
} catch (\InvalidArgumentException $exception) {
return false;
}
return true;
}
public function supportsUser($user): bool
{
return $this->supportsClass(
$this->getClass($user)
);
}
public function get(string $class, string $id)
{
return $this->getManagerFor($class)->find($class, $id);
}
public function getClass($user): string
{
/** @var object $user */
return ClassUtils::getClass($user);
}
public function getId($user): string
{
/** @var object $user */
/** @var class-string $class */
$class = $this->getClass($user);
$identifiers = $this->getManagerFor($class)->getClassMetadata($class)->getIdentifierValues($user);
if (count($identifiers) > 1) {
throw new \InvalidArgumentException('Entities with composite ids are not supported');
}
$identifier = reset($identifiers);
if (is_scalar($identifier)
|| $identifier === null
|| (is_object($identifier) && method_exists($identifier, '__toString'))
) {
return (string)$identifier;
}
throw new \InvalidArgumentException('Entities with non stringable ids are not supported');
}
/**
* Get doctrine object manager for a class.
*
* @param class-string $class The user class
*/
private function getManagerFor(string $class): ObjectManager
{
$manager = $this->doctrine->getManagerForClass($class);
if ($manager === null) {
throw new \InvalidArgumentException(
sprintf(
'Class "%s" seems not to be a managed Doctrine entity. Did you forget to map it?',
$class
)
);
}
return $manager;
}
}