PdoResolver   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 79.17%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 7
eloc 23
c 4
b 0
f 0
dl 0
loc 67
ccs 19
cts 24
cp 0.7917
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A buildStatement() 0 17 3
A __construct() 0 4 1
A find() 0 16 3
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) {
0 ignored issues
show
introduced by
$statement is always a sub-type of PDOStatement.
Loading history...
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