Passed
Push — 1.11.x ( 080d0e...e78a37 )
by Angel Fernando Quiroz
10:16
created

Lti13Cache::cacheNonce()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 10
1
<?php
2
/* For license terms, see /license.txt */
3
4
use Packback\Lti1p3\Interfaces\Cache as Lti1p3Cache;
5
6
class Lti13Cache implements Lti1p3Cache
7
{
8
    public const NONCE_PREFIX = 'nonce_';
9
10
    private $cache;
11
12
    public function getLaunchData($key)
13
    {
14
        $this->loadCache();
15
16
        return $this->cache[$key];
17
    }
18
19
    public function cacheLaunchData($key, $jwtBody): Lti13Cache
20
    {
21
        $this->cache[$key] = $jwtBody;
22
        $this->saveCache();
23
24
        return $this;
25
    }
26
27
    public function cacheNonce($nonce): Lti13Cache
28
    {
29
        $this->cache['nonce'][$nonce] = true;
30
        $this->saveCache();
31
32
        return $this;
33
    }
34
35
    public function checkNonce($nonce): bool
36
    {
37
        $this->loadCache();
38
        if (!isset($this->cache['nonce'][$nonce])) {
39
            return false;
40
        }
41
42
        return true;
43
    }
44
45
    private function loadCache()
46
    {
47
        $cache = file_get_contents(api_get_path(SYS_ARCHIVE_PATH).'lti_cache.txt');
48
        if (empty($cache)) {
49
            file_put_contents(api_get_path(SYS_ARCHIVE_PATH).'lti_cache.txt', '{}');
50
            $this->cache = [];
51
        }
52
        $this->cache = json_decode($cache, true);
53
    }
54
55
    private function saveCache()
56
    {
57
        file_put_contents(api_get_path(SYS_ARCHIVE_PATH).'lti_cache.txt', json_encode($this->cache));
58
    }
59
}
60