Completed
Pull Request — master (#2)
by John
03:28 queued 55s
created

LEAuthorization::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2.0932

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 15
nc 2
nop 3
dl 0
loc 21
ccs 10
cts 14
cp 0.7143
crap 2.0932
rs 9.7666
c 1
b 0
f 0
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
            $this->identifier = $post['body']['identifier'];
49
            $this->status = $post['body']['status'];
50
            $this->expires = $post['body']['expires'];
51
            $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
        $get = $this->connector->get($this->authorizationURL);
66
        if ($get['status'] === 200) {
67
            $this->identifier = $get['body']['identifier'];
68
            $this->status = $get['body']['status'];
69
            $this->expires = $get['body']['expires'];
70
            $this->challenges = $get['body']['challenges'];
71
        } else {
72
            //@codeCoverageIgnoreStart
73
            $this->log->error("LEAuthorization::updateData cannot find authorization " . $this->authorizationURL);
74
            //@codeCoverageIgnoreEnd
75
        }
76
    }
77
78
    /**
79
     * Gets the challenge of the given $type for this LetsEncrypt Authorization instance.
80
     * Throws a Runtime Exception if the given $type is not found in this LetsEncrypt Authorization instance.
81
     *
82
     * @param string $type The type of verification.
83
     *                     Supporting LEOrder::CHALLENGE_TYPE_HTTP and LEOrder::CHALLENGE_TYPE_DNS.
84
     *
85
     * @return array Returns an array with the challenge of the requested $type.
86
     */
87
    public function getChallenge($type)
88
    {
89
        foreach ($this->challenges as $challenge) {
90
            if ($challenge['type'] == $type) {
91
                return $challenge;
92
            }
93
        }
94
        //@codeCoverageIgnoreStart
95
        throw new RuntimeException(
96
            'No challenge found for type \'' . $type . '\' and identifier \'' . $this->identifier['value'] . '\'.'
97
        );
98
        //@codeCoverageIgnoreEnd
99
    }
100
}
101