Issues (29)

src/services/Cache.php (6 issues)

1
<?php
2
/**
3
 * @link      https://dukt.net/facebook/
4
 * @copyright Copyright (c) Dukt
5
 * @license   https://github.com/dukt/facebook/blob/master/LICENSE.md
6
 */
7
8
namespace dukt\facebook\services;
9
10
use Craft;
11
use yii\base\Component;
12
use DateInterval;
13
use dukt\facebook\Plugin as Facebook;
14
15
16
/**
17
 * Class Cache service
18
 *
19
 * @author Dukt <[email protected]>
20
 * @since  2.0
21
 */
22
class Cache extends Component
23
{
24
    // Public Methods
25
    // =========================================================================
26
27
    /**
28
     * Retrieves a value from cache with a specified key.
29
     *
30
     * @param $id
31
     *
32
     * @return mixed
33
     */
34
    public function get($id)
35
    {
36
        if (Facebook::$plugin->getSettings()->enableCache == true) {
37
            $cacheKey = $this->getCacheKey($id);
38
39
            return Craft::$app->cache->get($cacheKey);
40
        }
41
    }
42
43
    /**
44
     * Stores a value identified by a key into cache.
45
     *
46
     * @param      $id
47
     * @param      $value
48
     * @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...
49
     * @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...
50
     * @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...
51
     *
52
     * @return bool
53
     * @throws \Exception
54
     */
55
    public function set($id, $value, $expire = null, $dependency = null, $enableCache = null)
56
    {
57
        if (is_null($enableCache)) {
0 ignored issues
show
The condition is_null($enableCache) is always true.
Loading history...
58
            $enableCache = Facebook::$plugin->getSettings()->enableCache;
59
        }
60
61
        if ($enableCache) {
62
            $cacheKey = $this->getCacheKey($id);
63
64
            if (!$expire) {
0 ignored issues
show
$expire is of type null, thus it always evaluated to false.
Loading history...
65
                $expire = Facebook::$plugin->getSettings()->cacheDuration;
66
                $expire = $this->_formatDuration($expire);
67
            }
68
69
            return Craft::$app->cache->set($cacheKey, $value, $expire, $dependency);
0 ignored issues
show
$expire of type string is incompatible with the type integer expected by parameter $duration of yii\caching\CacheInterface::set(). ( Ignorable by Annotation )

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

69
            return Craft::$app->cache->set($cacheKey, $value, /** @scrutinizer ignore-type */ $expire, $dependency);
Loading history...
70
        }
71
    }
72
73
    // Private Methods
74
    // =========================================================================
75
    /**
76
     * Formats duration.
77
     *
78
     * @param        $cacheDuration
79
     *
80
     * @return string
81
     * @throws \Exception
82
     */
83
    private function _formatDuration($cacheDuration, string $format = '%s')
84
    {
85
        $cacheDuration = new DateInterval($cacheDuration);
86
87
        return $cacheDuration->format($format);
88
    }
89
90
    /**
91
     * Return the cache key.
92
     *
93
     * @param array $request
94
     *
95
     * @return string
96
     */
97
    private function getCacheKey(array $request)
98
    {
99
        $dataSourceClassName = 'Facebook';
100
101
        unset($request['CRAFT_CSRF_TOKEN']);
102
103
        $request[] = $dataSourceClassName;
104
105
        $hash = md5(serialize($request));
106
107
        return 'facebook.'.$hash;
108
    }
109
}
110