MenuManager::instance()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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 Illuminate\Contracts\View\Factory as ViewFactory;
13
14
class MenuManager implements Countable
15
{
16
    /**
17
     * The menus collection.
18
     *
19
     * @var \Illuminate\Support\Collection
20
     */
21
    protected $menus;
22
23
    /**
24
     * The view factory.
25
     *
26
     * @var \Illuminate\Contracts\View\Factory
27
     */
28
    protected $viewFactory;
29
30
    /**
31
     * The router instance.
32
     *
33
     * @var \Illuminate\Routing\Router
34
     */
35
    protected $router;
36
37
    /**
38
     * The menu factory.
39
     *
40
     * @var \Rinvex\Menus\Models\MenuGenerator
41
     */
42
    protected $generator;
43
44
    /**
45
     * The menu callbacks.
46
     *
47
     * @var array
48
     */
49
    protected $callbacks = [];
50
51
    /**
52
     * The constructor.
53
     *
54
     * @param \Illuminate\Contracts\View\Factory $viewFactory
55
     * @param \Illuminate\Routing\Router         $router
56
     */
57
    public function __construct(ViewFactory $viewFactory, Router $router)
58
    {
59
        $this->viewFactory = $viewFactory;
60
        $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...
61
        $this->menus = collect();
62
        $this->router = $router;
63
    }
64
65
    /**
66
     * Check if the menu exists.
67
     *
68
     * @param string $name
69
     *
70
     * @return bool
71
     */
72
    public function has($name): bool
73
    {
74
        return $this->menus->has($name);
75
    }
76
77
    /**
78
     * Get instance of the given menu if exists.
79
     *
80
     * @param string $name
81
     *
82
     * @return \Rinvex\Menus\Models\MenuGenerator|null
83
     */
84
    public function instance($name): ?MenuGenerator
85
    {
86
        return $this->menus->get($name);
87
    }
88
89
    /**
90
     * Register a menu.
91
     *
92
     * @param string   $name
93
     * @param \Closure $callback
94
     *
95
     * @return void
96
     */
97
    public function register($name, Closure $callback): void
98
    {
99
        if ($this->has($name)) {
100
            $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...
101
        } else {
102
            $builder = new MenuGenerator();
103
104
            $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...
105
106
            $this->menus->put($name, $builder);
107
108
            $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...
109
        }
110
    }
111
112
    /**
113
     * Render the menu tag by given name.
114
     *
115
     * @param string $name
116
     * @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...
117
     * @param array  $bindings
118
     * @param bool   $specialSidebar
119
     *
120
     * @return string|null
121
     */
122
    public function render(string $name, string $presenter = null, array $bindings = [], bool $specialSidebar = false): ?string
123
    {
124
        if ($this->has($name)) {
125
            $instance = $this->instance($name);
126
127
            $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...
128
                $reflectionParams = collect((new ReflectionFunction($callback))->getParameters());
129
                $reflectionParams->shift();
130
131
                collect($reflectionParams)->each(function ($param) use (&$params) {
132
                    $name = $param->getType()->getName();
133
                    $params[] = Route::current()->hasParameter($param->getName()) ? Route::current()->parameter($param->getName())
134
                        : ((app()->bound($name) ?: class_exists($name) ?: interface_exists($name)) ? app($name) : 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