Test Failed
Push — master ( e3c39f...fe570d )
by Mihail
07:20
created

EntityUpdate::before()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Apps\Model\Admin\Main;
4
5
use Apps\ActiveRecord\System;
6
use Extend\Version;
7
use Ffcms\Core\Arch\Model;
8
use Ffcms\Core\Helper\Date;
9
use Ffcms\Core\Helper\FileSystem\File;
10
use Ffcms\Core\Helper\Type\Any;
11
use Ffcms\Core\Helper\Type\Arr;
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
    public static $apiZipTypes = [
21
        'application/zip',
22
        'application/x-zip-compressed'
23
    ];
24
25
    public $scriptVersion;
26
    public $dbVersion;
27
    public $versionsEqual = false;
28
29
    public $lastVersion;
30
    public $lastInfo = [
31
        'name' => null,
32
        'created_at' => null,
33
        'download_url' => null
34
    ];
35
36
    public $haveRemoteNew = false;
37
38
    /**
39
     * Set default model properties
40
     */
41
    public function before()
42
    {
43
        $this->scriptVersion = Version::VERSION;
44
        $this->dbVersion = System::getVar('version')['data'];
45
46
        $this->versionsEqual = (version_compare($this->scriptVersion, $this->dbVersion) === 0);
0 ignored issues
show
Bug introduced by
It seems like $this->dbVersion can also be of type Illuminate\Database\Eloq...uent\Relations\Relation and Illuminate\Database\Eloquent\Relations\Relation; however, parameter $version2 of version_compare() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

46
        $this->versionsEqual = (version_compare($this->scriptVersion, /** @scrutinizer ignore-type */ $this->dbVersion) === 0);
Loading history...
47
        $this->findLatestVersion();
48
        $this->haveRemoteNew = ($this->lastVersion !== null && version_compare($this->scriptVersion, $this->lastVersion) === -1);
49
    }
50
51
    /**
52
     * Find latest release in github API and get required info
53
     */
54
    private function findLatestVersion()
55
    {
56
        // get remote api with json response
57
        $gitJson = File::getFromUrl(static::API_LATEST_RELEASE);
58
        if (!$gitJson) {
59
            return;
60
        }
61
62
        // parse api response to model attributes
63
        $git = json_decode($gitJson, true);
64
        $this->lastVersion = $git['tag_name'];
65
        // get download url to full compiled distributive (uploaded to each release as .zip archive, placed in release.assets)
66
        $download = null;
67
        if (Any::isArray($git['assets'])) {
68
            foreach ($git['assets'] as $asset) {
69
                if (Arr::in($asset['content_type'], static::$apiZipTypes) && $asset['state'] === 'uploaded') {
70
                    $download = $asset['browser_download_url'];
71
                }
72
            }
73
        }
74
        $this->lastInfo = [
75
            'name' => $git['name'],
76
            'created_at' => Date::convertToDatetime($git['published_at'], Date::FORMAT_TO_HOUR),
77
            'download_url' => $download
78
        ];
79
    }
80
}
81