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

DNS   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 19
dl 0
loc 55
ccs 0
cts 20
cp 0
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setWriter() 0 2 1
A _getChallengeType() 0 2 1
A _existsNotValidChallenges() 0 31 5
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
     * @param Response\Authorization\Struct\Challenge $challenge
28
     * @param Response\Authorization\Get $authorizationResponse
29
     * @return bool
30
     *
31
     * @throws Exception\AuthorizationInvalid
32
     * @throws Exception\DNSAuthorizationInvalid
33
     * @throws Exception\ExpiredAuthorization
34
     * @throws Exception\InvalidResponse
35
     * @throws Exception\RateLimitReached
36
     */
37
    protected function _existsNotValidChallenges(Response\Authorization\Struct\Challenge $challenge,
38
                                                 Response\Authorization\Get $authorizationResponse
39
    ) : bool {
40
41
        if($challenge->status == Response\Authorization\Struct\Challenge::STATUS_PENDING) {
42
43
            if(self::$_dnsWriter === null) {
44
                throw new \RuntimeException('DNS writer is not set');
45
            }
46
47
            if( self::$_dnsWriter->write(
48
                    $this->_order,
49
                    $authorizationResponse->getIdentifier()->value,
50
                    (new ChallengeAuthorizationKey($this->_account))->getEncoded($challenge->token)
51
                )
52
            ) {
53
                $request = new Request\Authorization\Start($this->_account, $this->_order, $challenge);
54
                /* $response = */ $request->getResponse();
55
            } else {
56
57
                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

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