ConfigCachingService   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 79
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A refreshCache() 0 7 1
A getConfigJson() 0 8 3
A isDisabled() 0 4 1
A createConfigJson() 0 11 2
1
<?php
2
namespace JsLocalization\Caching;
3
4
use Config;
5
use Event;
6
7
/**
8
 * Class ConfigCachingService
9
 * @package JsLocalization\Caching
10
 */
11
class ConfigCachingService extends AbstractCachingService
12
{
13
14
    /**
15
     * The key used to cache the JSON encoded messages.
16
     *
17
     * @var string
18
     */
19
    const CACHE_KEY = 'js-localization-config-json';
20
21
    /**
22
     * The key used to cache the timestamp of the last
23
     * refreshCache() call.
24
     *
25
     * @var string
26
     */
27
    const CACHE_TIMESTAMP_KEY = 'js-localization-config-last-modified';
28
29
30 8
    public function __construct()
31
    {
32 8
        parent::__construct(self::CACHE_KEY, self::CACHE_TIMESTAMP_KEY);
33 8
    }
34
35
    /**
36
     * Refreshes the cache item containing the JSON encoded
37
     * config object.
38
     * Fires the 'JsLocalization.registerConfig' event.
39
     *
40
     * @return void
41
     */
42 5
    public function refreshCache()
43
    {
44 5
        Event::fire('JsLocalization.registerConfig');
45
        
46 5
        $configJson = $this->createConfigJson();
47 5
        $this->refreshCacheUsing($configJson);
48 5
    }
49
50
    /**
51
     * Returns the config (already JSON encoded).
52
     * Refreshes the cache if necessary.
53
     *
54
     * @param bool $noCache (optional) Defines if cache should be ignored.
55
     * @return mixed|string string   The JSON-encoded config exports.
56
     */
57 5
    public function getConfigJson($noCache = false)
58
    {
59 5
        if ($noCache || $this->isDisabled()) {
60 2
            return $this->createConfigJson();
61
        } else {
62 3
            return $this->getData();
63
        }
64
    }
65
66
    /**
67
     * @return bool Is config caching disabled? `true` means that this class does not cache, but create the data on the fly.
68
     */
69 6
    public function isDisabled()
70
    {
71 6
        return Config::get('js-localization.disable_config_cache', false);
72
    }
73
74
    /**
75
     * @return string
76
     */
77 7
    protected function createConfigJson()
78
    {
79 7
        $propertyNames = Config::get('js-localization.config', []);
80 7
        $configArray = [];
81
        
82 7
        foreach ($propertyNames as $propertyName) {
83 6
            $configArray[$propertyName] = Config::get($propertyName);
84 7
        }
85
        
86 7
        return json_encode((object)$configArray);
87
    }
88
    
89
}