Issues (7)

src/Routings/Builder.php (3 issues)

1
<?php
2
3
namespace Joesama\Pintu\Routings;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Str;
7
use Illuminate\Support\Collection;
8
use Illuminate\Routing\Router;
9
use Joesama\Pintu\Routings\Concerns\Grammar;
10
11
class Builder
12
{
13
    use Grammar;
14
15
    /**
16
     * Array of component.
17
     *
18
     * @var Illuminate\Routing\Router
0 ignored issues
show
The type Joesama\Pintu\Routings\Illuminate\Routing\Router was not found. Did you mean Illuminate\Routing\Router? If so, make sure to prefix the type with \.
Loading history...
19
     */
20
    private $router;
21
22
    /**
23
     * Array of methods.
24
     *
25
     * @var array
26
     */
27
    private $componentMethods = ['GET', 'POST', 'PUT'];
28
29
    /**
30
     * Construct routing builder.
31
     *
32
     * @param Router $router
33
     */
34 12
    public function __construct(Router $router)
35
    {
36 12
        $this->router = $router;
0 ignored issues
show
Documentation Bug introduced by
It seems like $router of type Illuminate\Routing\Router is incompatible with the declared type Joesama\Pintu\Routings\Illuminate\Routing\Router of property $router.

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...
37 12
    }
38
39
    /**
40
     * Component routing registration.
41
     *
42
     * @param Collection $component
43
     *
44
     * @return void
45
     */
46 3
    public function componentRouting(Collection $components, string $namespace)
47
    {
48
        $options = [
49 3
            'namespace' => $namespace,
50
            'middleware' => ['web']
51
        ];
52
53
        $components->each(function ($component, $controller) use ($options) {
54
            collect($component)->each(function ($attributes, $function) use ($controller, $options) {
55 1
                $options = Arr::set($options, 'prefix', $controller);
56
57 1
                if (Arr::get($attributes, 'auth', false)) {
58 1
                    $middleware = Arr::get($options, 'middleware');
59
60 1
                    $options = Arr::set($options, 'middleware', array_merge(Arr::wrap($middleware), ['auth']));
61
                }
62
63 1
                $only = Arr::get($attributes, 'only', collect($this->componentMethods)->toArray());
64
65
                $componentMethods = collect($this->componentMethods)->filter(function ($item) use ($only) {
66 1
                    return \in_array($item, \array_flip(\array_change_key_case(\array_flip($only), \CASE_UPPER)));
67 1
                });
68
69
                $this->router->group($options, function (Router $router) use ($componentMethods, $controller, $function, $attributes) {
70
                    $componentMethods->each(function ($type) use ($router, $controller, $function, $attributes) {
71 1
                        $router->addRoute(
72 1
                            Str::upper($type),
73 1
                            $this->pathConvention($type, $function, $attributes),
74 1
                            $this->classConvention($type, $controller, $function)
75 1
                        )->name(
76 1
                            $this->namedConvention($type, $controller, $function, $attributes)
77
                        );
78 1
                    });
79 1
                });
80 1
            });
81 3
        });
82 3
    }
83
84
    /**
85
     * Landing routing registration.
86
     *
87
     * @param Collection $component
88
     *
89
     * @return void
90
     */
91 1
    public function landingRouting(string $namespace)
92
    {
93
        $options = [
94 1
            'namespace' => $namespace,
95
            'middleware' => ['web', 'guest']
96
        ];
97
98 1
        $type = 'GET';
99 1
        $controller = 'landing';
100 1
        $function = 'default';
101 1
        $attributes = [];
102
103
        $this->router->group($options, function (Router $router) use ($type, $controller, $function, $attributes) {
0 ignored issues
show
The import $attributes is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
104 1
            $router->addRoute(
105 1
                Str::upper($type),
106 1
                '/',
107 1
                $this->classConvention($type, $controller, $function)
108 1
            )->name(
109 1
                $controller
110
            );
111 1
        });
112 1
    }
113
114
    /**
115
     * API routing registration.
116
     *
117
     * @param Collection $api
118
     *
119
     * @return void
120
     */
121 3
    public function apiRouting(Collection $apis, string $namespace)
122
    {
123
        $options = [
124 3
            'namespace' => $namespace,
125 3
            'prefix' => 'api',
126
            'middleware' => ['auth:api']
127
        ];
128
129
        $this->router->group($options, function (Router $router) use ($apis) {
130
            $apis->each(function ($routes, $method) use ($router) {
131 3
                if (!empty($routes)) {
132 1
                    list($path, $controller, $named) = Arr::first($routes);
133
134 1
                    $router->addRoute(
135 1
                        Str::upper($method),
136 1
                        $path,
137 1
                        Str::ucfirst($controller)
138 1
                    )->name(
139 1
                        'api.' . $named
140
                    );
141
                }
142 3
            });
143 3
        });
144 3
    }
145
}
146