Passed
Push — develop ( 31ebc6...f6a1e0 )
by Nikolay
04:27
created

CheckUpdates   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 25
c 1
b 0
f 0
dl 0
loc 48
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A process() 0 40 5
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\WorkerPrepareAdvices;
21
22
use MikoPBX\Common\Models\PbxSettings;
23
use MikoPBX\Core\System\Util;
24
use MikoPBX\PBXCoreREST\Http\Response;
25
use Phalcon\Di\Injectable;
26
use GuzzleHttp;
27
28
/**
29
 * Class CheckUpdates
30
 * This class is responsible for checking PBX updates.
31
 *
32
 * @package MikoPBX\Core\Workers\Libs\WorkerPrepareAdvices
33
 */
34
class CheckUpdates extends Injectable
35
{
36
    /**
37
     * Check for a new version PBX
38
     *
39
     * @return array An array containing information messages about available updates.
40
     *
41
     */
42
    public function process(): array
43
    {
44
        $messages = [];
45
        $PBXVersion = PbxSettings::getValueByKey('PBXVersion');
46
47
        $client = new GuzzleHttp\Client();
48
        try {
49
            $res = $client->request(
50
                'POST',
51
                'https://releases.mikopbx.com/releases/v1/mikopbx/ifNewReleaseAvailable',
52
                [
53
                    'form_params' => [
54
                        'PBXVER' => $PBXVersion,
55
                    ],
56
                    'timeout' => 5,
57
                ]
58
            );
59
            $code = $res->getStatusCode();
60
        } catch (\Throwable $e) {
61
            $code = Response::INTERNAL_SERVER_ERROR;
62
            Util::sysLogMsg(static::class, $e->getMessage());
63
        }
64
65
        if ($code !== Response::OK) {
66
            return [];
67
        }
68
69
        $answer = json_decode($res->getBody(), false);
70
        if ($answer !== null && $answer->newVersionAvailable === true) {
71
            $messages['info'][] = [
72
                'messageTpl'=>'adv_AvailableNewVersionPBX',
73
                'messageParams'=>[
74
                    'url' => $this->url->get('update/index/'),
75
                    'ver' => $answer->version,
76
                ]
77
            ];
78
79
        }
80
81
        return $messages;
82
    }
83
84
}