AbstractDatabase::find()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 3
crap 2
1
<?php
2
namespace DAL;
3
4
use \Psr\Log\LoggerAwareInterface;
5
use \Psr\Log\LoggerInterface;
6
use \Psr\Log\NullLogger;
7
8
abstract class AbstractDatabase implements LoggerAwareInterface, ReadableInterface, WritableInterface
9
{
10
11
    private $logger;
12
13
    /**
14
     * Instantiates the AbstractDatabase. Should be called from inheritors.
15
     */
16 40
    public function __construct()
17
    {
18 40
        $this->logger = new NullLogger();
19 40
    }
20
21
    /**
22
     * @param string         $table The table that will be accessed and written.
23
     * @param Condition|null $criteria The criteria that will filter the records.
24
     * @param array          $options The list of options that will help with finding the records.
25
     * @return array|null One record from table, or null if there aren't any matching records.
26
     */
27 3
    public function find($table, Condition $criteria = null, array $options = [])
28
    {
29 3
        $options['limit'] = 1;
30 3
        $data             = $this->findAll($table, $criteria, $options);
31 3
        $result           = reset($data);
32 3
        if ($result === false) {
33 1
            return null;
34
        }
35 2
        return $result;
36
    }
37
38
    /**
39
     * Returns the current logger object.
40
     *
41
     * @return LoggerInterface
42
     */
43 2
    public function getLogger()
44
    {
45 2
        return $this->logger;
46
    }
47
48
    /**
49
     * Sets the logger to $value.
50
     *
51
     * @param LoggerInterface $value A logger to be used for this database.
52
     * @return void
53
     */
54
    public function setLogger(LoggerInterface $value)
55
    {
56
        if (null === $value) {
57
            $value = new NullLogger();
58
        }
59
60
        $this->logger = $value;
61
    }
62
63
    /**
64
     * Returns the id of the last record created, or null if no creation took place.
65
     *
66
     * Must be implemented by inheritors.
67
     *
68
     * @return mixed
69
     */
70
    abstract public function lastIdCreated();
71
}
72