Passed
Push — v4.1 ( 588d2a...f00803 )
by Masiukevich
01:49
created

SqlMigrationLoader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
/**
4
 * SQL databases adapters implementation.
5
 *
6
 * @author  Maksim Masiukevich <[email protected]>
7
 * @license MIT
8
 * @license https://opensource.org/licenses/MIT
9
 */
10
11
declare(strict_types = 1);
12
13
namespace ServiceBus\Storage\Sql\Migration;
14
15
use Amp\Promise;
16
use function Amp\call;
17
use function Amp\File\scandir;
18
19
/**
20
 *
21
 */
22
final class SqlMigrationLoader
23
{
24
    /** @var string */
25
    private $directory;
26
27
    /**
28
     * SqlMigrationLoader constructor.
29
     *
30
     * @param string $directory
31
     */
32
    public function __construct(string $directory)
33
    {
34
        $this->directory = $directory;
35
    }
36
37
    /**
38
     * Creating migration objects and sorting versions
39
     *
40
     * @return Promise<array<string, \ServiceBus\Storage\Sql\Migration\Migration>>
41
     */
42
    public function load(): Promise
43
    {
44
        return call(
45
            function(): \Generator
46
            {
47
                $migrations = [];
48
49
                /** @var \SplFileInfo[] $supportedFiles */
50
                $supportedFiles = yield from $this->loadFiles();
51
52
                foreach($supportedFiles as $file)
53
                {
54
                    /**
55
                     * @psalm-suppress UnresolvableInclude
56
                     * @noinspection   PhpIncludeInspection
57
                     */
58
                    include_once (string) $file;
59
60
                    /** @var string $name */
61
                    $name    = $file->getBasename('.' . $file->getExtension());
62
                    $version = (string) \substr($name, 7);
63
64
                    /** @psalm-var class-string<\ServiceBus\Storage\Sql\Migration\Migration> $class */
65
                    $class = \sprintf('\%s', $name);
66
67
                    $migration = new $class;
68
69
                    if(($migration instanceof Migration) === false)
70
                    {
71
                        throw new \RuntimeException(
72
                            \sprintf('Migration must extend `%s` class', Migration::class)
73
                        );
74
                    }
75
76
                    $migrations[$version] = new $class;
77
                }
78
79
                \ksort($migrations);
80
81
                return $migrations;
82
            }
83
        );
84
    }
85
86
    /**
87
     * Getting list of migration files
88
     *
89
     * @return Promise<\SplFileInfo[]>
90
     */
91
    private function loadFiles(): Promise
92
    {
93
        return call(
94
            function(): \Generator
95
            {
96
                /** @var string[] $files */
97
                $files = yield scandir($this->directory);
98
99
                return \array_filter(
100
                    \array_map(
101
                        function(string $fileName): ?\SplFileInfo
102
                        {
103
                            return \strpos($fileName, 'Version') !== false
104
                                ? new \SplFileInfo($this->directory . '/' . $fileName)
105
                                : null;
106
                        },
107
                        $files
108
                    )
109
                );
110
            }
111
        );
112
    }
113
}