Migration::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Vados\MigrationRunner\migration;
4
5
use Phalcon\Db\AdapterInterface;
6
use Phalcon\Di;
7
8
/**
9
 * Class Migration
10
 * @package Vados\MigrationRunner\migration
11
 */
12
abstract class Migration
13
{
14
    /**
15
     * @var AdapterInterface
16
     */
17
    private $dbInstance;
18
19
    /**
20
     * Migration constructor.
21
     */
22
    public function __construct()
23
    {
24
        $this->dbInstance = Di::getDefault()->getShared('db');
25
    }
26
27
    /**
28
     * @param string $action
29
     * @return bool
30
     */
31
    public function safeRun(string $action): bool
32
    {
33
        $this->dbInstance->begin();
34
        try {
35
            switch ($action) {
36
                default:
37
                case 'up':
38
                    $result = $this->up();
39
                    break;
40
                case 'down':
41
                    $result = $this->down();
42
                    break;
43
            }
44
            $result ? $this->dbInstance->commit() : $this->dbInstance->rollback();
45
            return $result;
46
        } catch (\Exception $e) {
47
            echo $e->getMessage() . PHP_EOL;
48
            $this->dbInstance->rollback();
49
            return false;
50
        }
51
    }
52
53
    /**
54
     * @return AdapterInterface
55
     */
56
    public function getDbConnection(): AdapterInterface
57
    {
58
        return $this->dbInstance;
59
    }
60
61
    /**
62
     * @return bool
63
     */
64
    abstract public function up(): bool;
65
66
    /**
67
     * @return bool
68
     */
69
    abstract public function down(): bool;
70
}