Cache   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 104
rs 10
c 0
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A delete() 0 5 1
A set() 0 17 4
A getCacheKey() 0 13 1
A getCacheDuration() 0 5 1
A get() 0 9 2
1
<?php
2
/**
3
 * @link      https://dukt.net/analytics/
4
 * @copyright Copyright (c) 2022, Dukt
5
 * @license   https://github.com/dukt/analytics/blob/master/LICENSE.md
6
 */
7
8
namespace dukt\analytics\services;
9
10
use Craft;
11
use craft\helpers\DateTimeHelper;
12
use yii\base\Component;
13
use dukt\analytics\Plugin as Analytics;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, dukt\analytics\services\Analytics. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
14
use DateInterval;
15
16
class Cache extends Component
17
{
18
    // Public Methods
19
    // =========================================================================
20
21
    /**
22
     * Get cache
23
     *
24
     * @param $id
25
     *
26
     * @return mixed
27
     */
28
    public function get($id)
29
    {
30
        if (Analytics::$plugin->getSettings()->enableCache == true) {
31
            $cacheKey = $this->getCacheKey($id);
32
33
            return Craft::$app->getCache()->get($cacheKey);
34
        }
35
36
        return null;
37
    }
38
39
    /**
40
     * Set cache
41
     *
42
     * @param      $id
43
     * @param      $value
44
     * @param null $expire
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $expire is correct as it would always require null to be passed?
Loading history...
45
     * @param null $dependency
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $dependency is correct as it would always require null to be passed?
Loading history...
46
     * @param null $enableCache
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $enableCache is correct as it would always require null to be passed?
Loading history...
47
     *
48
     * @return mixed
49
     * @throws \Exception
50
     */
51
    public function set($id, $value, $expire = null, $dependency = null, $enableCache = null)
52
    {
53
        if (null === $enableCache) {
0 ignored issues
show
introduced by
The condition null === $enableCache is always true.
Loading history...
54
            $enableCache = Analytics::$plugin->getSettings()->enableCache;
55
        }
56
57
        if ($enableCache) {
58
            $cacheKey = $this->getCacheKey($id);
59
60
            if (!$expire) {
0 ignored issues
show
introduced by
$expire is of type null, thus it always evaluated to false.
Loading history...
61
                $expire = $this->getCacheDuration();
62
            }
63
64
            return Craft::$app->getCache()->set($cacheKey, $value, $expire, $dependency);
65
        }
66
67
        return null;
68
    }
69
70
    /**
71
     * Deletes a value with the specified key from cache.
72
     *
73
     * @param string $id The key of the value to be deleted.
74
     *
75
     * @return bool If no error happens during deletion.
76
     */
77
    public function delete($id)
78
    {
79
        $cacheKey = $this->getCacheKey($id);
0 ignored issues
show
Bug introduced by
$id of type string is incompatible with the type array expected by parameter $request of dukt\analytics\services\Cache::getCacheKey(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

79
        $cacheKey = $this->getCacheKey(/** @scrutinizer ignore-type */ $id);
Loading history...
80
81
        return Craft::$app->getCache()->delete($cacheKey);
82
    }
83
84
    // Private Methods
85
    // =========================================================================
86
87
    /**
88
     * Get cache duration (in seconds)
89
     *
90
     * @return int
91
     * @throws \Exception
92
     */
93
    private function getCacheDuration()
94
    {
95
        $duration = Analytics::$plugin->getSettings()->cacheDuration;
96
97
        return DateTimeHelper::intervalToSeconds(new DateInterval($duration));
98
    }
99
100
    /**
101
     * Get cache key
102
     *
103
     * @param array $request
104
     *
105
     * @return string
106
     */
107
    private function getCacheKey(array $request)
108
    {
109
        $dataSourceClassName = 'GoogleAnalytics';
110
111
        unset($request['CRAFT_CSRF_TOKEN']);
112
113
        $request[] = $dataSourceClassName;
114
115
        $hash = md5(serialize($request));
116
117
        $cacheKey = 'analytics.'.$hash;
118
119
        return $cacheKey;
120
    }
121
}
122