RepositoryProvider   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 0
cbo 7
dl 0
loc 43
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B register() 0 31 3
1
<?php
2
3
/*
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the MIT license. For more information, see
18
 * <https://github.com/baleen/migrations>.
19
 */
20
21
namespace Baleen\Cli\Provider;
22
23
use Baleen\Cli\Config\Config;
24
use Baleen\Cli\Exception\CliException;
25
use Baleen\Migrations\Migration\Factory\FactoryInterface;
26
use Baleen\Migrations\Migration\Factory\SimpleFactory;
27
use Baleen\Migrations\Repository\DirectoryRepository;
28
use League\Container\ServiceProvider;
29
use League\Flysystem\Adapter\Local;
30
use League\Flysystem\Filesystem;
31
32
/**
33
 * Class RepositoryProvider.
34
 *
35
 * @author Gabriel Somoza <[email protected]>
36
 */
37
class RepositoryProvider extends ServiceProvider
38
{
39
    protected $provides = [
40
        Services::REPOSITORY,
41
        Services::REPOSITORY_FILESYSTEM,
42
        Services::MIGRATION_FACTORY,
43
    ];
44
45
    /**
46
     * @inheritdoc
47
     */
48
    public function register()
49
    {
50
        $container = $this->getContainer();
51
52
        $container->singleton(Services::MIGRATION_FACTORY, SimpleFactory::class);
53
54
        $container->singleton(Services::REPOSITORY_FILESYSTEM, function (Config $appConfig) {
55
            $adapter = new Local(dirname($appConfig->getConfigFilePath()));
56
57
            return new Filesystem($adapter);
58
        })->withArgument(Services::CONFIG);
59
60
        $container->singleton(Services::REPOSITORY, function (Config $config, FactoryInterface $migrationFactory) {
61
            $migrationsDir = $config->getMigrationsDirectoryPath();
62
            if (!is_dir($migrationsDir)) {
63
                $result = mkdir($migrationsDir, 0777, true);
64
                if (!$result) {
65
                    throw new CliException(sprintf(
66
                        'Could not create directory "$s.',
67
                        $migrationsDir
68
                    ));
69
                }
70
            }
71
            // make sure classes in the migration directory are autoloaded
72
            /** @var \Composer\Autoload\ClassLoader $autoloader */
73
            $autoloader = $this->getContainer()->get(Services::AUTOLOADER);
74
            $autoloader->addPsr4($config->getMigrationsNamespace().'\\', $migrationsDir);
75
76
            return new DirectoryRepository($migrationsDir, $migrationFactory);
77
        })->withArguments([Services::CONFIG, Services::MIGRATION_FACTORY]);
78
    }
79
}
80