AbstractAuthorizer::progress()   A
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 11
dl 0
loc 23
ccs 0
cts 11
cp 0
rs 9.2222
c 2
b 0
f 0
cc 6
nc 5
nop 0
crap 42
1
<?php
2
3
namespace LE_ACME2\Authorizer;
4
5
use LE_ACME2\Response;
6
7
use LE_ACME2\Cache;
8
use LE_ACME2\Utilities;
9
use LE_ACME2\Exception;
10
11
use LE_ACME2\Account;
12
use LE_ACME2\Order;
13
14
abstract class AbstractAuthorizer {
15
16
    protected $_account;
17
    protected $_order;
18
19
    /**
20
     * AbstractAuthorizer constructor.
21
     *
22
     * @throws Exception\InvalidResponse
23
     * @throws Exception\RateLimitReached
24
     * @throws Exception\ExpiredAuthorization
25
     */
26
    public function __construct(Account $account, Order $order) {
27
28
        $this->_account = $account;
29
        $this->_order = $order;
30
31
        $this->_fetchAuthorizationResponses();
32
    }
33
34
    /** @var Response\Authorization\Get[] $_authorizationResponses */
35
    protected $_authorizationResponses = [];
36
37
    /**
38
     * @throws Exception\InvalidResponse
39
     * @throws Exception\RateLimitReached
40
     * @throws Exception\ExpiredAuthorization
41
     */
42
    protected function _fetchAuthorizationResponses() {
43
44
        if(!file_exists($this->_order->getKeyDirectoryPath() . 'private.pem')) {
45
46
            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

46
            Utilities\Logger::getInstance()->/** @scrutinizer ignore-call */ add(
Loading history...
47
                Utilities\Logger::LEVEL_DEBUG,
48
                static::class . '::' . __FUNCTION__ . ' result suppressed (Order has finished already)',
49
            );
50
51
            return;
52
        }
53
54
        $orderResponse = Cache\OrderResponse::getInstance()->get($this->_order);
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

54
        $orderResponse = Cache\OrderResponse::getInstance()->/** @scrutinizer ignore-call */ get($this->_order);
Loading history...
55
56
        foreach($orderResponse->getAuthorizations() as $authorization) {
57
58
            $this->_authorizationResponses[] = Cache\OrderAuthorizationResponse::getInstance()
59
                ->get($this->_order, $authorization, $this->_getChallengeType());
60
        }
61
    }
62
63
    protected function _hasValidAuthorizationResponses() : bool {
64
65
        return count($this->_authorizationResponses) > 0;
66
    }
67
68
    public function shouldStartAuthorization() : bool {
69
70
        foreach($this->_authorizationResponses as $response) {
71
72
            $challenge = $response->getChallenge($this->_getChallengeType());
73
            if($challenge && $challenge->status == Response\Authorization\Struct\Challenge::STATUS_PENDING) {
74
75
                Utilities\Logger::getInstance()->add(
76
                    Utilities\Logger::LEVEL_DEBUG,
77
                    static::class . '::' . __FUNCTION__ . ' "Pending challenge found',
78
                    [get_class($challenge) => $challenge]
79
                );
80
81
                return true;
82
            }
83
        }
84
        return false;
85
    }
86
87
    abstract protected function _getChallengeType() : string;
88
89
    private $_progressed = false;
90
91
    /**
92
     * @throws Exception\AuthorizationInvalid
93
     * @throws Exception\InvalidResponse
94
     * @throws Exception\RateLimitReached
95
     * @throws Exception\ExpiredAuthorization
96
     */
97
    public function progress() {
98
99
        if($this->_progressed) {
100
            return;
101
        }
102
103
        $this->_progressed = true;
104
105
        if(!$this->_hasValidAuthorizationResponses())
106
            return;
107
108
        $existsNotValidChallenges = false;
109
110
        foreach($this->_authorizationResponses as $authorizationResponse) {
111
112
            $challenge = $authorizationResponse->getChallenge($this->_getChallengeType());
113
114
            if($challenge && $this->_existsNotValidChallenges($challenge, $authorizationResponse)) {
115
                $existsNotValidChallenges = true;
116
            }
117
        }
118
119
        $this->_finished = !$existsNotValidChallenges;
120
    }
121
122
    /**
123
     * @throws Exception\AuthorizationInvalid
124
     */
125
    protected function _existsNotValidChallenges(Response\Authorization\Struct\Challenge $challenge,
126
                                                 Response\Authorization\Get $authorizationResponse
0 ignored issues
show
Unused Code introduced by
The parameter $authorizationResponse is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

126
                                                 /** @scrutinizer ignore-unused */ Response\Authorization\Get $authorizationResponse

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
127
    ) : bool {
128
129
        if($challenge->status == Response\Authorization\Struct\Challenge::STATUS_PENDING) {
130
131
            Utilities\Logger::getInstance()->add(
132
                Utilities\Logger::LEVEL_DEBUG,
133
                static::class . '::' . __FUNCTION__ . ' "Non valid challenge found',
134
                [get_class($challenge) => $challenge]
135
            );
136
137
            return true;
138
        }
139
        else if($challenge->status == Response\Authorization\Struct\Challenge::STATUS_PROGRESSING) {
140
141
            // Should come back later
142
            return true;
143
        }
144
        else if($challenge->status == Response\Authorization\Struct\Challenge::STATUS_VALID) {
145
146
        }
147
        else if($challenge->status == Response\Authorization\Struct\Challenge::STATUS_INVALID) {
148
            throw new Exception\AuthorizationInvalid(
149
                'Received status "' . Response\Authorization\Struct\Challenge::STATUS_INVALID . '" while challenge should be verified'
150
            );
151
        }
152
        else {
153
154
            throw new \RuntimeException('Challenge status "' . $challenge->status . '" is not implemented');
155
        }
156
157
        return false;
158
    }
159
160
    protected $_finished = false;
161
162
    public function hasFinished() : bool {
163
164
        Utilities\Logger::getInstance()->add(
165
            Utilities\Logger::LEVEL_DEBUG,
166
            get_called_class() . '::' . __FUNCTION__,
167
            ['finished' => $this->_finished]
168
        );
169
170
        return $this->_finished;
171
    }
172
}
173
174