Completed
Push — master ( 944da3...9216c3 )
by Dmitry
06:37
created

TarantoolProvider   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Test Coverage

Coverage 65.33%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 4
c 3
b 1
f 0
dl 0
loc 126
ccs 49
cts 75
cp 0.6533
rs 10

2 Methods

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