Completed
Push — master ( 7c2ac0...7f1c12 )
by Dmitry
08:32
created

Tarantool   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 3
c 1
b 0
f 1
lcom 0
cbo 7
dl 0
loc 69
ccs 31
cts 31
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A register() 0 55 3
1
<?php
2
3
namespace Basis\Providers;
4
5
use Basis\Config;
6
use Basis\Filesystem;
7
use League\Container\ServiceProvider\AbstractServiceProvider;
8
use Tarantool\Client as TarantoolClient;
9
use Tarantool\Connection\Connection;
10
use Tarantool\Connection\SocketConnection;
11
use Tarantool\Mapper\Client;
12
use Tarantool\Mapper\Manager;
13
use Tarantool\Mapper\Migrations\Migrator;
14
use Tarantool\Mapper\Schema\Meta;
15
use Tarantool\Mapper\Schema\Schema;
16
use Tarantool\Packer\Packer;
17
use Tarantool\Packer\PurePacker;
18
19
20
class Tarantool extends AbstractServiceProvider
21
{
22
    protected $provides = [
23
        Client::class,
24
        Connection::class,
25
        Manager::class,
26
        Migrator::class,
27
        Meta::class,
28
        Packer::class,
29
        Schema::class,
30
        TarantoolClient::class,
31
    ];
32
33 2
    public function register()
34
    {
35
        $this->getContainer()->share(Client::class, function () {
36 1
            return new Client(
37 1
                $this->getContainer()->get(Connection::class),
38 1
                $this->getContainer()->get(Packer::class)
39
            );
40 2
        });
41
42
        $this->getContainer()->share(Connection::class, function () {
43 1
            $config = $this->getContainer()->get(Config::class);
44 1
            return new SocketConnection(
45 1
                $config['tarantool.host'],
46 1
                $config['tarantool.port']
47
            );
48 2
        });
49
50
        $this->getContainer()->share(Meta::class, function () {
51
            return $this->getContainer()->get(Manager::class)->getMeta();
52 2
        });
53
54
        $this->getContainer()->share(Manager::class, function () {
55 1
            return new Manager(
56 1
                $this->getContainer()->get(Client::class)
57
            );
58 2
        });
59
60
        $this->container->share(Migrator::class, function() {
61 1
            $migrator = new Migrator();
62 1
            $fs = $this->container->get(Filesystem::class);
63 1
            foreach($fs->listFiles('resources/migrations') as $path) {
64 1
                list($ym, $filename) = explode('/', $path);
65 1
                $namespace = date_create_from_format('Ym', $ym)->format('FY');
66 1
                $class = $namespace.'\\'.substr($filename, 0, -4);
67 1
                if(!class_exists($class, false)) {
68 1
                    include $fs->getPath('resources/migrations/'.$path);
69
                }
70 1
                $migrator->registerMigration($class);
71
            }
72 1
            return $migrator;
73 2
        });
74
75
        $this->getContainer()->share(Packer::class, function () {
76 1
            return new PurePacker();
77 2
        });
78
79
        $this->getContainer()->share(Schema::class, function () {
80
            return $this->getContainer()->get(Manager::class)->getSchema();
81 2
        });
82
83 2
        $this->getContainer()->share(TarantoolClient::class, function() {
84
            return $this->getContainer()->get(Client::class);
85 2
        });
86
87
    }
88
}