Test Failed
Pull Request — master (#11)
by Florian
07:04 queued 04:09
created

PdoResolver   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 75%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A buildStatement() 0 17 3
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
     * @param string $sql SQL String
46
     */
47 1
    public function __construct(PDO $pdo, string $sql)
48
    {
49 1
        $this->pdo = $pdo;
50 1
        $this->sql = $sql;
51
    }
52
53
    /**
54
     * Builds the statement
55
     *
56
     * @return \PDOStatement
57
     */
58 1
    protected function buildStatement(): PDOStatement
59
    {
60 1
        $statement = $this->pdo->prepare($this->sql);
61
62 1
        $error = $this->pdo->errorInfo();
63 1
        if ($error[0] !== '00000') {
64
            throw new PDOException($error[2], (int)$error[0]);
65
        }
66
67 1
        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
        }
73
74 1
        return $statement;
75
    }
76
77
    /**
78
     * {@inheritDoc}
79
     */
80 1
    public function find(array $conditions): ?ArrayAccess
81
    {
82 1
        foreach ($conditions as $key => $value) {
83 1
            unset($conditions[$key]);
84 1
            $conditions[':' . $key] = $value;
85
        }
86
87 1
        $statement = $this->buildStatement();
88 1
        $statement->execute($conditions);
89 1
        $result = $statement->fetchAll();
90
91 1
        if (empty($result)) {
92
            return null;
93
        }
94
95 1
        return new ArrayObject($result[0]);
96
    }
97
}
98