Passed
Push — master ( 855535...fabb06 )
by Peter
02:32
created

FileFinderBootstrapper::getBindings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\Bootstrappers\Filesystem;
6
7
use AbterPhp\Framework\Constant\Env;
8
use AbterPhp\Framework\Filesystem\FileFinder;
9
use AbterPhp\Framework\Filesystem\IFileFinder;
10
use AbterPhp\Framework\Module\Manager; // @phan-suppress-current-line PhanUnreferencedUseNormal
11
use League\Flysystem\Adapter\Local;
12
use League\Flysystem\Filesystem;
13
use Opulence\Databases\Adapters\Pdo\MySql\Driver as MySqlDriver;
14
use Opulence\Databases\Adapters\Pdo\PostgreSql\Driver as PostgreSqlDriver;
15
use Opulence\Environments\Environment;
16
use Opulence\Ioc\Bootstrappers\Bootstrapper;
17
use Opulence\Ioc\Bootstrappers\ILazyBootstrapper;
18
use Opulence\Ioc\IContainer;
19
20
class FileFinderBootstrapper extends Bootstrapper implements ILazyBootstrapper
21
{
22
    const MIGRATION_FILE_FINDER = 'MigrationFileFinder';
23
24
    const MIGRATIONS_PATH_SEGMENT = 'migrations';
25
26
    /** @var string|null */
27
    protected $dbDriverName;
28
29
    /**
30
     * @return array
31
     */
32
    public function getBindings(): array
33
    {
34
        return [
35
            IFileFinder::class,
36
            static::MIGRATION_FILE_FINDER,
37
        ];
38
    }
39
40
    /**
41
     * @param IContainer $container
42
     *
43
     * @throws \Opulence\Ioc\IocException
44
     */
45
    public function registerBindings(IContainer $container)
46
    {
47
        $this->registerDefaultFileFinder($container);
48
        $this->registerMigrationFileFinder($container);
49
    }
50
51
    /**
52
     * @param IContainer $container
53
     */
54
    protected function registerDefaultFileFinder(IContainer $container)
55
    {
56
        /** @var IFileFinder $fileFinder */
57
        $fileFinder = new FileFinder();
58
59
        $container->bindInstance(IFileFinder::class, $fileFinder);
60
    }
61
62
    /**
63
     * @param IContainer $container
64
     */
65
    protected function registerMigrationFileFinder(IContainer $container)
66
    {
67
        /** @var Manager $abterModuleManager */
68
        global $abterModuleManager;
69
70
        $dbDriver   = $this->getDbDriver();
71
        $fileFinder = new FileFinder();
72
        foreach ($abterModuleManager->getResourcePaths() as $resourcePath) {
73
            $path    = $this->getMigrationsPath($resourcePath, $dbDriver);
74
            $adapter = new Local($path);
75
            $fs      = new Filesystem($adapter);
76
77
            $fileFinder->registerFilesystem($fs);
78
        }
79
80
        $container->bindInstance(static::MIGRATION_FILE_FINDER, $fileFinder);
81
    }
82
83
    /**
84
     * @param string $resourcePath
85
     * @param string $dbDriver
86
     *
87
     * @return string
88
     */
89
    protected function getMigrationsPath(string $resourcePath, string $dbDriver): string
90
    {
91
        return sprintf(
92
            '%s%s%s%s%s',
93
            $resourcePath,
94
            DIRECTORY_SEPARATOR,
95
            static::MIGRATIONS_PATH_SEGMENT,
96
            DIRECTORY_SEPARATOR,
97
            $dbDriver
98
        );
99
    }
100
101
    /**
102
     * @return string
103
     */
104
    protected function getDbDriver(): string
105
    {
106
        if ($this->dbDriverName !== null) {
107
            return $this->dbDriverName;
108
        }
109
110
        $driverClass = Environment::getVar(Env::DB_DRIVER) ?: PostgreSqlDriver::class;
111
112
        switch ($driverClass) {
113
            case MySqlDriver::class:
114
                $this->dbDriverName = 'mysql';
115
                break;
116
            case PostgreSqlDriver::class:
117
                $this->dbDriverName = 'pgsql';
118
                break;
119
            default:
120
                throw new \RuntimeException(
121
                    "Invalid database driver type specified in environment var \"DB_DRIVER\": $driverClass"
122
                );
123
        }
124
125
        return $this->dbDriverName;
126
    }
127
}
128