GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 23779b...f2432c )
by Aden
07:11
created

AdminManager::getSubAdminClasses()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 9.2
cc 4
eloc 10
nc 4
nop 1
1
<?php
2
3
namespace LaravelFlare\Flare\Admin;
4
5
use Illuminate\Support\Collection;
6
use LaravelFlare\Flare\Permissions\Permissions;
7
8
class AdminManager extends Collection
9
{
10
    /**
11
     * Base Class.
12
     *
13
     * The Base Class for Model Admin's
14
     */
15
    const BASE_CLASS = 'LaravelFlare\Flare\Admin\Admin';
16
17
    /**
18
     * Admin Config Key.
19
     *
20
     * Key which defined where in the Flare Admin Config to
21
     * load the Admin classes from.
22
     *
23
     * @var string
24
     */
25
    const ADMIN_KEY = 'admin';
26
27
    /**
28
     * __construct.
29
     */
30
    public function __construct()
31
    {
32
        parent::__construct();
33
34
        $this->items = $this->getAdminClasses();
35
    }
36
37
    /**
38
     * Gets Admin classes based on the current users permissions
39
     * which have been set. If a Admin class has not had the
40
     * Permissions provided, it will be displayed by default.
41
     * 
42
     * @return 
43
     */
44
    public function getAdminClasses()
45
    {
46
        $classCollection = [];
47
48
        if (!defined('static::ADMIN_KEY')) {
49
            return $classCollection;
50
        }
51
52
        $classCollection = $this->getSubAdminClasses(\Flare::config(static::ADMIN_KEY));
53
54
        return $classCollection;
55
    }
56
57
    /**
58
     * Takes an array of classes and returns the 
59
     * classes which are available with the 
60
     * current permissions/policy set.
61
     * 
62
     * @param  array  $classes
63
     * 
64
     * @return array         
65
     */
66
    public function getSubAdminClasses(array $classes)
67
    {
68
        $classCollection = [];
69
70
        foreach ($classes as $key => $class) {
71
            if ($this->usableClass($key)) {
72
                $classCollection[] = [$key => $this->getSubAdminClasses($class)];
73
                continue;
74
            }
75
76
            if ($this->usableClass($class)) {
77
                $classCollection[] = $class;
78
                continue;
79
            }
80
        }
81
82
        return $classCollection;
83
    }
84
85
    /**
86
     * Returns an instance of the Admin.
87
     * 
88
     * @return Admin
89
     */
90
    public static function getAdminInstance()
91
    {
92
        if (!$requested = Admin::getRequested()) {
93
            return;
94
        }
95
96
        return new $requested();
97
    }
98
99
    /**
100
     * Register Admin Routes.
101
     *
102
     * Loops through all of the Admin classes in the collection
103
     * and registers their Admin Routes.
104
     *
105
     * @return void
106
     */
107
    public function registerRoutes()
108
    {
109
        $this->registerSubRoutes($this->items);
110
    }
111
112
    /**
113
     * Loops through an array of classes
114
     * and registers their Route recursively.
115
     * 
116
     * @param  array  $classes
117
     * 
118
     * @return void
119
     */
120
    public function registerSubRoutes(array $classes)
121
    {
122
        foreach ($classes as $key => $class) {
123
            if (is_array($class)) {
124
                if ($this->usableClass($key)) {
125
                    $this->registerRoute($key);
126
                }
127
128
                $this->registerSubRoutes($class);
129
                continue;
130
            }
131
            $this->registerRoute($class);
132
        }
133
    }
134
135
    /**
136
     * Registers an individual group of Admin routes.
137
     * 
138
     * @param  string $class
139
     * 
140
     * @return void
141
     */
142
    public function registerRoute($class)
143
    {
144
        (new $class())->registerRoutes();
145
    }
146
147
    /**
148
     * Determines if a class is usable by the currently
149
     * defined user and their permission set.
150
     * 
151
     * @param string $class
152
     * 
153
     * @return bool
154
     */
155
    private function usableClass($class)
156
    {
157
        if (!is_scalar($class) || !class_exists($class)) {
158
            return false;
159
        }
160
161
        if ($class == static::BASE_CLASS) {
162
            return false;
163
        }
164
165
        if (!$this->checkUserHasAdminPermissions($class)) {
166
            return false;
167
        }
168
169
        return true;
170
    }
171
172
    /**
173
     * Checks if the current user has access to a given 
174
     * Admin class and returns a boolean.
175
     *
176
     * @param string $class
177
     * 
178
     * @return bool
179
     */
180
    private function checkUserHasAdminPermissions($class)
181
    {
182
        return Permissions::check($class, 'view');
183
    }
184
}
185