Passed
Pull Request — master (#4)
by Florian
06:02 queued 03:28
created

PdoStatementResolver::find()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 15
ccs 0
cts 9
cp 0
rs 10
c 0
b 0
f 0
cc 3
nc 4
nop 1
crap 12
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 A prepared statement to query the DB
0 ignored issues
show
Bug introduced by
The type Phauthentic\Authentication\Identifier\Resolver\A was not found. Did you mean A? If so, make sure to prefix the type with \.
Loading history...
40
     */
41
    public function __construct(PDOStatement $statement)
42
    {
43
        $this->statement = $statement;
44
    }
45
46
    /**
47
     * {@inheritDoc}
48
     */
49
    public function find(array $conditions): ?ArrayAccess
50
    {
51
        foreach ($conditions as $key => $value) {
52
            unset($conditions[$key]);
53
            $conditions[':' . $key] = $value;
54
        }
55
56
        $this->statement->execute($conditions);
57
        $result = $this->statement->fetchAll();
58
59
        if (empty($result)) {
60
            return null;
61
        }
62
63
        return new ArrayObject($result[0]);
64
    }
65
}
66