AbstractMigration::up()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
nc 1
1
<?php declare(strict_types=1);
2
/**
3
 * Starlit Db.
4
 *
5
 * @copyright Copyright (c) 2019 Starweb AB
6
 * @license   BSD 3-Clause
7
 */
8
9
namespace Starlit\Db\Migration;
10
11
use Starlit\Db\Db;
12
13
/**
14
 * Concrete migration class names must contain a sequential migration number, eg. "Migration1".
15
 *
16
 * Names can also include something more meaningful, eg. "Migration14CreateAuthorsTable".
17
 *
18
 * @author Andreas Nilsson <http://github.com/jandreasn>
19
 */
20
abstract class AbstractMigration
21
{
22
    /**
23
     * @var Db;
24
     */
25
    protected $db;
26
27
    /**
28
     * @param Db $db
29
     */
30 12
    final public function __construct(Db $db)
31
    {
32 12
        $this->db = $db;
33 12
    }
34
35 9
    public function getNumber(): int
36
    {
37 9
        if (!preg_match('/\d+/', get_class($this), $matches) ||  !($number = (int) $matches[0])) {
38 1
            throw new \LogicException("Invalid migration class name (must include a migration number)");
39
        }
40
41 8
        return $number;
42
    }
43
44
    /**
45
     * Actions to be performed when migrating up to this version (e.g. 6.1.0 -> 6.2.0)
46
     */
47
    abstract public function up(): void;
48
49
    /**
50
     * Actions to be performed when migrating down from this version (e.g. 6.2.0 -> 6.0.0)
51
     */
52 2
    public function down(): void
53
    {
54 2
    }
55
}
56