Completed
Push — master ( 194bfe...fac0e0 )
by Abdelrahman
01:34
created

MenuManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
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
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...
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
                    $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...
133
                        : (class_exists($param->getClass()->getName()) ? app($param->getClass()->getName()) : null);
134
                });
135
136
                $params ? $callback($instance, ...$params) : $callback($instance);
137
            });
138
139
            return $instance->setBindings($bindings)->render($presenter, $specialSidebar);
140
        }
141
142
        return null;
143
    }
144
145
    /**
146
     * Get all menus.
147
     *
148
     * @return array
149
     */
150
    public function all(): array
151
    {
152
        return $this->menus->toArray();
153
    }
154
155
    /**
156
     * Get count from all menus.
157
     *
158
     * @return int
159
     */
160
    public function count(): int
161
    {
162
        return $this->menus->count();
163
    }
164
165
    /**
166
     * Empty the current menus.
167
     */
168
    public function destroy()
169
    {
170
        $this->menus = collect();
171
172
        return $this;
173
    }
174
}
175