Passed
Push — master ( b4f342...485653 )
by Florian
01:34 queued 10s
created

PdoStatementResolver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
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
namespace Phauthentic\Authentication\Identifier\Resolver;
15
16
use ArrayAccess;
17
use ArrayObject;
18
use PDO;
19
use PDOStatement;
20
21
/**
22
 * A simple php PDO Statement Resolver
23
 *
24
 * This should work with any system that is using PDO as a base an provides
25
 * access to the PDO object.
26
 */
27
class PdoStatementResolver implements ResolverInterface
28
{
29
    /**
30
     * Prepared statement
31
     *
32
     * @var \PDOStatement
33
     */
34
    protected $statement;
35
36
    /**
37
     * Constructor.
38
     *
39
     * @param \PDOStatement $statement A prepared statement to query the DB
40
     */
41 9
    public function __construct(PDOStatement $statement)
42
    {
43 9
        $this->statement = $statement;
44 9
    }
45
46
    /**
47
     * {@inheritDoc}
48
     */
49 9
    public function find(array $conditions): ?ArrayAccess
50
    {
51 9
        foreach ($conditions as $key => $value) {
52 9
            unset($conditions[$key]);
53 9
            $conditions[':' . $key] = $value;
54
        }
55
56 9
        $this->statement->execute($conditions);
57 9
        $result = $this->statement->fetchAll();
58
59 9
        if (empty($result)) {
60 3
            return null;
61
        }
62
63 6
        return new ArrayObject($result[0]);
64
    }
65
}
66