MediaCacheService::clear()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 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