Completed
Push — master ( 62d9c0...305eb4 )
by Seth
06:56 queued 04:58
created

src/Snapshots/CacheableDatabase.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
58
            );
59
        }
60
        return $this->cache;
61
    }
62
}
63