RouteServiceProvider::mapCategoryRoutes()   B
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 37
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 23
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 37
rs 8.439
1
<?php
2
3
namespace Yajra\CMS\Providers;
4
5
use Illuminate\Database\QueryException;
6
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
7
use Illuminate\Routing\Router;
8
use Yajra\Acl\Models\Permission;
9
use Yajra\CMS\Entities\Article;
10
use Yajra\CMS\Entities\Category;
11
use Yajra\CMS\Entities\Widget;
12
use Yajra\CMS\Http\Controllers\ArticleController;
13
use Yajra\CMS\Http\Controllers\AuthController;
14
use Yajra\CMS\Http\Controllers\CategoryController;
15
16
class RouteServiceProvider extends ServiceProvider
17
{
18
    /**
19
     * This namespace is applied to your site controller routes.
20
     * In addition, it is set as the URL generator's root namespace.
21
     *
22
     * @var string
23
     */
24
    protected $namespace = 'App\Http\Controllers';
25
26
    /**
27
     * Define your route model bindings, pattern filters, etc.
28
     *
29
     * @return void
30
     */
31
    public function boot()
32
    {
33
        parent::boot();
34
    }
35
36
    /**
37
     * Define the routes for the application.
38
     *
39
     * @param  \Illuminate\Routing\Router $router
40
     * @return void
41
     */
42
    public function map(Router $router)
43
    {
44
        $router->bind('widget', function ($id) use ($router) {
45
            if ($router->is(admin_prefix() . '*')) {
46
                return Widget::withoutGlobalScope('menu_assignment')->findOrFail($id);
47
            }
48
49
            return Widget::findOrFail($id);
50
        });
51
52
        $this->mapWebRoutes($router);
53
    }
54
55
    /**
56
     * Define the "web" routes for the application.
57
     * These routes all receive session state, CSRF protection, etc.
58
     *
59
     * @param  \Illuminate\Routing\Router $router
60
     * @return void
61
     */
62
    protected function mapWebRoutes(Router $router)
63
    {
64
        $this->mapArticleRoutes($router);
65
        $this->mapCategoryRoutes($router);
66
        $this->mapAdministratorAuthenticationRoutes($router);
67
        $this->mapAdministratorRoutes($router);
68
    }
69
70
    /**
71
     * Map dynamic article routes using the slug.
72
     *
73
     * @param \Illuminate\Routing\Router $router
74
     */
75
    protected function mapArticleRoutes(Router $router)
76
    {
77
        try {
78
            /** @var \Yajra\CMS\Repositories\Article\Repository $repository */
79
            $repository = app('articles');
80
            $repository->getAllPublished()->each(function (Article $article) use ($router) {
81
                $middleware = ['web'];
82
                if ($article->requiresAuthentication()) {
83
                    $middleware[] = 'auth';
84
                }
85
86
                if ($article->permissions->count()) {
87
                    if ($article->authorization === 'can') {
88
                        $article->permissions->each(
89
                            function (Permission $permission) use ($middleware) {
90
                                $middleware[] = 'can:' . $permission->slug;
91
                            }
92
                        );
93
                    } else {
94
                        $abilities    = $article->permissions->pluck('slug')->toArray();
95
                        $middleware[] = 'canAtLeast:' . implode(',', $abilities);
96
                    }
97
                }
98
99
                $router->get($article->present()->slug,
100
                    function () use ($article, $router) {
101
                        return $this->app->call(ArticleController::class . '@show', [
102
                            'article'    => $article,
103
                            'parameters' => $router->current()->parameters(),
104
                        ]);
105
                    })->middleware($middleware)->name($article->getRouteName());
106
107
                /** @var \Yajra\Breadcrumbs\Manager $breadcrumb */
108
                $breadcrumb = app('breadcrumbs');
109
                $breadcrumb->register($article->getRouteName(),
110
                    function (\Yajra\Breadcrumbs\Generator $breadcrumbs) use ($article) {
111
                        if ($article->is_page) {
112
                            $breadcrumbs->parent('home');
113
                        } else {
114
                            $breadcrumbs->parent($article->category->getRouteName());
115
                        }
116
                        $breadcrumbs->push($article->title, route($article->getRouteName()));
117
                    }
118
                );
119
            });
120
        } catch (QueryException $e) {
121
            // \\_(",)_//
122
        }
123
    }
124
125
    /**
126
     * Map dynamic article routes using the slug.
127
     *
128
     * @param \Illuminate\Routing\Router $router
129
     */
130
    protected function mapCategoryRoutes(Router $router)
131
    {
132
        try {
133
            /** @var \Yajra\CMS\Repositories\Category\Repository $repository */
134
            $repository = app('categories');
135
            $repository->getAllPublished()->each(function (Category $category) use ($router) {
136
                $middleware = ['web'];
137
                if ($category->requiresAuthentication()) {
138
                    $middleware[] = 'auth';
139
                }
140
141
                $router->get($category->present()->slug,
142
                    function () use ($category, $router) {
143
                        return $this->app->call(CategoryController::class . '@show', [
144
                            'category'   => $category,
145
                            'parameters' => $router->current()->parameters(),
146
                        ]);
147
                    })->middleware($middleware)->name($category->getRouteName());
148
149
                /** @var \Yajra\Breadcrumbs\Manager $breadcrumb */
150
                $breadcrumb = app('breadcrumbs');
151
                $breadcrumb->register($category->getRouteName(),
152
                    function (\Yajra\Breadcrumbs\Generator $breadcrumbs) use ($category) {
153
                        if ($category->isChild() && $category->depth > 1) {
154
                            $parent = $category->ancestors()->where('depth', '<>', 0)->first();
155
                            $breadcrumbs->parent($parent->getRouteName());
156
                        } else {
157
                            $breadcrumbs->parent('home');
158
                        }
159
                        $breadcrumbs->push($category->title, route($category->getRouteName()));
160
                    }
161
                );
162
            });
163
        } catch (QueryException $e) {
164
            // \\_(",)_//
165
        }
166
    }
167
168
    /**
169
     * Map administrator authentication routes.
170
     *
171
     * @param \Illuminate\Routing\Router $router
172
     */
173
    protected function mapAdministratorAuthenticationRoutes(Router $router)
174
    {
175
        $router->group(['prefix' => admin_prefix()], function () use ($router) {
176
            $router->get('login', [
177
                'middleware' => 'web',
178
                'uses'       => AuthController::class . '@showLoginForm',
179
            ])->name('administrator.login');
180
181
            $router->get('logout', [
182
                'middleware' => 'web',
183
                'uses'       => AuthController::class . '@logout',
184
            ])->name('administrator.logout');
185
186
            $router->post('login', [
187
                'middleware' => 'web',
188
                'uses'       => AuthController::class . '@login',
189
            ])->name('administrator.login');
190
        });
191
    }
192
193
    /**
194
     * Map administrator routes.
195
     *
196
     * @param \Illuminate\Routing\Router $router
197
     */
198
    protected function mapAdministratorRoutes(Router $router)
199
    {
200
        $router->group([
201
            'middleware' => 'administrator',
202
            'prefix'     => admin_prefix(),
203
            'as'         => 'administrator.',
204
        ], function ($router) {
205
            require __DIR__ . '/../Http/routes.php';
206
        });
207
    }
208
}
209