Completed
Push — develop ( fe6449...98c976 )
by Abdelrahman
11:45
created

MenuManager::destroy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Menus\Models;
6
7
use Closure;
8
use Countable;
9
use ReflectionFunction;
10
use Illuminate\Routing\Router;
11
use Illuminate\Support\Facades\Route;
12
use Rinvex\Menus\Models\MenuGenerator;
13
use Illuminate\Contracts\View\Factory as ViewFactory;
14
15
class MenuManager implements Countable
16
{
17
    /**
18
     * The menus collection.
19
     *
20
     * @var \Illuminate\Support\Collection
21
     */
22
    protected $menus;
23
24
    /**
25
     * The view factory.
26
     *
27
     * @var \Illuminate\Contracts\View\Factory
28
     */
29
    protected $viewFactory;
30
31
    /**
32
     * The router instance.
33
     *
34
     * @var \Illuminate\Routing\Router
35
     */
36
    protected $router;
37
38
    /**
39
     * The menu factory.
40
     *
41
     * @var \Rinvex\Menus\Models\MenuGenerator
42
     */
43
    protected $generator;
44
45
    /**
46
     * The menu callbacks.
47
     *
48
     * @var array
49
     */
50
    protected $callbacks = [];
51
52
    /**
53
     * The constructor.
54
     *
55
     * @param \Illuminate\Contracts\View\Factory $viewFactory
56
     * @param \Illuminate\Routing\Router         $router
57
     */
58
    public function __construct(ViewFactory $viewFactory, Router $router)
59
    {
60
        $this->viewFactory = $viewFactory;
61
        $this->callbacks = collect();
0 ignored issues
show
Documentation Bug introduced by
It seems like collect() of type object<Illuminate\Support\Collection> is incompatible with the declared type array of property $callbacks.

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...
62
        $this->menus = collect();
63
        $this->router = $router;
64
    }
65
66
    /**
67
     * Check if the menu exists.
68
     *
69
     * @param string $name
70
     *
71
     * @return bool
72
     */
73
    public function has($name): bool
74
    {
75
        return $this->menus->has($name);
76
    }
77
78
    /**
79
     * Get instance of the given menu if exists.
80
     *
81
     * @param string $name
82
     *
83
     * @return \Rinvex\Menus\Models\MenuGenerator|null
84
     */
85
    public function instance($name): ?MenuGenerator
86
    {
87
        return $this->menus->get($name);
88
    }
89
90
    /**
91
     * Register a menu.
92
     *
93
     * @param string   $name
94
     * @param \Closure $callback
95
     *
96
     * @return void
97
     */
98
    public function register($name, Closure $callback): void
99
    {
100
        if ($this->has($name)) {
101
            $this->callbacks = $this->callbacks->put($name, $this->callbacks->get($name, collect())->push($callback));
0 ignored issues
show
Bug introduced by
The method get cannot be called on $this->callbacks (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
Bug introduced by
The method put cannot be called on $this->callbacks (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
102
        } else {
103
            $builder = new MenuGenerator();
104
105
            $builder->setViewFactory($this->viewFactory);
0 ignored issues
show
Compatibility introduced by
$this->viewFactory of type object<Illuminate\Contracts\View\Factory> is not a sub-type of object<Illuminate\View\Factory>. It seems like you assume a concrete implementation of the interface Illuminate\Contracts\View\Factory to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
106
107
            $this->menus->put($name, $builder);
108
109
            $this->callbacks = $this->callbacks->put($name, $this->callbacks->get($name, collect())->push($callback));
0 ignored issues
show
Bug introduced by
The method get cannot be called on $this->callbacks (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
Bug introduced by
The method put cannot be called on $this->callbacks (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
110
        }
111
    }
112
113
    /**
114
     * Render the menu tag by given name.
115
     *
116
     * @param string $name
117
     * @param string $presenter
0 ignored issues
show
Documentation introduced by
Should the type for parameter $presenter not be null|string?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
118
     * @param array  $bindings
119
     * @param bool   $specialSidebar
120
     *
121
     * @return string|null
122
     */
123
    public function render(string $name, string $presenter = null, array $bindings = [], bool $specialSidebar = false): ?string
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 127 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
124
    {
125
        if ($this->has($name)) {
126
            $instance = $this->instance($name);
127
128
            $this->callbacks->get($name)->each(function ($callback) use ($instance) {
0 ignored issues
show
Bug introduced by
The method get cannot be called on $this->callbacks (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
129
                $reflectionParams = collect((new ReflectionFunction($callback))->getParameters());
130
                $reflectionParams->shift();
131
132
                collect($reflectionParams)->each(function ($param) use (&$params) {
133
                    $params[] = Route::current()->hasParameter($param->getName()) ? Route::current()->parameter($param->getName())
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 130 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
134
                        : (class_exists($param->getClass()->getName()) ? app($param->getClass()->getName()) : null);
135
                });
136
137
                $params ? $callback($instance, ...$params) : $callback($instance);
138
            });
139
140
            return $instance->setBindings($bindings)->render($presenter, $specialSidebar);
141
        }
142
143
        return null;
144
    }
145
146
    /**
147
     * Get all menus.
148
     *
149
     * @return array
150
     */
151
    public function all(): array
152
    {
153
        return $this->menus->toArray();
154
    }
155
156
    /**
157
     * Get count from all menus.
158
     *
159
     * @return int
160
     */
161
    public function count(): int
162
    {
163
        return $this->menus->count();
164
    }
165
166
    /**
167
     * Empty the current menus.
168
     */
169
    public function destroy()
170
    {
171
        $this->menus = collect();
172
173
        return $this;
174
    }
175
}
176