Completed
Push — master ( d7f80a...ef11a8 )
by Dmitry
03:55
created

TarantoolProvider   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Test Coverage

Coverage 64.91%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 2
dl 0
loc 113
ccs 37
cts 57
cp 0.6491
rs 10
c 3
b 1
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$0 ➔ afterInstantiate() 0 4 1
B register() 0 97 1
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\Bootstrap;
19
use Tarantool\Mapper\Client;
20
use Tarantool\Mapper\Entity;
21
use Tarantool\Mapper\Mapper;
22
use Tarantool\Mapper\Plugin\Annotation;
23
use Tarantool\Mapper\Plugin\NestedSet;
24
use Tarantool\Mapper\Plugin\Sequence;
25
use Tarantool\Mapper\Plugin\Spy;
26
use Tarantool\Mapper\Plugin;
27
use Tarantool\Mapper\Pool;
28
use Tarantool\Mapper\Schema;
29
30
class TarantoolProvider extends AbstractServiceProvider
31
{
32
    protected $provides = [
33
        Bootstrap::class,
34
        Client::class,
35
        Connection::class,
36
        Mapper::class,
37
        Packer::class,
38
        Pool::class,
39
        Schema::class,
40
        Spy::class,
41
        StreamConnection::class,
42
        TarantoolClient::class,
43
    ];
44
45 4
    public function register()
46
    {
47
        $this->container->share(Bootstrap::class, function () {
48 2
            return $this->container->get(Mapper::class)->getBootstrap();
49 4
        });
50
51
        $this->getContainer()->share(Pool::class, function () {
52
            $mapper = $this->getContainer()->get(Mapper::class);
53
54
            $pool = new Pool();
55
            $pool->register('default', $mapper);
56
57
            $service = $this->getContainer()->get(Service::class);
58
59
            $local = $service->getName();
60
            $pool->register($local, $mapper);
61
62
            foreach ($service->listServices() as $remote) {
63
                if ($remote != $local) {
64
                    $pool->register($remote, function () use ($remote) {
65
                        $connection = new StreamConnection('tcp://'.$remote.'-db:3301');
66
                        $packer = new PurePacker();
67
                        $client = new Client($connection, $packer);
68
                        $client->disableRequest(DeleteRequest::class);
69
                        $client->disableRequest(InsertRequest::class);
70
                        $client->disableRequest(ReplaceRequest::class);
71
                        $client->disableRequest(UpdateRequest::class);
72
                        return new Mapper($client);
73
                    });
74
                }
75
            }
76
77
            return $pool;
78 4
        });
79
80
        $this->getContainer()->share(Client::class, function () {
81 4
            return new Client(
82 4
                $this->getContainer()->get(Connection::class),
83 4
                $this->getContainer()->get(Packer::class)
84
            );
85 4
        });
86
87
        $this->getContainer()->share(Connection::class, function () {
88 4
            return $this->getContainer()->get(StreamConnection::class);
89 4
        });
90
91
        $this->getContainer()->share(Mapper::class, function () {
92 4
            $mapper = new Mapper($this->getContainer()->get(Client::class));
93
94 4
            $annotation = $mapper->addPlugin(Annotation::class);
95
96 4
            $filesystem = $this->getContainer()->get(Filesystem::class);
97 4
            foreach ($filesystem->listClasses('Entity') as $class) {
98 4
                $annotation->register($class);
99
            }
100 4
            foreach ($filesystem->listClasses('Repository') as $class) {
101 4
                $annotation->register($class);
102
            }
103
104
105 4
            $mapper->addPlugin(NestedSet::class);
106 4
            $mapper->addPlugin(Sequence::class);
107 4
            $mapper->addPlugin(Spy::class);
108
109 4
            $mapper->application = $this->getContainer();
0 ignored issues
show
Bug introduced by
The property application does not seem to exist in Tarantool\Mapper\Mapper.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
110
111
            $mapper->addPlugin(new class($mapper) extends Plugin {
112 2
                public function afterInstantiate(Entity $entity)
113
                {
114 2
                    $entity->app = $this->mapper->application;
0 ignored issues
show
Bug introduced by
The property app does not seem to exist in Tarantool\Mapper\Entity.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Bug introduced by
The property application does not seem to exist in Tarantool\Mapper\Mapper.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
115 2
                }
116
            });
117
118 4
            return $mapper;
119 4
        });
120
121
        $this->getContainer()->share(Spy::class, function () {
122 1
            return $this->getContainer()->get(Mapper::class)->getPlugin(Spy::class);
123 4
        });
124
125
        $this->getContainer()->share(Packer::class, function () {
126 4
            return new PurePacker();
127 4
        });
128
129
        $this->getContainer()->share(Schema::class, function () {
130
            return $this->getContainer()->get(Mapper::class)->getSchema();
131 4
        });
132
133
        $this->getContainer()->share(StreamConnection::class, function () {
134 4
            $config = $this->getContainer()->get(Config::class);
135 4
            return new StreamConnection($config['tarantool']);
136 4
        });
137
138 4
        $this->getContainer()->share(TarantoolClient::class, function () {
139
            return $this->getContainer()->get(Client::class);
140 4
        });
141 4
    }
142
}
143