MediaCacheService   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
wmc 5
eloc 13
c 0
b 0
f 0
dl 0
loc 48
ccs 13
cts 14
cp 0.9286
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A clear() 0 3 1
A __construct() 0 3 1
A get() 0 8 2
A query() 0 6 1
1
<?php
2
3
namespace App\Services;
4
5
use App\Models\Album;
6
use App\Models\Artist;
7
use App\Models\Song;
8
use Illuminate\Cache\Repository as Cache;
9
10
class MediaCacheService
11
{
12
    private const CACHE_KEY = 'media_cache';
13
14
    private $cache;
15
16 132
    public function __construct(Cache $cache)
17
    {
18 132
        $this->cache = $cache;
19 132
    }
20
21
    /**
22
     * Get media data.
23
     * If caching is enabled, the data will be retrieved from the cache.
24
     *
25
     * @return mixed[]
26
     */
27 3
    public function get(): array
28
    {
29 3
        if (!config('koel.cache_media')) {
30 1
            return $this->query();
31
        }
32
33
        return $this->cache->rememberForever(self::CACHE_KEY, function (): array {
34
            return $this->query();
35 2
        });
36
    }
37
38
    /**
39
     * Query fresh data from the database.
40
     *
41
     * @return mixed[]
42
     */
43 1
    private function query(): array
44
    {
45
        return [
46 1
            'albums' => Album::orderBy('name')->get(),
47 1
            'artists' => Artist::orderBy('name')->get(),
48 1
            'songs' => Song::all(),
49
        ];
50
    }
51
52
    /**
53
     * Clear the media cache.
54
     */
55 7
    public function clear(): void
56
    {
57 7
        $this->cache->forget(self::CACHE_KEY);
58 7
    }
59
}
60