Passed
Push — develop ( 368e3d...115c7b )
by Nikolay
12:16
created

CheckUpdates::process()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 40
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 24
c 0
b 0
f 0
dl 0
loc 40
rs 9.2248
cc 5
nc 9
nop 0
1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright © 2017-2023 Alexey Portnov and Nikolay Beketov
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with this program.
17
 * If not, see <https://www.gnu.org/licenses/>.
18
 */
19
20
namespace MikoPBX\Core\Workers\Libs\WorkerPrepareAdvice;
21
22
use MikoPBX\Common\Models\PbxSettings;
23
use MikoPBX\Common\Models\PbxSettingsConstants;
24
use MikoPBX\Core\System\SystemMessages;
25
use MikoPBX\PBXCoreREST\Http\Response;
26
use Phalcon\Di\Injectable;
27
use GuzzleHttp;
28
29
/**
30
 * Class CheckUpdates
31
 * This class is responsible for checking PBX updates.
32
 *
33
 * @package MikoPBX\Core\Workers\Libs\WorkerPrepareAdvice
34
 */
35
class CheckUpdates extends Injectable
36
{
37
    /**
38
     * Check for a new version PBX
39
     *
40
     * @return array An array containing information messages about available updates.
41
     *
42
     */
43
    public function process(): array
44
    {
45
        $messages = [];
46
        $PBXVersion = PbxSettings::getValueByKey(PbxSettingsConstants::PBX_VERSION);
47
48
        $client = new GuzzleHttp\Client();
49
        try {
50
            $res = $client->request(
51
                'POST',
52
                'https://releases.mikopbx.com/releases/v1/mikopbx/ifNewReleaseAvailable',
53
                [
54
                    'form_params' => [
55
                        'PBXVER' => $PBXVersion,
56
                    ],
57
                    'timeout' => 5,
58
                ]
59
            );
60
            $code = $res->getStatusCode();
61
        } catch (\Throwable $e) {
62
            $code = Response::INTERNAL_SERVER_ERROR;
63
            SystemMessages::sysLogMsg(static::class, $e->getMessage());
64
        }
65
66
        if ($code !== Response::OK) {
67
            return [];
68
        }
69
70
        $answer = json_decode($res->getBody(), false);
71
        if ($answer !== null && $answer->newVersionAvailable === true) {
72
            $messages['info'][] = [
73
                'messageTpl'=>'adv_AvailableNewVersionPBX',
74
                'messageParams'=>[
75
                    'url' => $this->url->get('update/index/'),
76
                    'ver' => $answer->version,
77
                ]
78
            ];
79
80
        }
81
82
        return $messages;
83
    }
84
85
}