Cache   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 15
eloc 36
c 5
b 0
f 0
dl 0
loc 85
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A refresh() 0 26 5
A get() 0 9 2
A disable() 0 3 1
A update() 0 5 1
A delete() 0 3 1
A enable() 0 3 1
A deleteAll() 0 4 2
A updateAll() 0 8 2
1
<?php
2
3
namespace Mostafaznv\LaraCache;
4
5
use Mostafaznv\LaraCache\DTOs\CacheData;
6
use Mostafaznv\LaraCache\DTOs\CacheEvent;
7
use Mostafaznv\LaraCache\Jobs\RefreshCache;
8
use Mostafaznv\LaraCache\Traits\InteractsWithCache;
9
use Mostafaznv\LaraCache\Utils\RefreshDebouncer;
10
11
class Cache
12
{
13
    use InteractsWithCache;
0 ignored issues
show
introduced by
The trait Mostafaznv\LaraCache\Traits\InteractsWithCache requires some properties which are not provided by Mostafaznv\LaraCache\Cache: $refreshAfterDelete, $name, $refreshAfterCreate, $queueConnection, $isQueueable, $driver, $default, $refreshAfterUpdate, $queueName, $refreshAfterRestore, $ttl, $expiration, $cacheClosure
Loading history...
14
15
    public function get(string $name, bool $withCacheData = false): mixed
16
    {
17
        $cache = $this->retrieve($name);
18
19
        if ($withCacheData) {
20
            return $cache;
21
        }
22
23
        return $cache->value;
24
    }
25
26
    public function update(string $name): CacheData
27
    {
28
        $this->updateLaraCacheModelsList();
29
30
        return $this->updateCacheEntity($name);
31
    }
32
33
    public function updateAll(): void
34
    {
35
        $this->updateLaraCacheModelsList();
36
37
        foreach ($this->model::cacheEntities() as $entity) {
38
            $this->updateCacheEntity(
39
                name: $entity->name,
40
                entity: $entity
41
            );
42
        }
43
    }
44
45
    public function delete(string $name, bool $forever = false): CacheData
46
    {
47
        return $this->deleteCacheEntity($name, $forever);
48
    }
49
50
    public function deleteAll(bool $forever = false): void
51
    {
52
        foreach ($this->model::cacheEntities() as $entity) {
53
            $this->deleteCacheEntity($entity->name, $forever, $entity);
54
        }
55
    }
56
57
    public function refresh(CacheEvent $event): void
58
    {
59
        if ($this->model::$isEnabled) {
60
            $this->updateLaraCacheModelsList();
61
62
            foreach ($this->model::cacheEntities() as $entity) {
63
                if ($entity->debounce) {
64
                    $this->initCache($entity, $entity->getTtl());
65
66
                    RefreshDebouncer::dispatch(
67
                        model: $this->model,
68
                        name: $entity->name,
69
                        queueConnection: $entity->queueConnection,
70
                        queueName: $entity->queueName,
71
                        wait: $entity->debounceWaitTime
72
                    );
73
                }
74
                else if ($entity->isQueueable) {
75
                    $this->initCache($entity, $entity->getTtl());
76
77
                    RefreshCache::dispatch($this->model, $entity->name, $event)
78
                        ->onConnection($entity->queueConnection)
79
                        ->onQueue($entity->queueName);
80
                }
81
                else {
82
                    $this->updateCacheEntity($entity->name, $event, $entity);
83
                }
84
            }
85
        }
86
    }
87
88
    public function disable(): void
89
    {
90
        $this->model::$isEnabled = false;
91
    }
92
93
    public function enable(): void
94
    {
95
        $this->model::$isEnabled = true;
96
    }
97
}
98