|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright (c) Phauthentic (https://github.com/Phauthentic) |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under The MIT License |
|
7
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
|
8
|
|
|
* Redistributions of files must retain the above copyright notice. |
|
9
|
|
|
* |
|
10
|
|
|
* @copyright Copyright (c) Phauthentic (https://github.com/Phauthentic) |
|
11
|
|
|
* @link https://github.com/Phauthentic |
|
12
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
declare(strict_types=1); |
|
16
|
|
|
|
|
17
|
|
|
namespace Phauthentic\Authentication\Identifier\Resolver; |
|
18
|
|
|
|
|
19
|
|
|
use ArrayAccess; |
|
20
|
|
|
use ArrayObject; |
|
21
|
|
|
use PDO; |
|
22
|
|
|
use PDOException; |
|
23
|
|
|
use PDOStatement; |
|
24
|
|
|
use RuntimeException; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* PDO Resolver |
|
28
|
|
|
*/ |
|
29
|
|
|
class PdoResolver implements ResolverInterface |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @var \PDO |
|
33
|
|
|
*/ |
|
34
|
|
|
protected PDO $pdo; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
protected string $sql; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Constructor. |
|
43
|
|
|
* |
|
44
|
|
|
* @param \PDO $pdo PDO Instance |
|
45
|
4 |
|
* @param string $sql SQL String |
|
46
|
|
|
*/ |
|
47
|
4 |
|
public function __construct(PDO $pdo, string $sql) |
|
48
|
4 |
|
{ |
|
49
|
4 |
|
$this->pdo = $pdo; |
|
50
|
|
|
$this->sql = $sql; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Builds the statement |
|
55
|
|
|
* |
|
56
|
4 |
|
* @return \PDOStatement |
|
57
|
|
|
*/ |
|
58
|
4 |
|
protected function buildStatement(): PDOStatement |
|
59
|
|
|
{ |
|
60
|
4 |
|
$statement = $this->pdo->prepare($this->sql); |
|
61
|
4 |
|
|
|
62
|
|
|
$error = $this->pdo->errorInfo(); |
|
63
|
|
|
if ($error[0] !== '00000') { |
|
64
|
|
|
throw new PDOException($error[2], (int)$error[0]); |
|
65
|
4 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
if (!$statement instanceof PDOStatement) { |
|
|
|
|
|
|
68
|
|
|
throw new RuntimeException(sprintf( |
|
69
|
|
|
'There was an error running your PDO resolver using this query: %s', |
|
70
|
|
|
$this->sql |
|
71
|
|
|
)); |
|
72
|
4 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $statement; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
4 |
|
* {@inheritDoc} |
|
79
|
|
|
*/ |
|
80
|
4 |
|
public function find(array $conditions): ?ArrayAccess |
|
81
|
4 |
|
{ |
|
82
|
4 |
|
foreach ($conditions as $key => $value) { |
|
83
|
|
|
unset($conditions[$key]); |
|
84
|
|
|
$conditions[':' . $key] = $value; |
|
85
|
4 |
|
} |
|
86
|
4 |
|
|
|
87
|
4 |
|
$statement = $this->buildStatement(); |
|
88
|
|
|
$statement->execute($conditions); |
|
89
|
4 |
|
$result = $statement->fetchAll(); |
|
90
|
|
|
|
|
91
|
|
|
if (empty($result)) { |
|
92
|
|
|
return null; |
|
93
|
4 |
|
} |
|
94
|
|
|
|
|
95
|
|
|
return new ArrayObject($result[0]); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|