Completed
Push — master ( 505b4c...62d9c0 )
by Seth
05:17 queued 03:12
created

CacheableDatabase   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 53
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 21 6
A getMySql() 0 4 1
A getCache() 0 11 2
1
<?php
2
3
namespace smtech\GradingAnalytics\Snapshots;
4
5
use mysqli;
6
use smtech\ReflexiveCanvasLTI\Toolbox;
7
use smtech\GradingAnalytics\Snapshots\Exception\SnapshotException;
8
use Battis\HierarchicalSimpleCache;
9
10
class CacheableDatabase
11
{
12
    /**
13
     * MySQL database access
14
     * @var mysqli
15
     */
16
    protected $sql;
17
18
    /**
19
     * Caching for queried data
20
     * @var HierarchicalSimpleCache
21
     */
22
    protected $cache;
23
24
    public function __construct($databaseProvider)
25
    {
26
        $this->sql = false;
0 ignored issues
show
Documentation Bug introduced by
It seems like false of type false is incompatible with the declared type object<mysqli> of property $sql.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
27
        if (is_a($databaseProvider, mysqli::class)) {
28
            $this->sql = $databaseProvider;
29
        } elseif (is_a($databaseProvider, CacheableDatabase::class)) {
30
            $this->sql = $databaseProvider->sql;
31
        } elseif (is_a($databaseProvider, Toolbox::class)) {
32
            $this->sql = $databaseProvider->getMySql();
33
        }
34
35
        if (!is_a($this->sql, mysqli::class)) {
36
            throw new SnapshotException(
37
                'Database provider not recognized (instance of ' . (is_object($databaseProvider) ?
38
                        get_class($databaseProvider) :
39
                        gettype($databaseProvider)
40
                    ) .')',
41
                SnapshotException::MYSQL
42
            );
43
        }
44
    }
45
46
    protected function getMySql()
47
    {
48
        return $this->sql;
49
    }
50
51
    protected function getCache()
52
    {
53
        if (empty($this->cache)) {
54
            $this->cache = new HierarchicalSimpleCache(
55
                $this->getMySql(),
56
                get_class($this),
57
                true
0 ignored issues
show
Unused Code introduced by
The call to HierarchicalSimpleCache::__construct() has too many arguments starting with true.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
58
            );
59
        }
60
        return $this->cache;
61
    }
62
}
63