Completed
Push — master ( b56897...b1778b )
by Dmitry
06:21
created

TarantoolProvider   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Test Coverage

Coverage 66.67%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 4
c 3
b 1
f 0
dl 0
loc 125
ccs 50
cts 75
cp 0.6667
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$0 ➔ afterInstantiate() 0 4 1
B register() 0 108 3
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 8
        $this->container->share(Bootstrap::class, function () {
49 7
            return $this->container->get(Mapper::class)->getBootstrap();
50 8
        });
51
52 8
        $this->getContainer()->share(Client::class, function () {
53 8
            return new Client(
54 8
                $this->getContainer()->get(Connection::class),
55 8
                $this->getContainer()->get(Packer::class)
56
            );
57 8
        });
58
59 8
        $this->getContainer()->share(Connection::class, function () {
60 8
            return $this->getContainer()->get(StreamConnection::class);
61 8
        });
62
63 8
        $this->getContainer()->share(Mapper::class, function () {
64 8
            $mapper = new Mapper($this->getContainer()->get(Client::class));
65 8
            $filesystem = $this->getContainer()->get(Filesystem::class);
66
67 8
            $mapperCache = $filesystem->getPath('.cache/mapper-meta.php');
68 8
            if (file_exists($mapperCache)) {
69
                $meta = include $mapperCache;
70
                $mapper->setMeta($meta);
71
            }
72
73 8
            $annotation = $mapper->getPlugin(Annotation::class);
74
75 8
            foreach ($filesystem->listClasses('Entity') as $class) {
76 8
                $annotation->register($class);
77
            }
78 8
            foreach ($filesystem->listClasses('Repository') as $class) {
79 8
                $annotation->register($class);
80
            }
81
82 8
            $mapper->getPlugin(Sequence::class);
83 8
            $mapper->getPlugin(Spy::class);
84 8
            $mapper->getPlugin(Temporal::class);
85
86 8
            $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->getPlugin(new class($mapper) extends Plugin {
89 7
                public function afterInstantiate(Entity $entity)
90
                {
91 7
                    $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 7
                }
93
            });
94
95 8
            return $mapper;
96 8
        });
97
98 8
        $this->getContainer()->share(Packer::class, function () {
99 8
            return new PurePacker();
100 8
        });
101
102 8
        $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->getPlugin(Temporal::class);
125
                        return $mapper;
126
                    });
127
                }
128
            }
129
130
            return $pool;
131 8
        });
132
133 8
        $this->getContainer()->share(Schema::class, function () {
134
            return $this->getContainer()->get(Mapper::class)->getSchema();
135 8
        });
136
137 8
        $this->getContainer()->share(Spy::class, function () {
138 1
            return $this->getContainer()->get(Mapper::class)->getPlugin(Spy::class);
139 8
        });
140
141 8
        $this->getContainer()->share(StreamConnection::class, function () {
142 8
            $config = $this->getContainer()->get(Config::class);
143 8
            return new StreamConnection($config['tarantool.connection'], $config['tarantool.params']);
144 8
        });
145
146 8
        $this->getContainer()->share(TarantoolClient::class, function () {
147 6
            return $this->getContainer()->get(Client::class);
148 8
        });
149
150 8
        $this->getContainer()->share(Temporal::class, function () {
151
            return $this->getContainer()->get(Mapper::class)->getPlugin(Temporal::class);
152 8
        });
153 8
    }
154
}
155