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

OrderResponse::get()   B

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 32
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
     * @param Order $order
31
     * @return Response\Order\AbstractOrder
32
     * @throws Exception\InvalidResponse
33
     * @throws Exception\RateLimitReached
34
     */
35
    public function get(Order $order): Response\Order\AbstractOrder {
36
37
        $accountIdentifier = $this->_getObjectIdentifier($order->getAccount());
38
        $orderIdentifier = $this->_getObjectIdentifier($order);
39
40
        if(!isset($this->_responses[$accountIdentifier])) {
41
            $this->_responses[$accountIdentifier] = [];
42
        }
43
44
        if(array_key_exists($orderIdentifier, $this->_responses[$accountIdentifier])) {
45
            return $this->_responses[ $accountIdentifier ][ $orderIdentifier ];
46
        }
47
        $this->_responses[ $accountIdentifier ][ $orderIdentifier ] = null;
48
49
        $cacheFile = $order->getKeyDirectoryPath() . self::_FILE;
50
        $deprecatedCacheFile = $order->getKeyDirectoryPath() . self::_DEPRECATED_FILE;
51
52
        if(file_exists($deprecatedCacheFile) && !file_exists($cacheFile)) {
53
            rename($deprecatedCacheFile, $cacheFile);
54
        }
55
56
        if(file_exists($cacheFile)) {
57
58
            $rawResponse = Connector\RawResponse::getFromString(file_get_contents($cacheFile));
59
60
            $response = new Response\Order\Create($rawResponse);
61
62
            if(
63
                $response->getStatus() != Response\Order\AbstractOrder::STATUS_VALID
64
            ) {
65
66
                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

66
                Utilities\Logger::getInstance()->/** @scrutinizer ignore-call */ add(
Loading history...
67
                    Utilities\Logger::LEVEL_DEBUG,
68
                    get_class() . '::' . __FUNCTION__ . ' (cache did not satisfy, status "' . $response->getStatus() . '")'
69
                );
70
71
                $request = new Request\Order\Get($order, $response);
72
                $response = $request->getResponse();
73
                $this->set($order, $response);
74
                return $response;
75
            }
76
77
            Utilities\Logger::getInstance()->add(
78
                Utilities\Logger::LEVEL_DEBUG,
79
                get_class() . '::' . __FUNCTION__ .  ' (from cache, status "' . $response->getStatus() . '")'
80
            );
81
82
            $this->_responses[$accountIdentifier][$orderIdentifier] = $response;
83
84
            return $response;
85
        }
86
87
        throw new \RuntimeException(
88
            self::_FILE . ' could not be found for order: ' .
89
            '- Path: ' . $order->getKeyDirectoryPath() . PHP_EOL .
90
            '- Subjects: ' . var_export($order->getSubjects(), true) . PHP_EOL
91
        );
92
    }
93
94
    public function set(Order $order, Response\Order\AbstractOrder $response = null) : void {
95
96
        $accountIdentifier = $this->_getObjectIdentifier($order->getAccount());
97
        $orderIdentifier = $this->_getObjectIdentifier($order);
98
99
        $filePath = $order->getKeyDirectoryPath() . self::_FILE;
100
101
        if($response === null) {
102
103
            unset($this->_responses[$accountIdentifier][$orderIdentifier]);
104
105
            if(file_exists($filePath)) {
106
                unlink($filePath);
107
            }
108
109
            return;
110
        }
111
112
        $this->_responses[$accountIdentifier][$orderIdentifier] = $response;
113
        file_put_contents($filePath, $response->getRaw()->toString());
114
    }
115
}