1
|
|
|
<?php |
2
|
|
|
namespace Germania\Users; |
3
|
|
|
|
4
|
|
|
use Psr\Container\ContainerInterface; |
5
|
|
|
use Ramsey\Uuid\UuidFactoryInterface; |
6
|
|
|
|
7
|
|
|
class PdoUserUuidFactory implements ContainerInterface |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
public $table = 'users'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
public $users_class; |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var PDOStatement |
|
|
|
|
23
|
|
|
*/ |
24
|
|
|
public $stmt; |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var PDO |
29
|
|
|
*/ |
30
|
|
|
public $pdo; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Ramsey\Uuid\UuidFactory |
|
|
|
|
34
|
|
|
*/ |
35
|
|
|
public $uuid_factory; |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param \PDO $pdo PDO instance |
40
|
|
|
* @param UuidFactoryInterface $uuid_factory Ramsey's UUID factory |
41
|
|
|
* @param UserAbstract|null $user Optional: UserInterface instance |
42
|
|
|
* @param [type] $table Optional: table name |
|
|
|
|
43
|
|
|
*/ |
44
|
25 |
|
public function __construct( \PDO $pdo, UuidFactoryInterface $uuid_factory, UserAbstract $user = null, $table = null ) |
45
|
|
|
{ |
46
|
25 |
|
$this->pdo = $pdo; |
|
|
|
|
47
|
25 |
|
$this->uuid_factory = $uuid_factory; |
|
|
|
|
48
|
25 |
|
$this->table = $table ?: $this->table; |
49
|
25 |
|
$this->php_users_class = $user ? get_class($user) : User::class; |
|
|
|
|
50
|
|
|
|
51
|
|
|
|
52
|
|
|
|
53
|
|
|
$sql = "SELECT DISTINCT |
54
|
|
|
U.id AS id, |
55
|
|
|
LOWER(HEX(U.uuid)) AS uuid, |
56
|
|
|
U.user_first_name AS first_name, |
57
|
|
|
U.user_first_name AS first_name, |
58
|
|
|
U.user_last_name AS last_name, |
59
|
|
|
U.user_login_name AS login_name, |
60
|
|
|
U.user_display_name AS display_name, |
61
|
|
|
U.user_email AS email, |
62
|
|
|
U.api_key AS api_key |
63
|
|
|
|
64
|
25 |
|
FROM {$this->table} U |
65
|
|
|
|
66
|
|
|
WHERE U.uuid = UNHEX( REPLACE( :uuid, '-','') ) |
67
|
5 |
|
AND U.is_active > 0"; |
68
|
|
|
|
69
|
25 |
|
$this->stmt = $pdo->prepare( $sql ); |
|
|
|
|
70
|
|
|
|
71
|
25 |
|
$this->stmt->setFetchMode( \PDO::FETCH_CLASS, $this->php_users_class ); |
|
|
|
|
72
|
|
|
|
73
|
25 |
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
public function __debugInfo() { |
77
|
|
|
return [ |
78
|
|
|
'DatabaseTable' => $this->table, |
79
|
|
|
'UsersPhpClass' => $this->php_users_class |
80
|
|
|
]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
|
85
|
10 |
|
public function has ($uuid) { |
86
|
10 |
|
$uuid = $this->assertUUID( $uuid ); |
87
|
10 |
|
if (!$this->stmt->execute([ |
88
|
8 |
|
'uuid' => $uuid |
89
|
2 |
|
])): |
90
|
5 |
|
throw new \RuntimeException("Could not read User from database"); |
91
|
|
|
endif; |
92
|
|
|
|
93
|
5 |
|
return (bool) $this->stmt->fetch(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
|
97
|
15 |
|
public function get ($uuid) { |
98
|
15 |
|
$uuid = $this->assertUUID( $uuid ); |
99
|
15 |
|
if (!$this->stmt->execute([ |
100
|
12 |
|
'uuid' => $uuid |
101
|
3 |
|
])): |
102
|
5 |
|
throw new \RuntimeException("Could not read User from database"); |
103
|
|
|
endif; |
104
|
|
|
|
105
|
|
|
|
106
|
10 |
|
if ($user = $this->stmt->fetch()) { |
107
|
5 |
|
return $user; |
108
|
|
|
} |
109
|
|
|
|
110
|
5 |
|
throw new UserNotFoundException("Could not find User with UUID '$uuid'"); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
|
114
|
|
|
|
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param UuidInterface|string $uuid |
118
|
|
|
* @return UuidInterface |
119
|
|
|
* |
120
|
|
|
* @throws Ramsey\Uuid\Exception\InvalidUuidStringException |
121
|
|
|
*/ |
122
|
20 |
|
protected function assertUUID( $uuid ) |
123
|
|
|
{ |
124
|
20 |
|
if (is_string( $uuid )): |
125
|
20 |
|
$uuid = $this->uuid_factory->fromString( $uuid ); |
126
|
4 |
|
elseif (!$uuid instanceOf UuidInterface): |
|
|
|
|
127
|
|
|
throw new \InvalidArgumentException("UuidInterface or UUID string expected"); |
128
|
|
|
endif; |
129
|
|
|
|
130
|
20 |
|
return $uuid; |
131
|
|
|
|
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
|