Completed
Pull Request — master (#1)
by Joao
04:35
created

AbstractCommand::getDbDriver()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace ByJG\DbMigration\Commands;
4
5
use ByJG\AnyDataset\DbDriverInterface;
6
7
abstract class AbstractCommand implements CommandInterface
8
{
9
    /**
10
     * @var DbDriverInterface
11
     */
12
    private $dbDriver;
13
14
    /**
15
     * Command constructor.
16
     *
17
     * @param DbDriverInterface $dbDriver
18
     */
19
    public function __construct(DbDriverInterface $dbDriver)
20
    {
21
        $this->dbDriver = $dbDriver;
22
    }
23
24
    /**
25
     * @return DbDriverInterface
26
     */
27
    public function getDbDriver()
28
    {
29
        return $this->dbDriver;
30
    }
31
32
    public function getVersion()
33
    {
34
        try {
35
            return $this->getDbDriver()->getScalar('SELECT version FROM migration_version');
36
        } catch (\Exception $ex) {
37
            throw new \Exception('This database does not have a migration version. Please use "migrate reset" to create one.');
38
        }
39
    }
40
41
    public function setVersion($version)
42
    {
43
        $this->getDbDriver()->execute('UPDATE migration_version SET version = :version', ['version' => $version]);
44
    }
45
46
    protected function checkExistsVersion()
47
    {
48
        // Get the version to check if exists
49
        $version = $this->getVersion();
50
        if (empty($version)) {
51
            $this->getDbDriver()->execute('insert into migration_version values(0)');
52
        }
53
    }
54
}
55