Completed
Push — master ( 2f4806...4e3d73 )
by Renato
05:45
created

CleanCacheRepository   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
dl 0
loc 56
ccs 16
cts 17
cp 0.9412
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 19 4
1
<?php
2
namespace NwLaravel\Repositories\Listeners;
3
4
use \Exception;
5
use Illuminate\Support\Facades\Log;
6
use Prettus\Repository\Events\RepositoryEventBase;
7
8
/**
9
 * Class CleanCacheRepository
10
 */
11
class CleanCacheRepository
12
{
13
14
    /**
15
     * @var \Illuminate\Contracts\Cache\Repository
16
     */
17
    protected $cache = null;
18
19
    /**
20
     * @var \Prettus\Repository\Contracts\RepositoryInterface;
21
     */
22
    protected $repository = null;
23
24
    /**
25
     * @var \Illuminate\Database\Eloquent\Model
26
     */
27
    protected $model = null;
28
29
    /**
30
     * @var string
31
     */
32
    protected $action = null;
33
34
    /**
35
     * Construct
36
     */
37 1
    public function __construct()
38
    {
39 1
        $this->cache = app(config('repository.cache.repository', 'cache'));
40 1
    }
41
42
    /**
43
     * Handle
44
     *
45
     * @param RepositoryEventBase $event
46
     */
47 1
    public function handle(RepositoryEventBase $event)
48
    {
49
        try {
50 1
            $cleanEnabled = config("repository.cache.clean.enabled", true);
51
52 1
            if ($cleanEnabled) {
53 1
                $this->repository = $event->getRepository();
54 1
                $this->model = $event->getModel();
55 1
                $this->action = $event->getAction();
56
57 1
                if (config("repository.cache.clean.on.{$this->action}", true)) {
58 1
                    $tag = get_class($this->repository);
59 1
                    $this->cache->tags($tag)->flush();
60 1
                }
61 1
            }
62 1
        } catch (Exception $e) {
63
            Log::error($e->getMessage());
64
        }
65 1
    }
66
}
67