DNS::setWriter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 2
ccs 0
cts 2
cp 0
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace LE_ACME2\Authorizer;
4
5
use LE_ACME2\Request;
6
use LE_ACME2\Response;
7
use LE_ACME2\Exception;
8
9
use LE_ACME2\Order;
10
use LE_ACME2\Struct\ChallengeAuthorizationKey;
11
use LE_ACME2\Utilities;
12
13
class DNS extends AbstractAuthorizer {
14
15
    protected function _getChallengeType(): string {
16
        return Order::CHALLENGE_TYPE_DNS;
17
    }
18
19
    /** @var AbstractDNSWriter $_dnsWriter */
20
    private static $_dnsWriter = null;
21
22
    public static function setWriter(AbstractDNSWriter $dnsWriter) : void {
23
        self::$_dnsWriter = $dnsWriter;
24
    }
25
26
    /**
27
     * @throws Exception\AuthorizationInvalid
28
     * @throws Exception\DNSAuthorizationInvalid
29
     * @throws Exception\ExpiredAuthorization
30
     * @throws Exception\InvalidResponse
31
     * @throws Exception\RateLimitReached
32
     * @throws Exception\ServiceUnavailable
33
     */
34
    protected function _existsNotValidChallenges(Response\Authorization\Struct\Challenge $challenge,
35
                                                 Response\Authorization\Get $authorizationResponse
36
    ) : bool {
37
38
        if($challenge->status == Response\Authorization\Struct\Challenge::STATUS_PENDING) {
39
40
            if(self::$_dnsWriter === null) {
41
                throw new \RuntimeException('DNS writer is not set');
42
            }
43
44
            if( self::$_dnsWriter->write(
45
                    $this->_order,
46
                    $authorizationResponse->getIdentifier()->value,
47
                    (new ChallengeAuthorizationKey($this->_account))->getEncoded($challenge->token)
48
                )
49
            ) {
50
                $request = new Request\Authorization\Start($this->_account, $this->_order, $challenge);
51
                /* $response = */ $request->getResponse();
52
            } else {
53
54
                Utilities\Logger::getInstance()->add(Utilities\Logger::LEVEL_INFO, 'Pending challenge deferred');
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

54
                Utilities\Logger::getInstance()->/** @scrutinizer ignore-call */ add(Utilities\Logger::LEVEL_INFO, 'Pending challenge deferred');
Loading history...
55
            }
56
        }
57
58
        if($challenge->status == Response\Authorization\Struct\Challenge::STATUS_INVALID) {
59
            throw new Exception\DNSAuthorizationInvalid(
60
                'Received status "' . Response\Authorization\Struct\Challenge::STATUS_INVALID . '" while challenge should be verified'
61
            );
62
        }
63
64
        return parent::_existsNotValidChallenges($challenge, $authorizationResponse);
65
    }
66
}