Completed
Push — develop ( 305eb4...62d9c0 )
by Seth
03:21
created

History::getHistory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace smtech\GradingAnalytics\Snapshots;
4
5
use smtech\GradingAnalytics\Snapshots\Exception\SnapshotException;
6
7
class History extends CacheableDatabase
8
{
9
    protected $courseId;
10
11
    protected static $data;
12
13
    public function __construct($databaseProvider, $courseId)
14
    {
15
        parent::__construct($databaseProvider);
16
17
        if (is_numeric($courseId)) {
18
            $this->courseId = $courseId;
19
        } else {
20
            throw new SnapshotException(
21
                'Numeric Course ID required',
22
                SnapshotException::COURSE_ID
23
            );
24
        }
25
    }
26
27
    public function getCourseId()
28
    {
29
        return $this->courseId;
30
    }
31
32
    public function getDepartmentId()
33
    {
34
        if ($this->cacheHistory()) {
35
            return (integer) static::$data[$this->getCourseID()][0]['course[account_id]'];
36
        }
37
        return false;
38
    }
39
40
    public function getCurrentTimestamp()
41
    {
42
        if ($this->cacheHistory()) {
43
            return substr(static::$data[$this->getCourseId()][0]['timestamp'], 0, 10);
44
        }
45
        return false;
46
    }
47
48
    public function cacheHistory()
49
    {
50
        if (empty(static::$data[$this->getCourseId()])) {
51
            static::$data = $this->getCache()->getCache($this->getCourseId());
52
            if (empty($this->data)) {
53
                if ($response = $this->sql->query("
54
                    SELECT * FROM `course_statistics`
55
                        WHERE
56
                            `course[id]` = '" . $this->getCourseId() . "'
57
                        ORDER BY
58
                            `timestamp` DESC
59
                ")) {
60
                    while ($row = $response->fetch_assoc()) {
61
                        static::$data[$this->getCourseId()][] = $row;
62
                    }
63
                    $this->getCache()->setCache($this->getCourseId(), static::$data[$This->getCourseId()]);
0 ignored issues
show
Bug introduced by
The variable $This does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
64
                }
65
            }
66
        }
67
        return (is_array(static::$data) &&
68
            is_array(static::$data[$this->getCourseId()]) &&
69
            count(static::$data[$this->getCourseId()]) > 0
70
        );
71
    }
72
73
    public function getHistory()
74
    {
75
        if ($this->cacheHistory()) {
76
            return $this->data;
77
        }
78
        return false;
79
    }
80
}
81