Passed
Push — master ( b5fd53...298c35 )
by Peter
02:38
created

BaseMigration   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 63
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 3 1
A __construct() 0 5 1
A up() 0 5 1
A down() 0 5 1
A getCreationDate() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Databases\Migrations;
6
7
use AbterPhp\Framework\Filesystem\FileFinder;
8
use DateTime;
9
use Opulence\Databases\IConnection;
10
use Opulence\Databases\Migrations\Migration;
11
12
class BaseMigration extends Migration
13
{
14
    const FILENAME = 'foo-bar';
15
16
    const UP   = 'up';
17
    const DOWN = 'down';
18
19
    /** @var FileFinder */
20
    protected $fileFinder;
21
22
    /**
23
     * Init constructor.
24
     *
25
     * @param IConnection $connection
26
     * @param string      $migrationsPath
27
     * @param string      $driver
28
     */
29
    public function __construct(IConnection $connection, FileFinder $fileFinder)
30
    {
31
        parent::__construct($connection);
32
33
        $this->fileFinder = $fileFinder;
34
    }
35
36
    /**
37
     * Gets the creation date, which is used for ordering
38
     *
39
     * @return DateTime The date this migration was created
40
     */
41
    public static function getCreationDate(): DateTime
42
    {
43
        return new DateTime();
44
    }
45
46
    /**
47
     * Executes the query that rolls back the migration
48
     */
49
    public function down(): void
50
    {
51
        $sql       = $this->load(static::FILENAME, static::DOWN);
52
        $statement = $this->connection->prepare($sql);
53
        $statement->execute();
54
    }
55
56
    /**
57
     * Executes the query that commits the migration
58
     */
59
    public function up(): void
60
    {
61
        $sql       = $this->load(static::FILENAME, static::UP);
62
        $statement = $this->connection->prepare($sql);
63
        $statement->execute();
64
    }
65
66
    /**
67
     * @param string $filename
68
     * @param string $direction
69
     *
70
     * @return string
71
     */
72
    protected function load(string $filename, string $direction): string
73
    {
74
        return $this->fileFinder->read(sprintf('%s/%s', $direction, $filename));
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->fileFinder...$direction, $filename)) could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
75
    }
76
}
77