AccountResponse   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
eloc 34
dl 0
loc 73
ccs 0
cts 29
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B get() 0 38 6
A set() 0 19 3
1
<?php
2
namespace LE_ACME2\Cache;
3
4
use LE_ACME2\Account;
5
use LE_ACME2\Connector;
6
use LE_ACME2\Request;
7
use LE_ACME2\Response;
8
use LE_ACME2\Exception;
9
use LE_ACME2\Utilities;
10
use LE_ACME2\SingletonTrait;
11
12
class AccountResponse extends AbstractKeyValuableCache {
13
14
    use SingletonTrait;
15
16
    private const _FILE = 'CacheResponse';
17
    private const _DEPRECATED_FILE = 'DirectoryNewAccountResponse';
18
19
    private $_responses = [];
20
21
    /**
22
     * @throws Exception\InvalidResponse
23
     * @throws Exception\RateLimitReached
24
     * @throws Exception\ServiceUnavailable
25
     */
26
    public function get(Account $account): Response\Account\AbstractAccount {
27
28
        $accountIdentifier = $this->_getObjectIdentifier($account);
29
30
        if(array_key_exists($accountIdentifier, $this->_responses)) {
31
            return $this->_responses[ $accountIdentifier ];
32
        }
33
        $this->_responses[ $accountIdentifier ] = null;
34
35
        $cacheFile = $account->getKeyDirectoryPath() . self::_FILE;
36
        $deprecatedCacheFile = $account->getKeyDirectoryPath() . self::_DEPRECATED_FILE;
37
38
        if(file_exists($deprecatedCacheFile) && !file_exists($cacheFile)) {
39
            rename($deprecatedCacheFile, $cacheFile);
40
        }
41
42
        if(file_exists($cacheFile) && filemtime($cacheFile) > strtotime('-7 days')) {
43
44
            $rawResponse = Connector\RawResponse::getFromString(file_get_contents($cacheFile));
45
46
            $response = new Response\Account\Create($rawResponse);
47
48
            $this->_responses[ $accountIdentifier ] = $response;
49
50
            Utilities\Logger::getInstance()->add(
0 ignored issues
show
Bug introduced by
It seems like add() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

50
            Utilities\Logger::getInstance()->/** @scrutinizer ignore-call */ add(
Loading history...
51
                Utilities\Logger::LEVEL_DEBUG,
52
                static::class . '::' . __FUNCTION__ . ' response from cache'
53
            );
54
55
            return $response;
56
        }
57
58
        $request = new Request\Account\Get($account);
59
        $response = $request->getResponse();
60
61
        $this->set($account, $response);
62
63
        return $response;
64
    }
65
66
    public function set(Account $account, Response\Account\AbstractAccount $response = null) : void {
67
68
        $accountIdentifier = $this->_getObjectIdentifier($account);
69
70
        $filePath = $account->getKeyDirectoryPath() . self::_FILE;
71
72
        if($response === null) {
73
74
            unset($this->_responses[$accountIdentifier]);
75
76
            if(file_exists($filePath)) {
77
                unlink($filePath);
78
            }
79
80
            return;
81
        }
82
83
        $this->_responses[$accountIdentifier] = $response;
84
        file_put_contents($filePath, $response->getRaw()->toString());
85
    }
86
}