Completed
Push — master ( 7a1e6c...8794c3 )
by Arjay
11:28
created

RouteServiceProvider::mapCategoryRoutes()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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