Completed
Pull Request — master (#8)
by John
02:04
created

LEAuthorization   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 36
c 1
b 1
f 0
dl 0
loc 84
rs 10
ccs 27
cts 27
cp 1
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A updateData() 0 18 4
A __construct() 0 22 4
A getChallenge() 0 7 3
1
<?php
2
3
namespace LEClient;
4
5
use LEClient\Exceptions\LEAuthorizationException;
6
7
/**
8
 * LetsEncrypt Authorization class, getting LetsEncrypt authorization data associated with a LetsEncrypt Order instance.
9
 *
10
 * PHP version 5.2.0
11
 *
12
 * MIT License
13
 *
14
 * Copyright (c) 2018 Youri van Weegberg
15
 *
16
 * Permission is hereby granted, free of charge, to any person obtaining a copy
17
 * of this software and associated documentation files (the "Software"), to deal
18
 * in the Software without restriction, including without limitation the rights
19
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
20
 * copies of the Software, and to permit persons to whom the Software is
21
 * furnished to do so, subject to the following conditions:
22
 *
23
 * The above copyright notice and this permission notice shall be included in all
24
 * copies or substantial portions of the Software.
25
 *
26
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32
 * SOFTWARE.
33
 *
34 56
 * @author     Youri van Weegberg <[email protected]>
35
 * @copyright  2018 Youri van Weegberg
36 56
 * @license    https://opensource.org/licenses/mit-license.php  MIT License
37 56
 * @link       https://github.com/yourivw/LEClient
38 56
 * @since      Class available since Release 1.0.0
39
 */
40 56
class LEAuthorization
41 56
{
42 56
	private $connector;
43 56
44
	public $authorizationURL;
45
	public $identifier;
46 56
	public $status;
47 56
	public $expires;
48 56
	public $challenges;
49 56
50 56
	private $log;
51 56
52
    /**
53
     * Initiates the LetsEncrypt Authorization class. Child of a LetsEncrypt Order instance.
54
     *
55
     * @param LEConnector	$connector			The LetsEncrypt Connector instance to use for HTTP requests.
56
     * @param int 			$log 				The level of logging. Defaults to no logging. LOG_OFF, LOG_STATUS, LOG_DEBUG accepted.
57 56
     * @param string 		$authorizationURL 	The URL of the authorization, given by a LetsEncrypt order request.
58
     */
59
	public function __construct($connector, $log, $authorizationURL)
60
	{
61
		$this->connector = $connector;
62
		$this->log = $log;
63 2
		$this->authorizationURL = $authorizationURL;
64
65 2
		$sign = $this->connector->signRequestKid('', $this->connector->accountURL, $this->authorizationURL);
0 ignored issues
show
Bug introduced by
'' of type string is incompatible with the type array expected by parameter $payload of LEClient\LEConnector::signRequestKid(). ( Ignorable by Annotation )

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

65
		$sign = $this->connector->signRequestKid(/** @scrutinizer ignore-type */ '', $this->connector->accountURL, $this->authorizationURL);
Loading history...
66 2
		$post = $this->connector->post($this->authorizationURL, $sign);
0 ignored issues
show
Bug introduced by
$sign of type string is incompatible with the type object expected by parameter $data of LEClient\LEConnector::post(). ( Ignorable by Annotation )

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

66
		$post = $this->connector->post($this->authorizationURL, /** @scrutinizer ignore-type */ $sign);
Loading history...
67 2
		if($post['status'] === 200)
68 2
		{
69
			$this->identifier = $post['body']['identifier'];
70
			$this->status = $post['body']['status'];
71 2
			$this->expires = $post['body']['expires'];
72 2
			$this->challenges = $post['body']['challenges'];
73 2
		}
74 2
		else
75 2
		{
76 2
			if($this->log instanceof \Psr\Log\LoggerInterface) 
0 ignored issues
show
introduced by
$this->log is never a sub-type of Psr\Log\LoggerInterface.
Loading history...
77
			{
78
				$this->log->info('Cannot find authorization \'' . $authorizationURL . '\'.');
79
			}
80
			elseif($this->log >= LEClient::LOG_STATUS) LEFunctions::log('Cannot find authorization \'' . $authorizationURL . '\'.', 'function LEAuthorization __construct');
0 ignored issues
show
Bug introduced by
'Cannot find authorizati...authorizationURL . ''.' of type string is incompatible with the type object expected by parameter $data of LEClient\LEFunctions::log(). ( Ignorable by Annotation )

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

80
			elseif($this->log >= LEClient::LOG_STATUS) LEFunctions::log(/** @scrutinizer ignore-type */ 'Cannot find authorization \'' . $authorizationURL . '\'.', 'function LEAuthorization __construct');
Loading history...
81
		}
82 2
	}
83
84
    /**
85
     * Updates the data associated with the current LetsEncrypt Authorization instance.
86
     */
87
88
	public function updateData()
89
	{
90
		$sign = $this->connector->signRequestKid('', $this->connector->accountURL, $this->authorizationURL);
0 ignored issues
show
Bug introduced by
'' of type string is incompatible with the type array expected by parameter $payload of LEClient\LEConnector::signRequestKid(). ( Ignorable by Annotation )

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

90
		$sign = $this->connector->signRequestKid(/** @scrutinizer ignore-type */ '', $this->connector->accountURL, $this->authorizationURL);
Loading history...
91
		$post = $this->connector->post($this->authorizationURL, $sign);
0 ignored issues
show
Bug introduced by
$sign of type string is incompatible with the type object expected by parameter $data of LEClient\LEConnector::post(). ( Ignorable by Annotation )

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

91
		$post = $this->connector->post($this->authorizationURL, /** @scrutinizer ignore-type */ $sign);
Loading history...
92
		if($post['status'] === 200)
93 6
		{
94
			$this->identifier = $post['body']['identifier'];
95 6
			$this->status = $post['body']['status'];
96 6
			$this->expires = $post['body']['expires'];
97 6
			$this->challenges = $post['body']['challenges'];
98
		}
99
		else
100
		{
101
			if($this->log instanceof \Psr\Log\LoggerInterface) 
0 ignored issues
show
introduced by
$this->log is never a sub-type of Psr\Log\LoggerInterface.
Loading history...
102
			{
103
				$this->log->info('Cannot find authorization \'' . $this->authorizationURL . '\'.');
104
			}
105
			elseif($this->log >= LEClient::LOG_STATUS) LEFunctions::log('Cannot find authorization \'' . $this->authorizationURL . '\'.', 'function updateData');
0 ignored issues
show
Bug introduced by
'Cannot find authorizati...authorizationURL . ''.' of type string is incompatible with the type object expected by parameter $data of LEClient\LEFunctions::log(). ( Ignorable by Annotation )

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

105
			elseif($this->log >= LEClient::LOG_STATUS) LEFunctions::log(/** @scrutinizer ignore-type */ 'Cannot find authorization \'' . $this->authorizationURL . '\'.', 'function updateData');
Loading history...
106
		}
107
	}
108
109
    /**
110
     * Gets the challenge of the given $type for this LetsEncrypt Authorization instance. Throws a Runtime Exception if the given $type is not found in this
111
	 * LetsEncrypt Authorization instance.
112
     *
113
     * @param int	$type 	The type of verification. Supporting LEOrder::CHALLENGE_TYPE_HTTP and LEOrder::CHALLENGE_TYPE_DNS.
114
     *
115
     * @return array	Returns an array with the challenge of the requested $type.
116
     */
117
	public function getChallenge($type)
118
	{
119
		foreach($this->challenges as $challenge)
120
		{
121
			if($challenge['type'] == $type) return $challenge;
122
		}
123
		throw LEAuthorizationException::NoChallengeFoundException($type, $this->identifier['value']);
124
	}
125
}
126