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

FormUpdateDatabase::make()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Apps\Model\Admin\Main;
4
5
use Apps\ActiveRecord\System;
6
use Ffcms\Core\Arch\Model;
7
use Ffcms\Core\Helper\Type\Str;
8
use Ffcms\Core\Managers\MigrationsManager;
9
10
/**
11
 * Class FormUpdateDatabase. Update database business logic model
12
 * @package Apps\Model\Admin\Main
13
 */
14
class FormUpdateDatabase extends Model
15
{
16
    private $dbVersion;
17
    private $scriptVersion;
18
    public $updateQueries = [];
19
20
    /**
21
     * FormUpdateDatabase constructor. Pass db and script version inside
22
     * @param string $db
23
     * @param string $script
24
     */
25
    public function __construct($db, $script)
26
    {
27
        $this->dbVersion = $db;
28
        $this->scriptVersion = $script;
29
        parent::__construct(true);
30
    }
31
32
    /**
33
     * Find update files with sql queries
34
     */
35
    public function findUpdateFiles()
36
    {
37
        // find all file with update sql queries between $dbVersion<->scriptVersion (dbVer <= x <= scriptVer)
38
        $migrations = new MigrationsManager('/Private/Migrations/Updates/');
39
        $search = $migrations->search('update');
40
        foreach ($search as $file) {
0 ignored issues
show
Bug introduced by
The expression $search of type false is not traversable.
Loading history...
41
            $fullName = Str::cleanExtension(basename($file));
42
            $name = Str::firstIn($fullName, '-');
43
            // get update version number from migration name
44
            list($type, $obj, $version) = explode('_', $name);
45
            $intVersion = (int)Str::replace('.', '', $this->dbVersion);
46
            // if migration version > db version - implement it
47
            if ($version > $intVersion) {
48
                $this->updateQueries[] = $file;
49
            }
50
        }
51
        sort($this->updateQueries);
52
    }
53
54
    /**
55
     * Include files with update queries
56
     */
57
    public function make()
58
    {
59
        // run update queries from migrations
60
        $migration = new MigrationsManager('/Private/Migrations/Updates/');
61
        $migration->makeUp($this->updateQueries);
62
        // update version in db table
63
        $row = System::getVar('version');
64
        $row->data = $this->scriptVersion;
0 ignored issues
show
Bug introduced by
The property data does not seem to exist on Ffcms\Core\Arch\ActiveModel. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
65
        $row->save();
66
    }
67
}
68