1
|
|
|
<?php |
2
|
|
|
namespace LE_ACME2\Cache; |
3
|
|
|
|
4
|
|
|
use LE_ACME2\Connector; |
5
|
|
|
use LE_ACME2\Order; |
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 OrderResponse extends AbstractKeyValuableCache { |
13
|
|
|
|
14
|
|
|
use SingletonTrait; |
15
|
|
|
|
16
|
|
|
private const _FILE = 'CacheResponse'; |
17
|
|
|
private const _DEPRECATED_FILE = 'DirectoryNewOrderResponse'; |
18
|
|
|
|
19
|
|
|
private $_responses = []; |
20
|
|
|
|
21
|
|
|
public function exists(Order $order) : bool { |
22
|
|
|
|
23
|
|
|
$cacheFile = $order->getKeyDirectoryPath() . self::_FILE; |
24
|
|
|
$deprecatedCacheFile = $order->getKeyDirectoryPath() . self::_DEPRECATED_FILE; |
25
|
|
|
|
26
|
|
|
return file_exists($cacheFile) || file_exists($deprecatedCacheFile); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @throws Exception\InvalidResponse |
31
|
|
|
* @throws Exception\RateLimitReached |
32
|
|
|
* @throws Exception\ServiceUnavailable |
33
|
|
|
*/ |
34
|
|
|
public function get(Order $order): Response\Order\AbstractOrder { |
35
|
|
|
|
36
|
|
|
$accountIdentifier = $this->_getObjectIdentifier($order->getAccount()); |
37
|
|
|
$orderIdentifier = $this->_getObjectIdentifier($order); |
38
|
|
|
|
39
|
|
|
if(!isset($this->_responses[$accountIdentifier])) { |
40
|
|
|
$this->_responses[$accountIdentifier] = []; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if(array_key_exists($orderIdentifier, $this->_responses[$accountIdentifier])) { |
44
|
|
|
return $this->_responses[ $accountIdentifier ][ $orderIdentifier ]; |
45
|
|
|
} |
46
|
|
|
$this->_responses[ $accountIdentifier ][ $orderIdentifier ] = null; |
47
|
|
|
|
48
|
|
|
$cacheFile = $order->getKeyDirectoryPath() . self::_FILE; |
49
|
|
|
$deprecatedCacheFile = $order->getKeyDirectoryPath() . self::_DEPRECATED_FILE; |
50
|
|
|
|
51
|
|
|
if(file_exists($deprecatedCacheFile) && !file_exists($cacheFile)) { |
52
|
|
|
rename($deprecatedCacheFile, $cacheFile); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if(file_exists($cacheFile)) { |
56
|
|
|
|
57
|
|
|
$rawResponse = Connector\RawResponse::getFromString(file_get_contents($cacheFile)); |
58
|
|
|
|
59
|
|
|
$response = new Response\Order\Create($rawResponse); |
60
|
|
|
|
61
|
|
|
if( |
62
|
|
|
$response->getStatus() != Response\Order\AbstractOrder::STATUS_VALID |
63
|
|
|
) { |
64
|
|
|
|
65
|
|
|
Utilities\Logger::getInstance()->add( |
|
|
|
|
66
|
|
|
Utilities\Logger::LEVEL_DEBUG, |
67
|
|
|
static::class . '::' . __FUNCTION__ . ' (cache did not satisfy, status "' . $response->getStatus() . '")' |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
$request = new Request\Order\Get($order, $response->getLocation()); |
71
|
|
|
$response = $request->getResponse(); |
72
|
|
|
$this->set($order, $response); |
73
|
|
|
return $response; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
Utilities\Logger::getInstance()->add( |
77
|
|
|
Utilities\Logger::LEVEL_DEBUG, |
78
|
|
|
static::class . '::' . __FUNCTION__ . ' (from cache, status "' . $response->getStatus() . '")' |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
$this->_responses[$accountIdentifier][$orderIdentifier] = $response; |
82
|
|
|
|
83
|
|
|
return $response; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
throw new \RuntimeException( |
87
|
|
|
self::_FILE . ' could not be found for order: ' . |
88
|
|
|
'- Path: ' . $order->getKeyDirectoryPath() . PHP_EOL . |
89
|
|
|
'- Subjects: ' . var_export($order->getSubjects(), true) . PHP_EOL |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function set(Order $order, Response\Order\AbstractOrder $response = null) : void { |
94
|
|
|
|
95
|
|
|
$accountIdentifier = $this->_getObjectIdentifier($order->getAccount()); |
96
|
|
|
$orderIdentifier = $this->_getObjectIdentifier($order); |
97
|
|
|
|
98
|
|
|
$filePath = $order->getKeyDirectoryPath() . self::_FILE; |
99
|
|
|
|
100
|
|
|
if($response === null) { |
101
|
|
|
|
102
|
|
|
unset($this->_responses[$accountIdentifier][$orderIdentifier]); |
103
|
|
|
|
104
|
|
|
if(file_exists($filePath)) { |
105
|
|
|
unlink($filePath); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$this->_responses[$accountIdentifier][$orderIdentifier] = $response; |
112
|
|
|
file_put_contents($filePath, $response->getRaw()->toString()); |
113
|
|
|
} |
114
|
|
|
} |