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

FormUpdateDatabase::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
rs 9.4285
1
<?php
2
3
namespace Apps\Model\Admin\Main;
4
5
6
use Apps\ActiveRecord\System;
7
use Ffcms\Core\Arch\Model;
8
use Ffcms\Core\Helper\FileSystem\File;
9
use Ffcms\Core\Helper\Type\Str;
10
11
/**
12
 * Class FormUpdateDatabase. Update database business logic model
13
 * @package Apps\Model\Admin\Main
14
 */
15
class FormUpdateDatabase extends Model
16
{
17
    private $dbVersion;
18
    private $scriptVersion;
19
    public $updateQueries = [];
20
21
    /**
22
     * FormUpdateDatabase constructor. Pass db and script version inside
23
     * @param string $db
24
     * @param string $script
25
     */
26
    public function __construct($db, $script)
27
    {
28
        $this->dbVersion = $db;
29
        $this->scriptVersion = $script;
30
        parent::__construct(true);
31
    }
32
33
    /**
34
     * Find update files with sql queries
35
     */
36
    public function findUpdateFiles()
37
    {
38
        // find all file with update sql queries between $dbVersion<->scriptVersion (dbVer <= x <= scriptVer)
39
        $all = File::listFiles('/Private/Database/Updates/', ['.php'], true);
40
        foreach ($all as $file) {
41
            $file = Str::cleanExtension(basename($file));
42
            // $file="3.0.0-3.0.1" become to $start = 3.0.0,$end=3.0.1
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
43
            list($start,$end) = explode('-', $file);
44
            // true: start <= db & script >= $end
45
            if (version_compare($this->dbVersion, $start) !== 1 &&  version_compare($this->scriptVersion, $end) !== -1) {
46
                $this->updateQueries[] = $file;
47
            }
48
        }
49
        sort($this->updateQueries);
50
    }
51
52
    /**
53
     * Include files with update queries
54
     */
55
    public function make()
56
    {
57
        // run update queries from included files
58
        foreach ($this->updateQueries as $file) {
59
            @include root . '/Private/Database/Updates/' . $file . '.php';
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
60
        }
61
        // update version in db table
62
        $row = System::getVar('version');
63
        $row->data = $this->scriptVersion;
64
        $row->save();
65
    }
66
}