Passed
Push — master ( 96c45d...724434 )
by Mr
07:33
created

FlysystemMigrationLoader   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
dl 0
loc 36
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the daikon-cqrs/flysystem-adapter project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Daikon\Flysystem\Migration;
10
11
use Daikon\Dbal\Migration\MigrationList;
12
use Daikon\Dbal\Migration\MigrationLoaderInterface;
13
use Daikon\Flysystem\Connector\FlysystemConnector;
14
use League\Flysystem\MountManager;
15
use Psr\Container\ContainerInterface;
16
17
final class FlysystemMigrationLoader implements MigrationLoaderInterface
18
{
19
    private ContainerInterface $container;
20
21
    private FlysystemConnector $connector;
22
23
    private array $settings;
24
25
    public function __construct(ContainerInterface $container, FlysystemConnector $connector, array $settings = [])
26
    {
27
        $this->container = $container;
28
        $this->connector = $connector;
29
        $this->settings = $settings;
30
    }
31
32
    public function load(): MigrationList
33
    {
34
        /** @var MountManager $filesystem */
35
        $filesystem = $this->connector->getConnection();
36
        $contents = $filesystem->listContents($this->settings['location'], true);
37
        $migrationFiles = array_filter(
38
            $contents,
39
            fn(array $fileinfo): bool => isset($fileinfo['extension']) && $fileinfo['extension'] === 'php'
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected ':', expecting T_DOUBLE_ARROW on line 39 at column 31
Loading history...
40
        );
41
42
        $migrations = [];
43
        foreach ($migrationFiles as $migrationFile) {
44
            // @todo better way to include migration classes
45
            $declaredClasses = get_declared_classes();
46
            require_once $this->getBaseDir().'/'.$migrationFile['path'];
47
            $migrationClass = current(array_diff(get_declared_classes(), $declaredClasses));
48
            $migrations[] = $this->container->get($migrationClass);
49
        }
50
51
        return new MigrationList($migrations);
52
    }
53
54
    private function getBaseDir(): string
55
    {
56
        $connectorSettings = $this->connector->getSettings();
57
        return $connectorSettings['mounts']['migration']['location'];
58
    }
59
}
60