CacheTools   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 57
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A cacheKey() 0 5 1
A setModuleCount() 0 6 1
A existsCount() 0 6 1
A getModuleCount() 0 7 2
1
<?php
2
/**
3
 * BEdita, API-first content management framework
4
 * Copyright 2021 ChannelWeb Srl, Chialab Srl
5
 *
6
 * This file is part of BEdita: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as published
8
 * by the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
12
 */
13
14
namespace App\Utility;
15
16
use BEdita\WebTools\ApiClientProvider;
17
use Cake\Cache\Cache;
18
use Cake\Utility\Hash;
19
20
/**
21
 * Add cache related utility methods
22
 */
23
class CacheTools
24
{
25
    /**
26
     * Create multi project cache key.
27
     *
28
     * @param string $name Cache item name.
29
     * @return string
30
     */
31
    public static function cacheKey(string $name): string
32
    {
33
        $apiSignature = md5(ApiClientProvider::getApiClient()->getApiBaseUrl());
34
35
        return sprintf('%s_%s', $name, $apiSignature);
36
    }
37
38
    /**
39
     * Check if module count exists in cache.
40
     *
41
     * @param string $moduleName Module name.
42
     * @return bool
43
     */
44
    public static function existsCount(string $moduleName): bool
45
    {
46
        $name = sprintf('statistics_%s_count', $moduleName);
47
        $cacheKey = self::cacheKey($name);
48
49
        return Cache::read($cacheKey) !== null;
50
    }
51
52
    /**
53
     * Get module count from cache.
54
     *
55
     * @param string $moduleName Module name.
56
     * @return string
57
     */
58
    public static function getModuleCount(string $moduleName): string
59
    {
60
        $name = sprintf('statistics_%s_count', $moduleName);
61
        $cacheKey = self::cacheKey($name);
62
        $count = Cache::read($cacheKey);
63
64
        return is_int($count) ? (int)$count : '-';
65
    }
66
67
    /**
68
     * Set module count in cache.
69
     *
70
     * @param array $response API response.
71
     * @param string $moduleName Module name.
72
     * @return void
73
     */
74
    public static function setModuleCount(array $response, string $moduleName): void
75
    {
76
        $count = Hash::get($response, 'meta.pagination.count', 0);
77
        $name = sprintf('statistics_%s_count', $moduleName);
78
        $cacheKey = self::cacheKey($name);
79
        Cache::write($cacheKey, $count);
80
    }
81
}
82