OrderResponse::get()   B
last analyzed

Complexity

Conditions 7
Paths 14

Size

Total Lines 56
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 56
ccs 0
cts 29
cp 0
rs 8.4746
c 0
b 0
f 0
cc 7
nc 14
nop 1
crap 56

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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(
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

65
                Utilities\Logger::getInstance()->/** @scrutinizer ignore-call */ add(
Loading history...
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
}