|
1
|
|
|
<?php |
|
2
|
|
|
namespace Germania\Users; |
|
3
|
|
|
|
|
4
|
|
|
use Psr\Container\ContainerInterface; |
|
5
|
|
|
|
|
6
|
|
|
class PdoUserFactory implements ContainerInterface |
|
7
|
|
|
{ |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @var string |
|
11
|
|
|
*/ |
|
12
|
|
|
public $table = 'users'; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
public $users_class; |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var PDOStatement |
|
|
|
|
|
|
22
|
|
|
*/ |
|
23
|
|
|
public $stmt; |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var PDO |
|
28
|
|
|
*/ |
|
29
|
|
|
public $pdo; |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param PDO $pdo |
|
34
|
|
|
* @param UserAbstract $user Optional: User template object |
|
35
|
|
|
* @param string $table Optional: Users table name |
|
36
|
|
|
*/ |
|
37
|
25 |
|
public function __construct( \PDO $pdo, UserAbstract $user = null, $table = null ) |
|
38
|
|
|
{ |
|
39
|
25 |
|
$this->pdo = $pdo; |
|
|
|
|
|
|
40
|
25 |
|
$this->table = $table ?: $this->table; |
|
41
|
25 |
|
$this->php_users_class = $user ? get_class($user) : User::class; |
|
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
// ID is listed twice here in order to use it with FETCH_UNIQUE as array key |
|
46
|
|
|
$sql = "SELECT DISTINCT |
|
47
|
|
|
U.id AS id, |
|
48
|
|
|
LOWER(HEX(U.uuid)) AS uuid, |
|
49
|
|
|
U.user_first_name AS first_name, |
|
50
|
|
|
U.user_last_name AS last_name, |
|
51
|
|
|
U.user_login_name AS login_name, |
|
52
|
|
|
U.user_display_name AS display_name, |
|
53
|
|
|
U.user_email AS email, |
|
54
|
|
|
U.api_key AS api_key |
|
55
|
|
|
|
|
56
|
25 |
|
FROM {$this->table} U |
|
57
|
|
|
|
|
58
|
|
|
WHERE U.id = :id |
|
59
|
5 |
|
AND U.is_active > 0"; |
|
60
|
|
|
|
|
61
|
25 |
|
$this->stmt = $pdo->prepare( $sql ); |
|
|
|
|
|
|
62
|
|
|
|
|
63
|
25 |
|
$this->stmt->setFetchMode( \PDO::FETCH_CLASS, $this->php_users_class ); |
|
|
|
|
|
|
64
|
|
|
|
|
65
|
25 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
public function __debugInfo() { |
|
69
|
|
|
return [ |
|
70
|
|
|
'DatabaseTable' => $this->table, |
|
71
|
|
|
'UsersPhpClass' => $this->php_users_class |
|
72
|
|
|
]; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
10 |
|
public function has ($id) { |
|
78
|
10 |
|
if (!$this->stmt->execute([ |
|
79
|
8 |
|
'id' => $id |
|
80
|
2 |
|
])): |
|
81
|
5 |
|
throw new \RuntimeException("Could not read User from database"); |
|
82
|
|
|
endif; |
|
83
|
|
|
|
|
84
|
5 |
|
return (bool) $this->stmt->fetch(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
|
|
88
|
15 |
|
public function get ($id) { |
|
89
|
15 |
|
if (!$this->stmt->execute([ |
|
90
|
12 |
|
'id' => $id |
|
91
|
3 |
|
])): |
|
92
|
5 |
|
throw new \RuntimeException("Could not read User from database"); |
|
93
|
|
|
endif; |
|
94
|
|
|
|
|
95
|
|
|
|
|
96
|
10 |
|
if ($user = $this->stmt->fetch()) { |
|
97
|
5 |
|
return $user; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
5 |
|
throw new UserNotFoundException("Could not find User with ID '$id'"); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
|