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

Lti13Cache   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 21
c 2
b 0
f 0
dl 0
loc 52
rs 10
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getLaunchData() 0 5 1
A checkNonce() 0 8 2
A saveCache() 0 3 1
A cacheNonce() 0 6 1
A cacheLaunchData() 0 6 1
A loadCache() 0 8 2
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