Passed
Push — master ( e9806b...56ffd0 )
by Luke
09:13
created

MoodleApi::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * Moodle component manager.
5
 *
6
 * @author Luke Carrier <[email protected]>
7
 * @copyright 2016 Luke Carrier
8
 * @license GPL-3.0+
9
 */
10
11
namespace ComponentManager;
12
13
use ComponentManager\Exception\MoodleApiException;
14
use Psr\Http\Message\ResponseInterface;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\Response;
17
18
/**
19
 * Interface to the Moodle.org API.
20
 *
21
 * Provides access to Moodle release and plugin information.
22
 */
23
class MoodleApi {
24
    /**
25
     * Update check endpoint URI.
26
     *
27
     * @var string
28
     */
29
    const URL_UPDATES = 'https://download.moodle.org/api/1.3/updates.php';
30
31
    /**
32
     * HTTP client.
33
     *
34
     * @var HttpClient
35
     */
36
    protected $client;
37
38
    /**
39
     * Initialiser.
40
     *
41
     * @param HttpClient     $client
42
     */
43
    public function __construct(HttpClient $client) {
44
        $this->client = $client;
45
    }
46
47
    /**
48
     * Perform a GET request to the specified URI with the specified parameters.
49
     *
50
     * @param string  $uri
51
     * @param mixed[] $queryParams
52
     *
53
     * @return ResponseInterface
54
     *
55
     * @throws MoodleApiException
56
     */
57
    protected function get($uri, $queryParams) {
58
        $uri = $this->client->createUri($uri)
59
            ->withQuery(http_build_query($queryParams));
60
        $message = $this->client->createRequest(Request::METHOD_GET, $uri);
61
        $response = $this->client->sendRequest($message);
62
63
        if ($response->getStatusCode() !== Response::HTTP_OK) {
64
            throw new MoodleApiException(
65
                    "Request failed to \"{$uri}\" failed",
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $uri instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
66
                    MoodleApiException::CODE_REQUEST_FAILED);
67
        }
68
69
        return $response;
70
    }
71
72
    /**
73
     * Get available Moodle versions.
74
     *
75
     * @return \ComponentManager\MoodleVersion[]
76
     *
77
     * @throws \ComponentManager\Exception\MoodleApiException
78
     */
79
    public function getMoodleVersions() {
80
        $responseBody = json_decode($this->get(static::URL_UPDATES, [
81
            'branch'  => 0,
82
            'version' => 0,
83
        ])->getBody());
84
85
        $result = [];
86
        foreach ($responseBody->updates->core as $version) {
87
            $result[] = new MoodleVersion(
88
                    $version->version, $version->release, $version->branch,
89
                    $version->maturity, $version->download);
90
        }
91
92
        return $result;
93
    }
94
}
95