BlogServiceProvider::addFeed()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
nc 1
nop 0
dl 0
loc 6
c 1
b 0
f 0
cc 1
rs 10
1
<?php
2
3
namespace Wingsline\Blog;
4
5
use Illuminate\Support\Facades\Route;
6
use Illuminate\Support\ServiceProvider;
7
use Spatie\Flash\Flash;
8
use Spatie\Menu\Laravel\Facades\Menu;
9
use Spatie\Menu\Laravel\View;
10
use Spatie\ResponseCache\Middlewares\CacheResponse;
11
use Spatie\Tags\Tag;
12
use Wingsline\Blog\Console\InstallCommand;
13
use Wingsline\Blog\Console\PublishCommand;
14
use Wingsline\Blog\Console\ThemePublishCommand;
15
use Wingsline\Blog\Http\Middleware\Authenticate;
16
use Wingsline\Blog\Http\Middleware\NoHttpCache;
17
use Wingsline\Blog\Posts\Post;
18
use Wingsline\Blog\Posts\PostObserver;
19
20
class BlogServiceProvider extends ServiceProvider
21
{
22
    public function addFeed()
23
    {
24
        $this->app['config']
25
            ->set(
26
                'feed.feeds.blog',
27
                $this->app['config']->get('blog.feed')
28
            );
29
    }
30
31
    /**
32
     * Perform post-registration booting of services.
33
     */
34
    public function boot()
35
    {
36
        // prepend the views from the theme
37
        $this->app['view']->getFinder()->prependLocation(base_path('theme/views'));
38
39
        $this->loadViewsFrom(__DIR__.'/../resources/views', 'blog');
40
        $this->loadMigrationsFrom(__DIR__.'/../database/migrations');
41
        $this->loadRoutesFrom(__DIR__.'/../routes/web.php');
42
43
        Flash::levels([
44
            'success' => '',
45
            'error' => 'bg-red-500',
46
        ]);
47
48
        Menu::macro('blogAdminNavHeader', function () {
49
            return Menu::build(
50
                config('blog.navHeader'),
51
                function (\Spatie\Menu\Laravel\Menu $menu, $view) {
52
                    $menu->add(View::create($view)->addParentClass('ml-5 lg:ml-6'));
53
                })
54
                ->addClass('flex items-end lg:items-center lg:pr-2')
55
                ->addItemClass('p-2')
56
                ->setActiveFromRequest('/');
57
        });
58
59
        Menu::macro('blogAdminNav', function () {
60
            return Menu::build(
61
                config('blog.navAdmin'),
62
                function (\Spatie\Menu\Laravel\Menu $menu, $view) {
63
                    $menu->add(View::create($view)->addParentClass(''));
64
                })
65
                ->addItemClass('p-2')
66
                ->setActiveFromRequest('/');
67
        });
68
69
        // register the route middleware groups
70
        $this->app['router']->middlewareGroup('blog-auth',
71
            [Authenticate::class]);
72
        $this->app['router']->middlewareGroup('blog-nocache',
73
            [NoHttpCache::class]);
74
        $this->app['router']->middlewareGroup('blog-cacheResponse',
75
            [CacheResponse::class]);
76
77
        // router bindings
78
        $this->registerRouteModelBindings();
79
80
        // model event observers
81
        Post::observe(PostObserver::class);
82
83
        // Publishing is only necessary when using the CLI.
84
        if ($this->app->runningInConsole()) {
85
            $this->bootForConsole();
86
        }
87
    }
88
89
    /**
90
     * Register any package services.
91
     */
92
    public function register()
93
    {
94
        $this->mergeConfigFrom(__DIR__.'/../config/blog.php', 'blog');
95
96
        if (file_exists(base_path('theme/config.php'))) {
97
            $this->mergeConfigFrom(base_path('theme/config.php'), 'theme');
98
        }
99
100
        $this->addFeed();
101
102
        $this->commands([
103
            InstallCommand::class,
104
            PublishCommand::class,
105
            ThemePublishCommand::class,
106
        ]);
107
    }
108
109
    public function registerRouteModelBindings()
110
    {
111
        Route::bind('postSlug', function ($slug) {
112
            if (auth()->check()) {
113
                return Post::where('slug', $slug)->first() ?? abort(404);
0 ignored issues
show
Bug introduced by
Are you sure the usage of abort(404) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
114
            }
115
116
            $post = Post::where('slug', $slug)->public()->first() ?? abort(404);
0 ignored issues
show
Bug introduced by
Are you sure the usage of abort(404) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
117
118
            if (! $post->published) {
119
                abort(404);
120
            }
121
122
            return $post;
123
        });
124
125
        Route::bind('tagSlug', function ($slug) {
126
            return Tag::where('slug->en', $slug)->first() ?? abort(404);
0 ignored issues
show
Bug introduced by
Are you sure the usage of abort(404) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
127
        });
128
    }
129
130
    /**
131
     * Console-specific booting.
132
     */
133
    protected function bootForConsole()
134
    {
135
        // Publishing the configuration file.
136
        $this->publishes([
137
            __DIR__.'/../config/blog.php' => config_path('blog.php'),
138
            __DIR__.'/../config/theme.php' => config_path('theme.php'),
139
        ], 'blog.config');
140
141
        // Publishing the views.
142
        $this->publishes([
143
            __DIR__.'/../resources/views' => base_path('resources/views/vendor/wingsline'),
144
        ], 'blog.views');
145
146
        // Publishing the migrations.
147
        $this->publishes([
148
            __DIR__.'/../database/migrations' => database_path('migrations'),
149
        ], 'blog.migrations');
150
151
        // Publishing assets.
152
        $this->publishes([
153
            __DIR__.'/../public' => public_path('vendor/wingsline-blog'),
154
        ], 'blog.assets');
155
156
        // Publishing the translation files.
157
        /*$this->publishes([
158
            __DIR__.'/../resources/lang' => resource_path('lang/vendor/wingsline'),
159
        ], 'blog.views');*/
160
161
        // Registering package commands.
162
        // $this->commands([]);
163
    }
164
}
165