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

AbstractAuthorizer::progress()   A

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 12
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
     * @param Account $account
23
     * @param Order $order
24
     *
25
     * @throws Exception\InvalidResponse
26
     * @throws Exception\RateLimitReached
27
     * @throws Exception\ExpiredAuthorization
28
     */
29
    public function __construct(Account $account, Order $order) {
30
31
        $this->_account = $account;
32
        $this->_order = $order;
33
34
        $this->_fetchAuthorizationResponses();
35
    }
36
37
    /** @var Response\Authorization\Get[] $_authorizationResponses */
38
    protected $_authorizationResponses = [];
39
40
    /**
41
     * @throws Exception\InvalidResponse
42
     * @throws Exception\RateLimitReached
43
     * @throws Exception\ExpiredAuthorization
44
     */
45
    protected function _fetchAuthorizationResponses() {
46
47
        if(!file_exists($this->_order->getKeyDirectoryPath() . 'private.pem')) {
48
49
            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

49
            Utilities\Logger::getInstance()->/** @scrutinizer ignore-call */ add(
Loading history...
50
                Utilities\Logger::LEVEL_DEBUG,
51
                get_class() . '::' . __FUNCTION__ . ' result suppressed (Order has finished already)',
52
            );
53
54
            return;
55
        }
56
57
        $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

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

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