Completed
Push — master ( 2fd3c7...7cf196 )
by Nassif
34:41 queued 29:24
created

LocaleAwareResultCache::getCacheKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
nc 1
cc 1
eloc 2
nop 0
crap 1
1
<?php
2
3
namespace Tequilarapido\ResultCache;
4
5
abstract class LocaleAwareResultCache extends ResultCache
6
{
7
    /**
8
     * Locale to be used.
9
     * If not set, application locale will be used.
10
     *
11
     * @var string
12
     */
13
    protected $locale;
14
15
    /**
16
     * Return an array of supported application locales.
17
     *
18
     * @return array
19
     */
20
    abstract public function supportedLocales();
21
22
    /**
23
     * Sets locale.
24
     *
25
     * @param $locale
26
     *
27
     * @return LocaleAwareResultCache
28
     */
29 1
    public function setLocale($locale)
30
    {
31 1
        $this->locale = $locale;
32
33 1
        return $this;
34
    }
35
36
    /**
37
     * Return concrete used cache key.
38
     *
39
     * @return string
40
     */
41 5
    protected function getCacheKey()
42
    {
43 5
        return $this->localizedKey($this->locale);
44
    }
45
46
    /**
47
     * Return cache key suffixed with locale.
48
     *
49
     * @param null|string $locale
50
     *
51
     * @return string
52
     */
53 6
    protected function localizedKey($locale = null)
54
    {
55 6
        return $this->key().'::'.($locale ?: app()->getLocale());
56
    }
57
58
    /**
59
     * Removes cache for all locales.
60
     */
61 1
    public function forget()
62
    {
63 1
        foreach ($this->supportedLocales() as $locale) {
64 1
            $this->getCache()->forget($this->localizedKey($locale));
65 1
        }
66 1
    }
67
}
68