Completed
Push — master ( 772acc...7ea003 )
by Dmitry
11:15
created

TarantoolProvider   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 6
c 3
b 1
f 0
dl 0
loc 137
ccs 0
cts 80
cp 0
rs 10

2 Methods

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