Completed
Push — master ( 7ea003...b9e0e5 )
by Dmitry
03:31
created

TarantoolProvider   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Test Coverage

Coverage 67.09%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 6
c 3
b 1
f 0
dl 0
loc 136
ccs 53
cts 79
cp 0.6709
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$0 ➔ afterInstantiate() 0 4 1
B register() 0 119 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\Bootstrap;
19
use Tarantool\Mapper\Client;
20
use Tarantool\Mapper\Entity;
21
use Tarantool\Mapper\Mapper;
22
use Tarantool\Mapper\Plugin;
23
use Tarantool\Mapper\Plugin\Annotation;
24
use Tarantool\Mapper\Plugin\Sequence;
25
use Tarantool\Mapper\Plugin\Spy;
26
use Tarantool\Mapper\Plugin\Temporal;
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
        Temporal::class,
44
    ];
45
46
    public function register()
47
    {
48 4
        $this->container->share(Bootstrap::class, function () {
49 2
            return $this->container->get(Mapper::class)->getBootstrap();
50 4
        });
51
52 4
        $this->getContainer()->share(Client::class, function () {
53 4
            return new Client(
54 4
                $this->getContainer()->get(Connection::class),
55 4
                $this->getContainer()->get(Packer::class)
56
            );
57 4
        });
58
59 4
        $this->getContainer()->share(Connection::class, function () {
60 4
            return $this->getContainer()->get(StreamConnection::class);
61 4
        });
62
63 4
        $this->getContainer()->share(Mapper::class, function () {
64 4
            $mapper = new Mapper($this->getContainer()->get(Client::class));
65 4
            $filesystem = $this->getContainer()->get(Filesystem::class);
66
67 4
            $mapperCache = $filesystem->getPath('.cache/mapper-meta.php');
68 4
            if (file_exists($mapperCache)) {
69
                $meta = include $mapperCache;
70
                $mapper->setMeta($meta);
71
            }
72
73 4
            $annotation = $mapper->addPlugin(Annotation::class);
74
75 4
            foreach ($filesystem->listClasses('Entity') as $class) {
76 4
                $annotation->register($class);
77
            }
78 4
            foreach ($filesystem->listClasses('Repository') as $class) {
79 4
                $annotation->register($class);
80
            }
81
82 4
            $mapper->addPlugin(Sequence::class);
83 4
            $mapper->addPlugin(Spy::class);
84 4
            $mapper->addPlugin(Temporal::class);
85
86 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...
87
88
            $mapper->addPlugin(new class($mapper) extends Plugin {
89 2
                public function afterInstantiate(Entity $entity)
90
                {
91 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...
92 2
                }
93
            });
94
95 4
            return $mapper;
96 4
        });
97
98 4
        $this->getContainer()->share(Packer::class, function () {
99 4
            return new PurePacker();
100 4
        });
101
102 4
        $this->getContainer()->share(Pool::class, function () {
103
            $mapper = $this->getContainer()->get(Mapper::class);
104
105
            $pool = new Pool();
106
            $pool->register('default', $mapper);
107
108
            $service = $this->getContainer()->get(Service::class);
109
110
            $local = $service->getName();
111
            $pool->register($local, $mapper);
112
113
            foreach ($service->listServices() as $remote) {
114
                if ($remote != $local) {
115
                    $pool->register($remote, function () use ($remote) {
116
                        $connection = new StreamConnection('tcp://'.$remote.'-db:3301');
117
                        $packer = new PurePacker();
118
                        $client = new Client($connection, $packer);
119
                        $client->disableRequest(DeleteRequest::class);
120
                        $client->disableRequest(InsertRequest::class);
121
                        $client->disableRequest(ReplaceRequest::class);
122
                        $client->disableRequest(UpdateRequest::class);
123
                        $mapper = new Mapper($client);
124
                        $mapper->addPlugin(Temporal::class);
125
                        return $mapper;
126
                    });
127
                }
128
            }
129
130
            return $pool;
131 4
        });
132
133 4
        $this->getContainer()->share(Schema::class, function () {
134
            return $this->getContainer()->get(Mapper::class)->getSchema();
135 4
        });
136
137 4
        $this->getContainer()->share(Spy::class, function () {
138 1
            return $this->getContainer()->get(Mapper::class)->getPlugin(Spy::class);
139 4
        });
140
141 4
        $this->getContainer()->share(StreamConnection::class, function () {
142 4
            $config = $this->getContainer()->get(Config::class);
143 4
            $params = [];
144
            $env = [
145 4
                'connect_timeout' => 'TARANTOOL_CONNECT_TIMEOUT',
146
                'socket_timeout' => 'TARANTOOL_SOCKET_TIMEOUT',
147
                'tcp_nodelay' => 'TARANTOOL_TCP_NODELAY',
148
            ];
149 4
            foreach ($env as $param => $name) {
150 4
                if (getenv($name)) {
151 4
                    $params[$param] = getenv($name);
152
                }
153
            }
154 4
            return new StreamConnection($config['tarantool'], $params);
155 4
        });
156
157 4
        $this->getContainer()->share(TarantoolClient::class, function () {
158
            return $this->getContainer()->get(Client::class);
159 4
        });
160
161 4
        $this->getContainer()->share(Temporal::class, function () {
162
            return $this->getContainer()->get(Mapper::class)->getPlugin(Temporal::class);
163 4
        });
164
    }
165
}
166