Completed
Push — master ( 975a91...8b0e7e )
by Freek
02:39 queued 45s
created

FeedServiceProvider::bindFeedsLinks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Spatie\Feed;
4
5
use Illuminate\Support\ServiceProvider;
6
use Illuminate\Events\Dispatcher;
7
use Illuminate\View\View;
8
9
class FeedServiceProvider extends ServiceProvider
10
{
11
    public function boot()
12
    {
13
        $this->publishes([
14
            __DIR__ . '/../config/laravel-feed.php' => config_path('laravel-feed.php'),
15
        ], 'config');
16
17
        $this->loadViewsFrom(__DIR__ . '/../resources/views', 'laravel-feed');
18
19
        $this->registerFeeds();
20
21
        $this->bindFeedLinks();
22
    }
23
24
    public function register()
25
    {
26
        $this->mergeConfigFrom(__DIR__ . '/../config/laravel-feed.php', 'laravel-feed');
27
28
        $this->app->singleton(Feed::class);
29
    }
30
31
    protected function registerFeeds()
32
    {
33
        collect(config('laravel-feed.feeds'))->each(function ($feedConfiguration) {
34
            if (!$feedConfiguration['url']) {
35
                return;
36
            }
37
            $this->registerRoute($feedConfiguration);
38
        });
39
    }
40
41
    protected function registerRoute(array $feedConfiguration)
42
    {
43
        $this->app['router']->get($feedConfiguration['url'], function () use ($feedConfiguration) {
44
45
            $feed = new Feed($feedConfiguration);
46
47
            return $feed->getFeedResponse();
48
        });
49
    }
50
51
52
    public function bindFeedLinks()
53
    {
54
        $this->app->make(Dispatcher::class)->listen('composing: laravel-feed::feed-links', function (View $view) {
55
            $view->with(['feeds' => config('laravel-feed.feeds')]);
56
        });
57
    }
58
}
59