PdoStatementResolver   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
eloc 11
c 2
b 0
f 0
dl 0
loc 37
ccs 12
cts 12
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A find() 0 15 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 PDOStatement;
23
24
/**
25
 * A simple php PDO Statement Resolver
26
 *
27
 * This should work with any system that is using PDO as a base an provides
28
 * access to the PDO object.
29
 */
30
class PdoStatementResolver implements ResolverInterface
31
{
32
    /**
33
     * Prepared statement
34
     *
35
     * @var \PDOStatement
36
     */
37
    protected PDOStatement $statement;
38
39
    /**
40
     * Constructor.
41 12
     *
42
     * @param \PDOStatement $statement A prepared statement to query the DB
43 12
     */
44 12
    public function __construct(PDOStatement $statement)
45
    {
46
        $this->statement = $statement;
47
    }
48
49 12
    /**
50
     * {@inheritDoc}
51 12
     */
52 12
    public function find(array $conditions): ?ArrayAccess
53 12
    {
54
        foreach ($conditions as $key => $value) {
55
            unset($conditions[$key]);
56 12
            $conditions[':' . $key] = $value;
57 12
        }
58
59 12
        $this->statement->execute($conditions);
60 4
        $result = $this->statement->fetchAll();
61
62
        if (empty($result)) {
63 8
            return null;
64
        }
65
66
        return new ArrayObject($result[0]);
67
    }
68
}
69