Completed
Pull Request — master (#1)
by John
05:30
created

LEAuthorization::updateData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 0
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 2
rs 10
c 0
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 56
    public function __construct($connector, LoggerInterface $log, $authorizationURL)
35
    {
36 56
        $this->connector = $connector;
37 56
        $this->log = $log;
38 56
        $this->authorizationURL = $authorizationURL;
39
40 56
        $get = $this->connector->get($this->authorizationURL);
41 56
        if ($get['status'] === 200) {
42 56
            $this->identifier = $get['body']['identifier'];
43 56
            $this->status = $get['body']['status'];
44 56
            $this->expires = $get['body']['expires'];
45 56
            $this->challenges = $get['body']['challenges'];
46
        } else {
47
            //@codeCoverageIgnoreStart
48
            $this->log->error("LEAuthorization::__construct cannot find authorization $authorizationURL");
49
            //@codeCoverageIgnoreEnd
50
        }
51 56
    }
52
53
    /**
54
     * Updates the data associated with the current LetsEncrypt Authorization instance.
55
     */
56
57 2
    public function updateData()
58
    {
59 2
        $get = $this->connector->get($this->authorizationURL);
60 2
        if ($get['status'] === 200) {
61 2
            $this->identifier = $get['body']['identifier'];
62 2
            $this->status = $get['body']['status'];
63 2
            $this->expires = $get['body']['expires'];
64 2
            $this->challenges = $get['body']['challenges'];
65
        } else {
66
            //@codeCoverageIgnoreStart
67
            $this->log->error("LEAuthorization::updateData cannot find authorization " . $this->authorizationURL);
68
            //@codeCoverageIgnoreEnd
69
        }
70 2
    }
71
72
    /**
73
     * Gets the challenge of the given $type for this LetsEncrypt Authorization instance.
74
     * Throws a Runtime Exception if the given $type is not found in this LetsEncrypt Authorization instance.
75
     *
76
     * @param string $type The type of verification.
77
     *                     Supporting LEOrder::CHALLENGE_TYPE_HTTP and LEOrder::CHALLENGE_TYPE_DNS.
78
     *
79
     * @return array Returns an array with the challenge of the requested $type.
80
     */
81 6
    public function getChallenge($type)
82
    {
83 6
        foreach ($this->challenges as $challenge) {
84 6
            if ($challenge['type'] == $type) {
85 6
                return $challenge;
86
            }
87
        }
88
        //@codeCoverageIgnoreStart
89
        throw new RuntimeException(
90
            'No challenge found for type \'' . $type . '\' and identifier \'' . $this->identifier['value'] . '\'.'
91
        );
92
        //@codeCoverageIgnoreEnd
93
    }
94
}
95