Completed
Push — develop ( 79e117...34fa42 )
by Seth
02:25
created

Toolbox   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 114
rs 10
wmc 14
lcom 2
cbo 3

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getGenerator() 0 17 1
A getAccountList() 0 21 3
A getTermList() 0 22 3
A blank() 0 8 2
B isAcademic() 0 13 5
1
<?php
2
3
namespace smtech\AdvisorDashboard;
4
5
use smtech\LTI\Configuration\Option;
6
use Battis\HierarchicalSimpleCache;
7
8
/**
9
 * Advisor Dashboard toolbox
10
 *
11
 * Adds some common, useful methods to the St. Mark's-styled
12
 * ReflexiveCanvasLTI Toolbox
13
 *
14
 * @author  Seth Battis <[email protected]>
15
 * @version v1.2
16
 */
17
class Toolbox extends \smtech\StMarksReflexiveCanvasLTI\Toolbox
18
{
19
20
    /**
21
     * Configure course and account navigation placements
22
     *
23
     * @return Generator
24
     */
25
    public function getGenerator()
26
    {
27
        parent::getGenerator();
28
29
        $this->generator->setOptionProperty(
30
            Option::COURSE_NAVIGATION(),
31
            'visibility',
32
            'admins'
33
        );
34
        $this->generator->setOptionProperty(
35
            Option::ACCOUNT_NAVIGATION(),
36
            'visibility',
37
            'admins'
38
        );
39
40
        return $this->generator;
41
    }
42
43
    /**
44
     * Get a listing of all accounts organized for presentation in a select picker
45
     *
46
     * @return array
47
     **/
48
    public function getAccountList()
49
    {
50
        $cache = new HierarchicalSimpleCache($this->getMySQL(), __CLASS__);
51
52
        $accounts = $cache->getCache('accounts');
53
        if ($accounts === false) {
54
            $accountsResponse = $this->api_get(
55
                'accounts/1/sub_accounts',
56
                [
57
                    'recursive' => 'true'
58
                ]
59
            );
60
            $accounts = [];
61
            foreach ($accountsResponse as $account) {
0 ignored issues
show
Bug introduced by
The expression $accountsResponse of type object<smtech\CanvasPest...CanvasPest\CanvasArray> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
62
                $accounts[$account['id']] = $account;
63
            }
64
            $cache->setCache('accounts', $accounts);
65
        }
66
67
        return $accounts;
68
    }
69
70
    /**
71
     * Get a listing of all terms organized for presentation in a select picker
72
     *
73
     * @return array
74
     **/
75
    public function getTermList()
76
    {
77
        $cache = new HierarchicalSimpleCache($this->getMySQL(), __CLASS__);
78
79
        $terms = $cache->getCache('terms');
80
        if ($terms === false) {
81
            $_terms = $this->api_get(
82
                'accounts/1/terms',
83
                [
84
                    'workflow_state' => 'active'
85
                ]
86
            );
87
            $termsResponse = $_terms['enrollment_terms'];
88
            $terms = [];
89
            foreach ($termsResponse as $term) {
90
                $terms[$term['id']] = $term;
91
            }
92
            $cache->setCache('terms', $terms);
93
        }
94
95
        return $terms;
96
    }
97
98
    /**
99
     * @param array $arr
100
     * @param string|integer $key
101
     * @return string `$arr[$key]` if present, `''` otherwise
102
     */
103
    public function blank($arr, $key)
104
    {
105
        if (empty($arr[$key])) {
106
            return '';
107
        } else {
108
            return $arr[$key];
109
        }
110
    }
111
112
    /**
113
     * @param integer $account Canvas account ID
114
     * @return boolean `TRUE` if the account is a child of the Academics
115
     *                        account, `FALSE` otherwise
116
     */
117
    public function isAcademic($account)
118
    {
119
        if ($account == 132) { // FIXME really, hard-coded values? Really?
120
            return true;
121
        } elseif ($account == 1 || !is_integer($account)) {
122
            return false;
123
        } else {
124
            if (empty($this->accounts)) {
125
                $this->accounts = $this->getAccountList();
0 ignored issues
show
Bug introduced by
The property accounts does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
126
            }
127
            return isAcademic($this->accounts[$account]['parent_account_id']);
128
        }
129
    }
130
}
131