1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* UpdateRequest.php |
4
|
|
|
* Copyright (c) 2019 [email protected] |
5
|
|
|
* |
6
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii). |
7
|
|
|
* |
8
|
|
|
* This program is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU Affero General Public License as |
10
|
|
|
* published by the Free Software Foundation, either version 3 of the |
11
|
|
|
* License, or (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* This program is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU Affero General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU Affero General Public License |
19
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace FireflyIII\Services\FireflyIIIOrg\Update; |
23
|
|
|
|
24
|
|
|
use Exception; |
25
|
|
|
use FireflyIII\Exceptions\FireflyException; |
26
|
|
|
use GuzzleHttp\Client; |
27
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
28
|
|
|
use JsonException; |
29
|
|
|
use Log; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Class UpdateRequest |
33
|
|
|
*/ |
34
|
|
|
class UpdateRequest implements UpdateRequestInterface |
35
|
|
|
{ |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $channel |
39
|
|
|
* |
40
|
|
|
* @return array |
41
|
|
|
* @throws FireflyException |
42
|
|
|
*/ |
43
|
|
|
public function getVersion(string $channel): array |
44
|
|
|
{ |
45
|
|
|
$uri = 'https://version.firefly-iii.org/index.json'; |
46
|
|
|
Log::debug(sprintf('Going to call %s', $uri)); |
47
|
|
|
try { |
48
|
|
|
$client = new Client(); |
49
|
|
|
$res = $client->request('GET', $uri); |
50
|
|
|
} catch (GuzzleException|Exception $e) { |
51
|
|
|
throw new FireflyException(sprintf('Response error from update check: %s', $e->getMessage())); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if (200 !== $res->getStatusCode()) { |
55
|
|
|
throw new FireflyException(sprintf('Returned error code %d from update check.', $res->getStatusCode())); |
56
|
|
|
} |
57
|
|
|
$body = (string)$res->getBody(); |
58
|
|
|
try { |
59
|
|
|
$json = json_decode($body, true, 512, JSON_THROW_ON_ERROR); |
60
|
|
|
} catch (JsonException $e) { |
61
|
|
|
throw new FireflyException('Invalid JSON in server response.'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (!isset($json[$channel])) { |
65
|
|
|
throw new FireflyException(sprintf('Unknown update channel "%s"', $channel)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $json[$channel]; |
69
|
|
|
} |
70
|
|
|
} |