Completed
Push — master ( 62f73e...29f67a )
by Dmitry
03:35
created

TarantoolProvider   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Test Coverage

Coverage 100%

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B register() 0 86 5
1
<?php
2
3
namespace Basis\Provider;
4
5
use Basis\Config;
6
use Basis\Filesystem;
7
use Basis\Service;
8
use League\Container\ServiceProvider\AbstractServiceProvider;
9
use Tarantool\Client\Client as TarantoolClient;
10
use Tarantool\Client\Connection\Connection;
11
use Tarantool\Client\Connection\StreamConnection;
12
use Tarantool\Client\Packer\Packer;
13
use Tarantool\Client\Packer\PurePacker;
14
use Tarantool\Client\Request\DeleteRequest;
15
use Tarantool\Client\Request\InsertRequest;
16
use Tarantool\Client\Request\ReplaceRequest;
17
use Tarantool\Client\Request\UpdateRequest;
18
use Tarantool\Mapper\Bootsrap;
19
use Tarantool\Mapper\Client;
20
use Tarantool\Mapper\Mapper;
21
use Tarantool\Mapper\Plugin\Annotation;
22
use Tarantool\Mapper\Plugin\NestedSet;
23
use Tarantool\Mapper\Plugin\Sequence;
24
use Tarantool\Mapper\Plugin\Spy;
25
use Tarantool\Mapper\Pool;
26
use Tarantool\Mapper\Schema;
27
28
class TarantoolProvider extends AbstractServiceProvider
29
{
30
    protected $provides = [
31
        Bootsrap::class,
32
        Client::class,
33
        Connection::class,
34
        Mapper::class,
35
        Packer::class,
36
        Pool::class,
37
        Schema::class,
38
        Spy::class,
39
        StreamConnection::class,
40
        TarantoolClient::class,
41
    ];
42
43 3
    public function register()
44
    {
45
        $this->container->share(Bootsrap::class, function () {
46
            return $this->container->get(Mapper::class)->getBootstrap();
47 3
        });
48
49
        $this->getContainer()->share(Pool::class, function () {
50
            $mapper = $this->getContainer()->get(Mapper::class);
51
52
            $pool = new Pool();
53
            $pool->register('default', $mapper);
54
55
            $service = $this->getContainer()->get(Service::class);
56
57
            $local = $service->getName();
58
            $pool->register($local, $mapper);
59
60
            foreach ($service->listServices() as $remote) {
61
                if ($remote != $local) {
62
                    $pool->register($remote, function () use ($remote) {
63
                        $connection = new StreamConnection('tcp://'.$remote.'-db:3301');
64
                        $packer = new PurePacker();
65
                        $client = new Client($connection, $packer);
66
                        $client->disableRequest(DeleteRequest::class);
67
                        $client->disableRequest(InsertRequest::class);
68
                        $client->disableRequest(ReplaceRequest::class);
69
                        $client->disableRequest(UpdateRequest::class);
70
                        return new Mapper($client);
71
                    });
72
                }
73
            }
74
75
            return $pool;
76 3
        });
77
78
        $this->getContainer()->share(Client::class, function () {
79 3
            return new Client(
80 3
                $this->getContainer()->get(Connection::class),
81 3
                $this->getContainer()->get(Packer::class)
82
            );
83 3
        });
84
85
        $this->getContainer()->share(Connection::class, function () {
86 3
            return $this->getContainer()->get(StreamConnection::class);
87 3
        });
88
89
        $this->getContainer()->share(Mapper::class, function () {
90 3
            $mapper = new Mapper($this->getContainer()->get(Client::class));
91
92 3
            $annotation = $mapper->addPlugin(Annotation::class);
93
94 3
            $filesystem = $this->getContainer()->get(Filesystem::class);
95 3
            foreach ($filesystem->listClasses('Entity') as $class) {
96 3
                $annotation->register($class);
97
            }
98 3
            foreach ($filesystem->listClasses('Repository') as $class) {
99 3
                $annotation->register($class);
100
            }
101
102 3
            $mapper->addPlugin(NestedSet::class);
103 3
            $mapper->addPlugin(Sequence::class);
104 3
            $mapper->addPlugin(Spy::class);
105 3
            return $mapper;
106 3
        });
107
108
        $this->getContainer()->share(Spy::class, function () {
109 1
            return $this->getContainer()->get(Mapper::class)->getPlugin(Spy::class);
110 3
        });
111
112
        $this->getContainer()->share(Packer::class, function () {
113 3
            return new PurePacker();
114 3
        });
115
116
        $this->getContainer()->share(Schema::class, function () {
117
            return $this->getContainer()->get(Mapper::class)->getSchema();
118 3
        });
119
120
        $this->getContainer()->share(StreamConnection::class, function () {
121 3
            $config = $this->getContainer()->get(Config::class);
122 3
            return new StreamConnection($config['tarantool']);
123 3
        });
124
125 3
        $this->getContainer()->share(TarantoolClient::class, function () {
126
            return $this->getContainer()->get(Client::class);
127 3
        });
128 3
    }
129
}
130