ChangeKeys::getResponse()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 54
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 34
dl 0
loc 54
ccs 0
cts 26
cp 0
rs 9.376
c 2
b 0
f 0
cc 1
nc 1
nop 0
crap 2

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
3
namespace LE_ACME2\Request\Account;
4
5
use LE_ACME2\Request\AbstractRequest;
6
use LE_ACME2\Response;
7
8
use LE_ACME2\Connector;
9
use LE_ACME2\Cache;
10
use LE_ACME2\Utilities;
11
use LE_ACME2\Exception;
12
13
use LE_ACME2\Account;
14
15
class ChangeKeys extends AbstractRequest {
16
17
    protected $_account;
18
19
    public function __construct(Account $account) {
20
        $this->_account = $account;
21
    }
22
23
    /**
24
     * @throws Exception\InvalidResponse
25
     * @throws Exception\RateLimitReached
26
     * @throws Exception\ServiceUnavailable
27
     */
28
    public function getResponse() : Response\Account\ChangeKeys {
29
30
        $currentPrivateKey = openssl_pkey_get_private(
31
            file_get_contents($this->_account->getKeyDirectoryPath() . 'private.pem')
32
        );
33
        $currentPrivateKeyDetails = openssl_pkey_get_details($currentPrivateKey);
34
35
        /**
36
         *  draft-13 Section 7.3.6
37
         *  "newKey" is deprecated after August 23rd 2018
38
         */
39
        $newPrivateKey = openssl_pkey_get_private(
40
            file_get_contents($this->_account->getKeyDirectoryPath() . 'private-replacement.pem')
41
        );
42
        $newPrivateKeyDetails = openssl_pkey_get_details($newPrivateKey);
43
44
        $innerPayload = [
45
            'account' => Cache\AccountResponse::getInstance()->get($this->_account)->getLocation(),
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

45
            'account' => Cache\AccountResponse::getInstance()->/** @scrutinizer ignore-call */ get($this->_account)->getLocation(),
Loading history...
46
            'oldKey' => [
47
                "kty" => "RSA",
48
                "n" => Utilities\Base64::UrlSafeEncode($currentPrivateKeyDetails["rsa"]["n"]),
49
                "e" => Utilities\Base64::UrlSafeEncode($currentPrivateKeyDetails["rsa"]["e"])
50
            ],
51
            'newKey' => [
52
                "kty" => "RSA",
53
                "n" => Utilities\Base64::UrlSafeEncode($newPrivateKeyDetails["rsa"]["n"]),
54
                "e" => Utilities\Base64::UrlSafeEncode($newPrivateKeyDetails["rsa"]["e"])
55
            ]
56
        ];
57
58
        $outerPayload = Utilities\RequestSigner::JWK(
59
            $innerPayload,
60
            Cache\DirectoryResponse::getInstance()->get()->getKeyChange(),
61
            Cache\NewNonceResponse::getInstance()->get()->getNonce(),
62
            $this->_account->getKeyDirectoryPath(),
63
            'private-replacement.pem'
64
        );
65
66
        $data = Utilities\RequestSigner::KID(
67
            $outerPayload,
68
            Cache\AccountResponse::getInstance()->get($this->_account)->getLocation(),
69
            Cache\DirectoryResponse::getInstance()->get()->getKeyChange(),
70
            Cache\NewNonceResponse::getInstance()->get()->getNonce(),
71
            $this->_account->getKeyDirectoryPath(),
72
            'private.pem'
73
        );
74
75
        $result = Connector\Connector::getInstance()->request(
0 ignored issues
show
Bug introduced by
It seems like request() 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

75
        $result = Connector\Connector::getInstance()->/** @scrutinizer ignore-call */ request(
Loading history...
76
            Connector\Connector::METHOD_POST,
77
            Cache\DirectoryResponse::getInstance()->get()->getKeyChange(),
78
            $data
79
        );
80
81
        return new Response\Account\ChangeKeys($result);
82
    }
83
}
84