Toolbox::getGenerator()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
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
     * Store a listing of Canvas accounts for quick access
21
     * @var \smtech\CanvasPest\CanvasObject[]
22
     */
23
    private $accounts;
24
25
    /**
26
     * Configure course and account navigation placements
27
     *
28
     * @return Generator
29
     */
30
    public function getGenerator()
31
    {
32
        parent::getGenerator();
33
34
        $this->generator->setOptionProperty(
35
            Option::COURSE_NAVIGATION(),
36
            'visibility',
37
            'admins'
38
        );
39
        $this->generator->setOptionProperty(
40
            Option::ACCOUNT_NAVIGATION(),
41
            'visibility',
42
            'admins'
43
        );
44
45
        return $this->generator;
46
    }
47
48
    /**
49
     * Get a listing of all accounts organized for presentation in a select picker
50
     *
51
     * @return array
52
     **/
53
    public function getAccountList()
54
    {
55
        $cache = new HierarchicalSimpleCache($this->getMySQL(), __CLASS__);
56
57
        $accounts = $cache->getCache('accounts');
58
        if ($accounts === false) {
59
            $accountsResponse = $this->api_get(
60
                'accounts/1/sub_accounts',
61
                [
62
                    'recursive' => 'true'
63
                ]
64
            );
65
            $accounts = [];
66
            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...
67
                $accounts[$account['id']] = $account;
68
            }
69
            $cache->setCache('accounts', $accounts);
70
        }
71
72
        return $accounts;
73
    }
74
75
    /**
76
     * Get a listing of all terms organized for presentation in a select picker
77
     *
78
     * @return array
79
     **/
80
    public function getTermList()
81
    {
82
        $cache = new HierarchicalSimpleCache($this->getMySQL(), __CLASS__);
83
84
        $terms = $cache->getCache('terms');
85
        if ($terms === false) {
86
            $_terms = $this->api_get(
87
                'accounts/1/terms',
88
                [
89
                    'workflow_state' => 'active'
90
                ]
91
            );
92
            $termsResponse = $_terms['enrollment_terms'];
93
            $terms = [];
94
            foreach ($termsResponse as $term) {
95
                $terms[$term['id']] = $term;
96
            }
97
            $cache->setCache('terms', $terms);
98
        }
99
100
        return $terms;
101
    }
102
103
    /**
104
     * @param array $arr
105
     * @param string|integer $key
106
     * @return string `$arr[$key]` if present, `''` otherwise
107
     */
108
    public function blank($arr, $key)
109
    {
110
        if (empty($arr[$key])) {
111
            return '';
112
        } else {
113
            return $arr[$key];
114
        }
115
    }
116
117
    /**
118
     * @param integer $account Canvas account ID
119
     * @return boolean `TRUE` if the account is a child of the Academics
120
     *                        account, `FALSE` otherwise
121
     */
122
    public function isAcademic($account)
123
    {
124
        if ($account == 132) { // FIXME really, hard-coded values? Really?
125
            return true;
126
        } elseif ($account == 1 || !is_integer($account)) {
127
            return false;
128
        } else {
129
            if (empty($this->accounts)) {
130
                $this->accounts = $this->getAccountList();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getAccountList() can also be of type boolean. However, the property $accounts is declared as type array<integer,object<smt...nvasPest\CanvasObject>>. 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...
131
            }
132
            return $this->isAcademic($this->accounts[$account]['parent_account_id']);
133
        }
134
    }
135
}
136