Passed
Branch master (fc5382)
by Fabian
03:13
created

OrderAuthorizationResponse::get()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 49
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
dl 0
loc 49
ccs 0
cts 28
cp 0
rs 8.8497
c 1
b 0
f 0
cc 6
nc 8
nop 3
crap 42
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 OrderAuthorizationResponse extends AbstractKeyValuableCache {
13
14
    use SingletonTrait;
15
16
    private const _FILE_prefix = 'CacheAuthorizationResponse';
17
18
    private $_responses = [];
19
20
    private function _getCacheFilePath(Order $order, string $authorizationUrl) : string {
21
        return $order->getKeyDirectoryPath() . self::_FILE_prefix . '-' . md5($authorizationUrl);
22
    }
23
24
    /**
25
     * @param Order $order
26
     * @param string $authorizationUrl
27
     * @return Response\Authorization\Get
28
     * @throws Exception\ExpiredAuthorization
29
     * @throws Exception\InvalidResponse
30
     * @throws Exception\RateLimitReached
31
     */
32
    public function get(Order $order, string $authorizationUrl, string $challengeType): Response\Authorization\Get {
33
34
        $accountIdentifier = $this->_getObjectIdentifier($order->getAccount());
35
        $orderIdentifier = $this->_getObjectIdentifier($order);
36
37
        if(!isset($this->_responses[$accountIdentifier][$orderIdentifier])) {
38
            $this->_responses[$accountIdentifier][$orderIdentifier] = [];
39
        }
40
41
        if(array_key_exists($authorizationUrl, $this->_responses[$accountIdentifier][$orderIdentifier])) {
42
            return $this->_responses[ $accountIdentifier ][ $orderIdentifier ][ $authorizationUrl ];
43
        }
44
        $this->_responses[ $accountIdentifier ][ $orderIdentifier ][ $authorizationUrl ] = null;
45
46
        $cacheFile = $this->_getCacheFilePath($order, $authorizationUrl);
47
48
        if(file_exists($cacheFile)) {
49
50
            $rawResponse = Connector\RawResponse::getFromString(file_get_contents($cacheFile));
51
52
            $response = new Response\Authorization\Get($rawResponse);
53
            if(
54
                ($challenge = $response->getChallenge($challengeType)) &&
55
                $challenge->status == Response\Authorization\Struct\Challenge::STATUS_PROGRESSING
56
            ) {
57
58
                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

58
                Utilities\Logger::getInstance()->/** @scrutinizer ignore-call */ add(
Loading history...
59
                    Utilities\Logger::LEVEL_DEBUG,
60
                    get_class() . '::' . __FUNCTION__ . ' (cache did not satisfy, status "' . $response->getStatus() . '")'
61
                );
62
63
                $request = new Request\Authorization\Get($order->getAccount(), $authorizationUrl);
64
                $this->set($order, $authorizationUrl, $response = $request->getResponse());
65
                return $response;
66
            }
67
68
            Utilities\Logger::getInstance()->add(
69
                Utilities\Logger::LEVEL_DEBUG,
70
                get_class() . '::' . __FUNCTION__ .  ' (from cache, status "' . $response->getStatus() . '")'
71
            );
72
73
            $this->_responses[$accountIdentifier][$orderIdentifier][$authorizationUrl] = $response;
74
75
            return $response;
76
        }
77
78
        $request = new Request\Authorization\Get($order->getAccount(), $authorizationUrl);
79
        $this->set($order, $authorizationUrl, $response = $request->getResponse());
80
        return $response;
81
    }
82
83
    public function set(Order $order, string $authorizationUrl, Response\Authorization\Get $response = null) : void {
84
85
        $accountIdentifier = $this->_getObjectIdentifier($order->getAccount());
86
        $orderIdentifier = $this->_getObjectIdentifier($order);
87
88
        $filePath = $this->_getCacheFilePath($order, $authorizationUrl);
89
90
        if($response === null) {
91
92
            unset($this->_responses[$accountIdentifier][$orderIdentifier][$authorizationUrl]);
93
94
            if(file_exists($filePath)) {
95
                unlink($filePath);
96
            }
97
98
            return;
99
        }
100
101
        $this->_responses[$accountIdentifier][$orderIdentifier][$authorizationUrl] = $response;
102
        file_put_contents($filePath, $response->getRaw()->toString());
103
    }
104
105
    /**
106
     * Clear the cache, when the next response could be different:
107
     * - Order has ended (certificate received)
108
     * - Authorization::Start
109
     *
110
     * @param Order $order
111
     * @throws Exception\ExpiredAuthorization
112
     * @throws Exception\InvalidResponse
113
     * @throws Exception\RateLimitReached
114
     */
115
    public function clear(Order $order) : void {
116
117
        $orderResponse = OrderResponse::getInstance()->get($order);
0 ignored issues
show
Bug introduced by
It seems like get() 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

117
        $orderResponse = OrderResponse::getInstance()->/** @scrutinizer ignore-call */ get($order);
Loading history...
118
        foreach($orderResponse->getAuthorizations() as $authorization) {
119
            $this->set($order, $authorization, null);
120
        }
121
    }
122
}