RepositoryServiceProvider::boot()   B
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 26
Code Lines 16

Duplication

Lines 10
Ratio 38.46 %

Importance

Changes 0
Metric Value
cc 4
eloc 16
nc 2
nop 0
dl 10
loc 26
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
namespace Yajra\CMS\Providers;
4
5
use Illuminate\Support\ServiceProvider;
6
use Yajra\CMS\Contracts\Cacheable;
7
use Yajra\CMS\Entities\Extension;
8
use Yajra\CMS\Entities\Menu;
9
use Yajra\CMS\Entities\Navigation;
10
use Yajra\CMS\Entities\Widget;
11
use Yajra\CMS\Repositories\Widget\WidgetCacheRepository;
12
use Yajra\CMS\Repositories\Widget\WidgetEloquentRepository;
13
use Yajra\CMS\Repositories\Widget\WidgetRepository;
14
15
class RepositoryServiceProvider extends ServiceProvider
16
{
17
    /**
18
     * List of model that implements caching.
19
     *
20
     * @var array
21
     */
22
    protected $cachedModels = [
23
        Widget::class,
24
        Navigation::class,
25
        Menu::class,
26
    ];
27
28
    /**
29
     * Bootstrap the application services.
30
     *
31
     * @return void
32
     */
33
    public function boot()
34
    {
35
        foreach ($this->cachedModels as $cachedModel) {
36
            $closure = function ($model) {
37
                if ($model instanceof Cacheable) {
38
                    foreach ($model->getCacheKeys() as $key) {
39
                        $this->app['cache.store']->forget($key);
40
                    }
41
                }
42
            };
43
44
            $cachedModel::saved($closure);
45
            $cachedModel::deleted($closure);
46
        }
47
48 View Code Duplication
        Extension::saved(function ($model) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
            $this->app['cache.store']->forget('extension.' . $model->id);
50
            $this->app['cache.store']->forget('extensions.widgets');
51
            $this->app['cache.store']->forget('extensions.all');
52
        });
53 View Code Duplication
        Extension::deleted(function ($model) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
            $this->app['cache.store']->forget('extension.' . $model->id);
55
            $this->app['cache.store']->forget('extensions.widgets');
56
            $this->app['cache.store']->forget('extensions.all');
57
        });
58
    }
59
60
    /**
61
     * Register the application services.
62
     *
63
     * @return void
64
     */
65
    public function register()
66
    {
67
        $this->app->singleton(WidgetRepository::class, function () {
68
            return new WidgetCacheRepository(new WidgetEloquentRepository, $this->app['cache.store']);
69
        });
70
71
        $this->app->singleton('navigation', function () {
72
            return new \Yajra\CMS\Repositories\Navigation\CacheRepository(
73
                new \Yajra\CMS\Repositories\Navigation\EloquentRepository,
74
                $this->app['cache.store']
75
            );
76
        });
77
78
        $this->app->singleton('articles', function () {
79
            return new \Yajra\CMS\Repositories\Article\EloquentRepository;
80
        });
81
82
        $this->app->singleton('categories', function () {
83
            return new \Yajra\CMS\Repositories\Category\EloquentRepository;
84
        });
85
86
        $this->app->singleton('extensions', function () {
87
            return new \Yajra\CMS\Repositories\Extension\CacheRepository(
88
                new \Yajra\CMS\Repositories\Extension\EloquentRepository,
89
                $this->app['cache.store']
90
            );
91
        });
92
        
93
        $this->app->alias('articles', \Yajra\CMS\Repositories\Article\Repository::class);
94
        $this->app->alias('categories', \Yajra\CMS\Repositories\Category\Repository::class);
95
        $this->app->alias('navigation', \Yajra\CMS\Repositories\Navigation\Repository::class);
96
        $this->app->alias('extensions', \Yajra\CMS\Repositories\Extension\Repository::class);
97
    }
98
}
99