Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Push — create-setup-cache-class ( d50094 )
by Pedro
15:35
created

SetupCache::store()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 12
rs 10
1
<?php
2
3
namespace Backpack\CRUD\app\Library\Support;
4
5
use Illuminate\Support\Facades\Cache;
6
7
abstract class SetupCache
8
{
9
    protected string $cachePrefix = 'setup_cache_';
10
    protected int $cacheDuration = 60; // minutes
11
    
12
    /**
13
     * Generate a cache key for the given identifier.
14
     */
15
    protected function generateCacheKey($identifier): string
16
    {
17
        return $this->cachePrefix . $identifier;
18
    }
19
    
20
    /**
21
     * Store data in the cache.
22
     */
23
    public function store($identifier, ...$args)
24
    {
25
        $cacheKey = $this->generateCacheKey($identifier);
26
        $data = $this->prepareDataForStorage(...$args);
27
        
28
        if ($data !== false && $data !== null) {
29
            Cache::forget($cacheKey);
30
            Cache::put($cacheKey, $data, now()->addMinutes($this->cacheDuration));
31
            return true;
32
        }
33
        
34
        return false;
35
    }
36
    
37
    /**
38
     * Apply cached data.
39
     */
40
    public function apply($identifier, ...$args)
41
    {
42
        $cacheKey = $this->generateCacheKey($identifier);
43
        $cachedData = Cache::get($cacheKey);
44
        
45
        if (!$cachedData) {
46
            return false;
47
        }
48
        
49
        return $this->applyFromCache($cachedData, ...$args);
50
    }
51
    
52
    /**
53
     * Get cached data without applying it.
54
     */
55
    public function get($identifier)
56
    {
57
        $cacheKey = $this->generateCacheKey($identifier);
58
        return Cache::get($cacheKey);
59
    }
60
    
61
    /**
62
     * Check if cache exists for the given identifier.
63
     */
64
    public function has($identifier): bool
65
    {
66
        $cacheKey = $this->generateCacheKey($identifier);
67
        return Cache::has($cacheKey);
68
    }
69
    
70
    /**
71
     * Remove cached data.
72
     */
73
    public function forget($identifier): bool
74
    {
75
        $cacheKey = $this->generateCacheKey($identifier);
76
        return Cache::forget($cacheKey);
77
    }
78
    
79
    /**
80
     * Set the cache prefix.
81
     */
82
    public function setCachePrefix(string $prefix): self
83
    {
84
        $this->cachePrefix = $prefix;
85
        return $this;
86
    }
87
    
88
    /**
89
     * Set the cache duration in minutes.
90
     */
91
    public function setCacheDuration(int $minutes): self
92
    {
93
        $this->cacheDuration = $minutes;
94
        return $this;
95
    }
96
    
97
    /**
98
     * Prepare data for storage in the cache.
99
     * This method should be implemented by child classes.
100
     */
101
    abstract protected function prepareDataForStorage(...$args);
102
    
103
    /**
104
     * Apply data from the cache.
105
     * This method should be implemented by child classes.
106
     */
107
    abstract protected function applyFromCache($cachedData, ...$args);
108
}