AbstractDatabase   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 1
dl 0
loc 64
ccs 12
cts 18
cp 0.6667
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A find() 0 10 2
A getLogger() 0 4 1
A setLogger() 0 8 2
lastIdCreated() 0 1 ?
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