1
|
|
|
<?php |
2
|
|
|
/** CacheableDatabase class */ |
3
|
|
|
|
4
|
|
|
namespace smtech\GradingAnalytics\Snapshots; |
5
|
|
|
|
6
|
|
|
use mysqli; |
7
|
|
|
use smtech\ReflexiveCanvasLTI\Toolbox; |
8
|
|
|
use smtech\GradingAnalytics\Snapshots\Exception\SnapshotException; |
9
|
|
|
use Battis\HierarchicalSimpleCache; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* An object that has access to a MySQL database and a cache. |
13
|
|
|
* |
14
|
|
|
* @author Seth Battis <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class CacheableDatabase |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* MySQL database access |
20
|
|
|
* @var mysqli |
21
|
|
|
*/ |
22
|
|
|
protected $sql; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Caching for queried data |
26
|
|
|
* @var HierarchicalSimpleCache |
27
|
|
|
*/ |
28
|
|
|
protected $cache; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Construct a CacheableDatabase object |
32
|
|
|
* |
33
|
|
|
* Note that the cache is instantiated just in time, and will not be created |
34
|
|
|
* until its first use. |
35
|
|
|
* |
36
|
|
|
* @param mysqli|Toolbox|CacheableDatabase $databaseProvider An object |
37
|
|
|
* containing a mysqli object that can be reused |
38
|
|
|
* |
39
|
|
|
* @throws SnapshotException `SnapshotException::MYSQL` If a valid mysqli |
40
|
|
|
* database object cannot be extracted from the `$databaseProvider` |
41
|
|
|
*/ |
42
|
|
|
public function __construct($databaseProvider) |
43
|
|
|
{ |
44
|
|
|
if (is_a($databaseProvider, mysqli::class)) { |
45
|
|
|
$this->sql = $databaseProvider; |
46
|
|
|
} elseif (is_a($databaseProvider, CacheableDatabase::class)) { |
47
|
|
|
$this->sql = $databaseProvider->sql; |
48
|
|
|
} elseif (is_a($databaseProvider, Toolbox::class)) { |
49
|
|
|
$this->sql = $databaseProvider->getMySql(); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if (!is_a($this->sql, mysqli::class)) { |
53
|
|
|
throw new SnapshotException( |
54
|
|
|
'Database provider not recognized (instance of ' . (is_object($databaseProvider) ? |
55
|
|
|
get_class($databaseProvider) : |
56
|
|
|
gettype($databaseProvider) |
57
|
|
|
) .')', |
58
|
|
|
SnapshotException::MYSQL |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* MySQL access |
65
|
|
|
* |
66
|
|
|
* @return mysqli |
67
|
|
|
*/ |
68
|
|
|
protected function getMySql() |
69
|
|
|
{ |
70
|
|
|
return $this->sql; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Cache access |
75
|
|
|
* |
76
|
|
|
* @return HierarchicalSimpleCache |
77
|
|
|
*/ |
78
|
|
|
protected function getCache() |
79
|
|
|
{ |
80
|
|
|
if (empty($this->cache)) { |
81
|
|
|
$this->cache = new HierarchicalSimpleCache( |
82
|
|
|
$this->getMySql(), |
83
|
|
|
get_class($this) |
84
|
|
|
); |
85
|
|
|
$this->cache->purgeExpired(); |
86
|
|
|
} |
87
|
|
|
return $this->cache; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: