Completed
Pull Request — master (#2)
by John
11:47 queued 16s
created

LEAuthorization   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 61.29%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 40
dl 0
loc 89
ccs 19
cts 31
cp 0.6129
rs 10
c 1
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getChallenge() 0 10 3
A __construct() 0 21 2
A updateData() 0 17 2
1
<?php
2
namespace Zwartpet\PHPCertificateToolbox;
3
4
use Zwartpet\PHPCertificateToolbox\Exception\RuntimeException;
5
use Psr\Log\LoggerInterface;
6
7
/**
8
 * LetsEncrypt Authorization class, getting LetsEncrypt authorization data associated with a LetsEncrypt Order instance.
9
 *
10
 * @author     Youri van Weegberg <[email protected]>
11
 * @copyright  2018 Youri van Weegberg
12
 * @license    https://opensource.org/licenses/mit-license.php  MIT License
13
 */
14
class LEAuthorization
15
{
16
    private $connector;
17
18
    public $authorizationURL;
19
    public $identifier;
20
    public $status;
21
    public $expires;
22
    public $challenges;
23
24
    /** @var LoggerInterface  */
25
    private $log;
26
27
    /**
28
     * Initiates the LetsEncrypt Authorization class. Child of a LetsEncrypt Order instance.
29
     *
30
     * @param LEConnector $connector The LetsEncrypt Connector instance to use for HTTP requests.
31
     * @param LoggerInterface $log PSR-3 logger
32
     * @param string $authorizationURL The URL of the authorization, given by a LetsEncrypt order request.
33
     */
34 54
    public function __construct($connector, LoggerInterface $log, $authorizationURL)
35
    {
36 54
        $this->connector = $connector;
37 54
        $this->log = $log;
38 54
        $this->authorizationURL = $authorizationURL;
39
40 54
        $sign = $this->connector->signRequestKid(
41 54
            null,
42 54
            $this->connector->accountURL,
43 54
            $this->authorizationURL
44
        );
45
46 54
        $post = $this->connector->post($this->authorizationURL, $sign);
47 54
        if ($post['status'] === 200) {
48 54
            $this->identifier = $post['body']['identifier'];
49 54
            $this->status = $post['body']['status'];
50 54
            $this->expires = $post['body']['expires'];
51 54
            $this->challenges = $post['body']['challenges'];
52
        } else {
53
            //@codeCoverageIgnoreStart
54
            $this->log->error("LEAuthorization::__construct cannot find authorization $authorizationURL");
55
            //@codeCoverageIgnoreEnd
56
        }
57 54
    }
58
59
    /**
60
     * Updates the data associated with the current LetsEncrypt Authorization instance.
61
     */
62
63
    public function updateData()
64
    {
65
        $sign = $this->connector->signRequestKid(
66
            null,
67
            $this->connector->accountURL,
68
            $this->authorizationURL
69
        );
70
71
        $post = $this->connector->post($this->authorizationURL, $sign);
72
        if ($post['status'] === 200) {
73
            $this->identifier = $post['body']['identifier'];
74
            $this->status = $post['body']['status'];
75
            $this->expires = $post['body']['expires'];
76
            $this->challenges = $post['body']['challenges'];
77
        } else {
78
            //@codeCoverageIgnoreStart
79
            $this->log->error("LEAuthorization::updateData cannot find authorization " . $this->authorizationURL);
80
            //@codeCoverageIgnoreEnd
81
        }
82
    }
83
84
    /**
85
     * Gets the challenge of the given $type for this LetsEncrypt Authorization instance.
86
     * Throws a Runtime Exception if the given $type is not found in this LetsEncrypt Authorization instance.
87
     *
88
     * @param string $type The type of verification.
89
     *                     Supporting LEOrder::CHALLENGE_TYPE_HTTP and LEOrder::CHALLENGE_TYPE_DNS.
90
     *
91
     * @return array Returns an array with the challenge of the requested $type.
92
     */
93 4
    public function getChallenge($type)
94
    {
95 4
        foreach ($this->challenges as $challenge) {
96 4
            if ($challenge['type'] == $type) {
97 4
                return $challenge;
98
            }
99
        }
100
        //@codeCoverageIgnoreStart
101
        throw new RuntimeException(
102
            'No challenge found for type \'' . $type . '\' and identifier \'' . $this->identifier['value'] . '\'.'
103
        );
104
        //@codeCoverageIgnoreEnd
105
    }
106
}
107