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

CacheData   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 8
c 1
b 0
f 0
dl 0
loc 25
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A make() 0 5 2
A fromCache() 0 10 2
1
<?php
2
3
namespace Mostafaznv\LaraCache\DTOs;
4
5
use Carbon\Carbon;
6
use Illuminate\Support\Facades\Cache;
7
use Mostafaznv\LaraCache\CacheEntity;
8
9
class CacheData
10
{
11
    public function __construct(
12
        public CacheStatus $status,
13
        public ?int        $expiration,
14
        public mixed       $value
15
    ) {}
16
17
    public static function make(CacheStatus $status, int $ttl, mixed $value): self
18
    {
19
        $expiration = $ttl ? Carbon::now()->addSeconds($ttl)->unix() : null;
20
21
        return new static($status, $expiration, $value);
22
    }
23
24
    public static function fromCache(CacheEntity $entity, string $prefix): self
25
    {
26
        $name = $prefix . '.' . $entity->name;
27
        $value = Cache::store($entity->driver)->get($name, $entity->default);
28
29
        if ($value === $entity->default) {
30
            return self::make(CacheStatus::NOT_CREATED(), 0, $entity->default);
31
        }
32
33
        return $value;
34
    }
35
}
36