Passed
Push — master ( 8b9923...b95d07 )
by Mostafa
03:05 queued 36s
created

Cache::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
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
10
class Cache
11
{
12
    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, $driver, $status, $default, $refreshAfterUpdate, $refreshAfterRestore, $expiration, $cacheClosure
Loading history...
13
14
    public function get(string $name, bool $withCacheData = false): mixed
15
    {
16
        $cache = $this->retrieve($name);
17
18
        if ($withCacheData) {
19
            return $cache;
20
        }
21
22
        return $cache->value;
23
    }
24
25
    public function update(string $name): CacheData
26
    {
27
        return $this->updateCacheEntity($name);
28
    }
29
30
    public function updateAll(): void
31
    {
32
        foreach ($this->model::cacheEntities() as $entity) {
33
            $this->updateCacheEntity(
34
                name: $entity->name,
35
                entity: $entity
36
            );
37
        }
38
    }
39
40
    public function delete(string $name, bool $forever = false): CacheData
41
    {
42
        return $this->deleteCacheEntity($name, $forever);
43
    }
44
45
    public function deleteAll(bool $forever = false): void
46
    {
47
        foreach ($this->model::cacheEntities() as $entity) {
48
            $this->deleteCacheEntity($entity->name, $forever, $entity);
49
        }
50
    }
51
52
    public function refresh(CacheEvent $event): void
53
    {
54
        if ($this->model::$isEnabled) {
55
            foreach ($this->model::cacheEntities() as $entity) {
56
                if ($entity->isQueueable) {
57
                    RefreshCache::dispatch($this->model, $entity->name, $event);
58
                }
59
                else {
60
                    $this->updateCacheEntity($entity->name, $event, $entity);
61
                }
62
            }
63
        }
64
    }
65
66
    public function disable(): void
67
    {
68
        $this->model::$isEnabled = false;
69
    }
70
71
    public function enable(): void
72
    {
73
        $this->model::$isEnabled = true;
74
    }
75
}
76