Resources   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 193
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 5
Bugs 1 Features 0
Metric Value
eloc 70
c 5
b 1
f 0
dl 0
loc 193
ccs 0
cts 75
cp 0
rs 10
wmc 22

9 Methods

Rating   Name   Duplication   Size   Complexity  
A registerModule() 0 12 2
A isActive() 0 18 3
A __construct() 0 3 1
A findOrCreateGroup() 0 8 2
A makeNavigation() 0 40 5
A navigableResource() 0 3 2
A validateContainer() 0 13 2
A collectModules() 0 9 1
A handle() 0 17 4
1
<?php
2
3
namespace Terranet\Administrator\Middleware;
4
5
use Closure;
6
use Illuminate\Contracts\Foundation\Application;
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Collection;
9
use Illuminate\Support\Str;
10
use Pingpong\Menus\Menu;
11
use Pingpong\Menus\MenuBuilder;
12
use Terranet\Administrator\Contracts\Module;
13
use Terranet\Administrator\Contracts\Module\Navigable;
14
use Terranet\Administrator\Traits\ResolvesClasses;
15
16
class Resources
17
{
18
    use ResolvesClasses;
19
20
    /**
21
     * @var Application
22
     */
23
    protected $application;
24
25
    /**
26
     * @param Application $application
27
     */
28
    public function __construct(Application $application)
29
    {
30
        $this->application = $application;
31
    }
32
33
    /**
34
     * Handle an incoming request.
35
     *
36
     * @param Request $request
37
     * @param Closure $next
38
     *
39
     * @return mixed
40
     */
41
    public function handle(Request $request, Closure $next)
42
    {
43
        $this->collectModules(function ($fileInfo) {
44
            $this->resolveClass($fileInfo, function ($module) {
45
                if ($module instanceof Module) {
46
                    $this->registerModule($module);
47
                }
48
49
                if ($this->navigableResource($module)
50
                    && ($navigation = $this->application->make('scaffold.navigation'))
51
                ) {
52
                    $this->makeNavigation($module, $navigation);
53
                }
54
            });
55
        });
56
57
        return $next($request);
58
    }
59
60
    /**
61
     * Make sure the resource is Navigable && Should be visible.
62
     *
63
     * @param $module
64
     *
65
     * @return bool
66
     */
67
    protected function navigableResource($module)
68
    {
69
        return $module instanceof Navigable && $module->showIf();
70
    }
71
72
    protected function collectModules(Closure $callback)
73
    {
74
        $resources = Collection::make($this->application['files']->allFiles(
75
            app_path($this->application['scaffold.config']->get('paths.module'))
0 ignored issues
show
Bug introduced by
The function app_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

75
            /** @scrutinizer ignore-call */ 
76
            app_path($this->application['scaffold.config']->get('paths.module'))
Loading history...
76
        ))->each($callback);
77
78
        $this->application->instance('scaffold.resources', $resources);
79
80
        return $resources;
81
    }
82
83
    /**
84
     * @param $module
85
     *
86
     * @return $this
87
     */
88
    protected function registerModule(Module $module)
89
    {
90
        $modules = $this->application['scaffold.modules'] ?? collect();
91
92
        if (!$modules->contains($module)) {
93
            $modules->push($module);
94
        }
95
        $this->application->instance('scaffold.modules', $modules);
96
97
        $this->application->instance("scaffold.module.{$module->url()}", $module);
98
99
        return $this;
100
    }
101
102
    /**
103
     * @param Module $module
104
     * @param $navigation
105
     */
106
    protected function makeNavigation(Module $module, $navigation)
107
    {
108
        // Force ordering menus
109
        $navigation = $navigation->instance(
110
            $this->validateContainer($module, $navigation)
111
        );
112
113
        $order = $module->order() ?: 1;
0 ignored issues
show
Bug introduced by
The method order() does not exist on Terranet\Administrator\Contracts\Module. Since it exists in all sub-types, consider adding an abstract or default implementation to Terranet\Administrator\Contracts\Module. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

113
        $order = $module->/** @scrutinizer ignore-call */ order() ?: 1;
Loading history...
114
115
        if ($group = $module->group()) {
0 ignored issues
show
Bug introduced by
The method group() does not exist on Terranet\Administrator\Contracts\Module. Since it exists in all sub-types, consider adding an abstract or default implementation to Terranet\Administrator\Contracts\Module. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

115
        if ($group = $module->/** @scrutinizer ignore-call */ group()) {
Loading history...
116
            $navigation = $this->findOrCreateGroup($module, $navigation, $group);
117
118
            $order = $module->order() ?: \count($navigation->getChilds()) + 1;
0 ignored issues
show
Bug introduced by
The method getChilds() does not exist on Pingpong\Menus\MenuBuilder. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

118
            $order = $module->order() ?: \count($navigation->/** @scrutinizer ignore-call */ getChilds()) + 1;

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
119
        }
120
121
        if (Navigable::AS_LINK === $module->showAs()) {
0 ignored issues
show
Bug introduced by
The method showAs() does not exist on Terranet\Administrator\Contracts\Module. Since it exists in all sub-types, consider adding an abstract or default implementation to Terranet\Administrator\Contracts\Module. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

121
        if (Navigable::AS_LINK === $module->/** @scrutinizer ignore-call */ showAs()) {
Loading history...
122
            $navigation->route(
123
                'scaffold.index',
124
                $module->title(),
125
                array_merge(['module' => $module->url()], $module->navigableParams()),
0 ignored issues
show
Bug introduced by
The method navigableParams() does not exist on Terranet\Administrator\Contracts\Module. Since it exists in all sub-types, consider adding an abstract or default implementation to Terranet\Administrator\Contracts\Module. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

125
                array_merge(['module' => $module->url()], $module->/** @scrutinizer ignore-call */ navigableParams()),
Loading history...
126
                $order,
127
                array_merge(
128
                    $module->linkAttributes(),
0 ignored issues
show
Bug introduced by
The method linkAttributes() does not exist on Terranet\Administrator\Contracts\Module. Since it exists in all sub-types, consider adding an abstract or default implementation to Terranet\Administrator\Contracts\Module. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

128
                    $module->/** @scrutinizer ignore-call */ 
129
                             linkAttributes(),
Loading history...
129
                    [
130
                        'active' => function () use ($module) {
131
                            return $this->isActive($module);
132
                        },
133
                    ]
134
                )
135
            );
136
        } else {
137
            $navigation->url(
138
                '#',
139
                $module->title(),
140
                $order,
141
                array_merge(
142
                    $module->linkAttributes(),
143
                    [
144
                        'active' => function () use ($module) {
145
                            return $module->showIf();
0 ignored issues
show
Bug introduced by
The method showIf() does not exist on Terranet\Administrator\Contracts\Module. Since it exists in all sub-types, consider adding an abstract or default implementation to Terranet\Administrator\Contracts\Module. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

145
                            return $module->/** @scrutinizer ignore-call */ showIf();
Loading history...
146
                        },
147
                    ]
148
                )
149
            );
150
        }
151
    }
152
153
    /**
154
     * @param $module
155
     * @param $navigation
156
     *
157
     * @return mixed
158
     */
159
    protected function validateContainer(Module $module, Menu $navigation)
160
    {
161
        $container = $module->navigableIn();
0 ignored issues
show
Bug introduced by
The method navigableIn() does not exist on Terranet\Administrator\Contracts\Module. Since it exists in all sub-types, consider adding an abstract or default implementation to Terranet\Administrator\Contracts\Module. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

161
        /** @scrutinizer ignore-call */ 
162
        $container = $module->navigableIn();
Loading history...
162
163
        if (!array_key_exists($container, $navigation->all())) {
164
            $message =
165
                "Can not add \"{$module->title()}\" to \"{$container}\" menu. Available menus: ".
166
                implode(', ', array_keys($navigation->all())).'.';
167
168
            throw new \InvalidArgumentException($message);
169
        }
170
171
        return $container;
172
    }
173
174
    /**
175
     * @param $module
176
     * @param $navigation
177
     * @param $group
178
     *
179
     * @return mixed
180
     */
181
    protected function findOrCreateGroup(Module $module, MenuBuilder $navigation, $group)
182
    {
183
        if (!$sub = $navigation->whereTitle($group)) {
184
            $sub = $navigation->dropdown($group, function () {
185
            }, 99, ['id' => $module->url(), 'icon' => 'fa fa-folder']);
186
        }
187
188
        return $sub;
189
    }
190
191
    protected function isActive($module)
192
    {
193
        static $checked = [];
194
        $module = $module->url();
195
196
        if (!array_key_exists($module, $checked)) {
197
            $urls = array_map(function ($url) {
198
                return trim($url, '/');
199
            }, [
200
                'current' => \URL::getRequest()->getPathInfo(),
201
                'create' => route('scaffold.create', ['module' => $module], false),
0 ignored issues
show
Bug introduced by
The function route was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

201
                'create' => /** @scrutinizer ignore-call */ route('scaffold.create', ['module' => $module], false),
Loading history...
202
                'module' => config('administrator.prefix')."/{$module}",
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

202
                'module' => /** @scrutinizer ignore-call */ config('administrator.prefix')."/{$module}",
Loading history...
203
            ]);
204
205
            $checked[$module] = Str::startsWith($urls['current'], $urls['module']) && ($urls['current'] !== $urls['create']);
206
        }
207
208
        return $checked[$module];
209
    }
210
}
211