DirectoryResponse   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 22
dl 0
loc 50
ccs 0
cts 18
cp 0
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 6 1
A get() 0 26 5
A __construct() 0 1 1
1
<?php
2
namespace LE_ACME2\Cache;
3
4
use LE_ACME2\Connector;
5
6
use LE_ACME2\Account;
7
use LE_ACME2\SingletonTrait;
8
9
use LE_ACME2\Exception;
10
use LE_ACME2\Request;
11
use LE_ACME2\Response;
12
13
class DirectoryResponse {
14
    
15
    use SingletonTrait;
16
17
    private const _FILE = 'DirectoryResponse';
18
    
19
    private function __construct() {}
20
    
21
    private $_responses = [];
22
    private $_index = 0;
23
24
    /**
25
     * @throws Exception\InvalidResponse
26
     * @throws Exception\RateLimitReached
27
     * @throws Exception\ServiceUnavailable
28
     */
29
    public function get() : Response\GetDirectory {
30
31
        if(array_key_exists($this->_index, $this->_responses)) {
32
            return $this->_responses[$this->_index];
33
        }
34
        $this->_responses[$this->_index] = null;
35
36
        $cacheFile = Account::getCommonKeyDirectoryPath() . self::_FILE;
37
38
        if(file_exists($cacheFile) && filemtime($cacheFile) > strtotime('-2 days')) {
39
40
            $rawResponse = Connector\RawResponse::getFromString(file_get_contents($cacheFile));
41
42
            try {
43
                return $this->_responses[$this->_index] = new Response\GetDirectory($rawResponse);
44
45
            } catch(Exception\AbstractException $e) {
46
                unlink($cacheFile);
47
            }
48
        }
49
50
        $request = new Request\GetDirectory();
51
        $response = $request->getResponse();
52
        $this->set($response);
53
54
        return $response;
55
    }
56
57
    public function set(Response\GetDirectory $response) : void {
58
59
        $cacheFile = Account::getCommonKeyDirectoryPath() . self::_FILE;
60
61
        $this->_responses[$this->_index] = $response;
62
        file_put_contents($cacheFile, $response->getRaw()->toString());
63
    }
64
}