Completed
Push — master ( 68e12f...ab20ed )
by Seth
02:02
created

History::cacheHistory()   C

Complexity

Conditions 7
Paths 12

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 13
nc 12
nop 0
dl 0
loc 25
rs 6.7272
c 0
b 0
f 0
1
<?php
2
/** History class */
3
4
namespace smtech\GradingAnalytics\Snapshots;
5
6
use smtech\GradingAnalytics\Snapshots\Exception\SnapshotException;
7
8
/**
9
 * A class to query and cache the history of a course's statistics
10
 *
11
 * @author Seth Battis <[email protected]>
12
 */
13
class History extends CacheableDatabase
14
{
15
    /**
16
     * The Canvas ID of the course
17
     * @var string|integer
18
     */
19
    protected $courseId;
20
21
    /**
22
     * The course history
23
     * @var array
24
     */
25
    protected static $data;
26
27
    /**
28
     * Construct a history object
29
     *
30
     * Note that the actual history will be queried and/or cached just in time
31
     * when needed, not during instantiation.
32
     *
33
     * @param \mysqli|\smtech\ReflexiveCanvasLTI\Toolbox|CacheableDatabase $databaseProvider
34
     *        An object containing a reusable mysqli access object
35
     * @param string|integer $courseId Canvas ID of the course
36
     *
37
     * @throws SnapshotException `SnapshotException::COURSE_ID` If `$courseId`
38
     *         does not appear to be a valid (numeric) Canvas course ID.
39
     */
40
    public function __construct($databaseProvider, $courseId)
41
    {
42
        parent::__construct($databaseProvider);
43
44
        if (is_numeric($courseId)) {
45
            $this->courseId = $courseId;
0 ignored issues
show
Documentation Bug introduced by Seth Battis
It seems like $courseId can also be of type double. However, the property $courseId is declared as type string|integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
46
        } else {
47
            throw new SnapshotException(
48
                'Numeric Course ID required',
49
                SnapshotException::COURSE_ID
50
            );
51
        }
52
    }
53
54
    /**
55
     * Get the numeric Canvas course ID
56
     *
57
     * @return string|integer
58
     */
59
    public function getCourseId()
60
    {
61
        return $this->courseId;
62
    }
63
64
    /**
65
     * Get the numeric Canvas account ID of the course's parent department
66
     *
67
     * @return string|integer|false Returns `FALSE` if no parent department is
68
     *         included in the most recent collected course statistics
69
     */
70
    public function getDepartmentId()
71
    {
72
        if ($this->cacheHistory()) {
73
            return (integer) static::$data[$this->getCourseID()][0]['course[account_id]'];
74
        }
75
        return false;
76
    }
77
78
    /**
79
     * Get the date of the most recently collected course statistics
80
     *
81
     * @return string|false The date in `YYYY-MM-DD` format or `FALSE` if no
82
     *                          timestamp is available.
83
     */
84
    public function getCurrentTimestamp()
85
    {
86
        if ($this->cacheHistory()) {
87
            return substr(static::$data[$this->getCourseId()][0]['timestamp'], 0, 10);
88
        }
89
        return false;
90
    }
91
92
    /**
93
     * Trigger a caching of the course history, if not already cached
94
     *
95
     * @return boolean `TRUE` if there is a non-empty cache of course
96
     *                        statistics to work with, `FALSE` otherwise
97
     */
98
    public function cacheHistory()
99
    {
100
        $courseId = $this->getCourseId();
101
        if (empty(static::$data[$courseId])) {
102
            static::$data = $this->getCache()->getCache($courseId);
0 ignored issues
show
Documentation Bug introduced by Seth Battis
It seems like $this->getCache()->getCache($courseId) of type boolean is incompatible with the declared type array of property $data.

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...
103
            if (empty($this->data)) {
104
                if ($response = $this->sql->query("
105
                    SELECT * FROM `course_statistics`
106
                        WHERE
107
                            `course[id]` = '$courseId'
108
                        ORDER BY
109
                            `timestamp` DESC
110
                ")) {
111
                    while ($row = $response->fetch_assoc()) {
112
                        static::$data[$courseId][] = $row;
113
                    }
114
                    $this->getCache()->setCache($courseId, static::$data[$courseId]);
115
                }
116
            }
117
        }
118
        return (is_array(static::$data) &&
119
            is_array(static::$data[$courseId]) &&
120
            count(static::$data[$courseId]) > 0
121
        );
122
    }
123
124
    /**
125
     * Get the course's history of statistics
126
     *
127
     * @link https://smtech.github.io/grading-analytics/definitions.html Online
128
     *       documentation of course statistic fields
129
     *
130
     * @return array|false One row per course statistic collected daily, as
131
     *                         described in the documentation, or `FALSE` if no
132
     *                         statistics are available
133
     */
134
    public function getHistory()
135
    {
136
        if ($this->cacheHistory()) {
137
            return static::$data[$this->getCourseId()];
138
        }
139
        return false;
140
    }
141
}
142