Completed
Push — master ( d22295...288421 )
by Dmitry
04:25
created

Tarantool   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 7
dl 0
loc 66
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A register() 0 52 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\Client as OriginalClient;
9
use Tarantool\Client\Connection\Connection;
10
use Tarantool\Client\Connection\StreamConnection;
11
use Tarantool\Client\Packer\Packer;
12
use Tarantool\Client\Packer\PurePacker;
13
use Tarantool\Mapper\Client;
14
use Tarantool\Mapper\Manager;
15
use Tarantool\Mapper\Migrations\Migrator;
16
use Tarantool\Mapper\Schema\Meta;
17
use Tarantool\Mapper\Schema\Schema;
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
        OriginalClient::class,
31
    ];
32
33 3
    public function register()
34
    {
35
        $this->getContainer()->share(Client::class, function () {
36 2
            return new Client(
37 2
                $this->getContainer()->get(Connection::class),
38 2
                $this->getContainer()->get(Packer::class)
39
            );
40 3
        });
41
42
        $this->getContainer()->share(Connection::class, function () {
43 2
            $config = $this->getContainer()->get(Config::class);
44 2
            return new StreamConnection('tcp://'.$config['tarantool.host'].':'.$config['tarantool.port']);
45 3
        });
46
47
        $this->getContainer()->share(Meta::class, function () {
48
            return $this->getContainer()->get(Manager::class)->getMeta();
49 3
        });
50
51
        $this->getContainer()->share(Manager::class, function () {
52 1
            return new Manager(
53 1
                $this->getContainer()->get(Client::class)
54
            );
55 3
        });
56
57
        $this->container->share(Migrator::class, function() {
58 1
            $migrator = new Migrator();
59 1
            $fs = $this->container->get(Filesystem::class);
60 1
            foreach($fs->listFiles('resources/migrations') as $path) {
61 1
                list($ym, $filename) = explode('/', $path);
62 1
                $namespace = date_create_from_format('Ym', $ym)->format('FY');
63 1
                $class = $namespace.'\\'.substr($filename, 0, -4);
64 1
                if(!class_exists($class, false)) {
65 1
                    include $fs->getPath('resources/migrations/'.$path);
66
                }
67 1
                $migrator->registerMigration($class);
68
            }
69 1
            return $migrator;
70 3
        });
71
72
        $this->getContainer()->share(Packer::class, function () {
73 2
            return new PurePacker();
74 3
        });
75
76
        $this->getContainer()->share(Schema::class, function () {
77
            return $this->getContainer()->get(Manager::class)->getSchema();
78 3
        });
79
80 3
        $this->getContainer()->share(OriginalClient::class, function() {
81 1
            return $this->getContainer()->get(Client::class);
82 3
        });
83
84
    }
85
}