Completed
Push — master ( afe80a...325e9f )
by Marin
07:33
created

LaravelCacheServiceProvider   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 0
loc 33
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 21 2
A register() 0 5 1
1
<?php
2
3
namespace Anorgan\LaravelCache;
4
5
use AlternativeLaravelCache\Core\AlternativeCacheStore;
6
use AlternativeLaravelCache\Provider\AlternativeCacheStoresServiceProvider;
7
use Illuminate\Cache\CacheManager;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Support\Facades\Event;
10
use Illuminate\Support\ServiceProvider;
11
12
/**
13
 * Class LaravelCacheServiceProvider
14
 * @package Anorgan\LaravelCache
15
 */
16
class LaravelCacheServiceProvider extends ServiceProvider
17
{
18
    /**
19
     * Bootstrap the application events.
20
     */
21
    public function boot()
22
    {
23
        $this->publishes([
24
            __DIR__.'/../config/laravel-cache.php' => config_path('laravel-cache.php'),
25
        ], 'config');
26
27
        $this->app->bind(AlternativeCacheStore::class, function () {
28
            return $this->app->make(CacheManager::class)->store()->getStore();
29
        });
30
31
        Event::listen(['eloquent.saved: *', 'eloquent.deleted: *', 'eloquent.restored: *'], function (Model $model) {
32
            /** @var CacheInvalidator $cacheInvalidator */
33
            $cacheInvalidator = $this->app->make(CacheInvalidator::class);
34
            $cacheInvalidator->invalidateByModel($model);
35
36
            $invalidateRules = config('laravel-cache.invalidate');
37
            if (isset($invalidateRules[get_class($model)])) {
38
                $cacheInvalidator->invalidateByKeys($invalidateRules[get_class($model)]);
39
            }
40
        });
41
    }
42
43
    public function register()
44
    {
45
        $this->app->register(AlternativeCacheStoresServiceProvider::class);
46
        $this->mergeConfigFrom(__DIR__.'/../config/laravel-cache.php', 'laravel-cache');
47
    }
48
}
49