Passed
Push — master ( 6042d7...377078 )
by Mihail
04:38
created

EntityUpdate::before()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 9
rs 9.6666
1
<?php
2
3
namespace Apps\Model\Admin\Main;
4
5
6
use Apps\ActiveRecord\System;
7
use Extend\Version;
8
use Ffcms\Core\Arch\Model;
9
use Ffcms\Core\Helper\Date;
10
use Ffcms\Core\Helper\FileSystem\File;
11
use Ffcms\Core\Helper\Type\Obj;
12
13
/**
14
 * Class EntityUpdate. Update entity business logic - update checks & statistic collection
15
 * @package Apps\Model\Admin\Main
16
 */
17
class EntityUpdate extends Model
18
{
19
    const API_LATEST_RELEASE = 'https://api.github.com/repos/phpffcms/ffcms/releases/latest';
20
21
    public $scriptVersion;
22
    public $dbVersion;
23
    public $versionsEqual = false;
24
25
    public $lastVersion;
26
    public $lastInfo = [
27
        'name' => null,
28
        'created_at' => null,
29
        'download_url' => null
30
    ];
31
32
    public $haveRemoteNew = false;
33
34
    /**
35
     * Set default model properties
36
     */
37
    public function before()
38
    {
39
        $this->scriptVersion = Version::VERSION;
40
        $this->dbVersion = System::getVar('version')['data'];
41
42
        $this->versionsEqual = (version_compare($this->scriptVersion, $this->dbVersion) === 0);
43
        $this->findLatestVersion();
44
        $this->haveRemoteNew = ($this->lastVersion !== null && version_compare($this->scriptVersion, $this->lastVersion) === -1);
45
    }
46
47
    /**
48
     * Find latest release in github API and get required info
49
     */
50
    private function findLatestVersion()
51
    {
52
        // get remote api with json response
53
        $gitJson = File::getFromUrl(static::API_LATEST_RELEASE);
0 ignored issues
show
Bug introduced by
The method getFromUrl() does not seem to exist on object<Ffcms\Core\Helper\FileSystem\File>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
54
        if ($gitJson === null || $gitJson === false) {
55
            return;
56
        }
57
58
        // parse api response to model attributes
59
        $git = json_decode($gitJson, true);
60
        $this->lastVersion = $git['tag_name'];
61
        // get download url to full compiled distributive (uploaded to each release as .zip archive, placed in release.assets)
62
        $download = null;
63
        if (Obj::isArray($git['assets'])) {
64
            foreach ($git['assets'] as $asset) {
65
                if ($asset['content_type'] === 'application/zip' && $asset['state'] === 'uploaded') {
66
                    $download = $asset['browser_download_url'];
67
                }
68
            }
69
        }
70
        $this->lastInfo = [
71
            'name' => $git['name'],
72
            'created_at' => Date::convertToDatetime($git['published_at'], Date::FORMAT_TO_HOUR),
73
            'download_url' => $download
74
        ];
75
    }
76
}