Completed
Push — master ( ef11a8...39341a )
by Dmitry
04:06
created

TarantoolProvider   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 2
dl 0
loc 118
ccs 39
cts 39
cp 1
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 102 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 4
            $filesystem = $this->getContainer()->get(Filesystem::class);
94
95 4
            $meta = $filesystem->getPath('.cache/mapper-meta.php');
96 4
            if (file_exists($meta)) {
97
                $mapper->setMeta($meta);
98
            }
99
100 4
            $annotation = $mapper->addPlugin(Annotation::class);
101
102 4
            foreach ($filesystem->listClasses('Entity') as $class) {
103 4
                $annotation->register($class);
104
            }
105 4
            foreach ($filesystem->listClasses('Repository') as $class) {
106 4
                $annotation->register($class);
107
            }
108
109
110 4
            $mapper->addPlugin(NestedSet::class);
111 4
            $mapper->addPlugin(Sequence::class);
112 4
            $mapper->addPlugin(Spy::class);
113
114 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...
115
116
            $mapper->addPlugin(new class($mapper) extends Plugin {
117 2
                public function afterInstantiate(Entity $entity)
118
                {
119 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...
120 2
                }
121
            });
122
123 4
            return $mapper;
124 4
        });
125
126
        $this->getContainer()->share(Spy::class, function () {
127 1
            return $this->getContainer()->get(Mapper::class)->getPlugin(Spy::class);
128 4
        });
129
130
        $this->getContainer()->share(Packer::class, function () {
131 4
            return new PurePacker();
132 4
        });
133
134
        $this->getContainer()->share(Schema::class, function () {
135
            return $this->getContainer()->get(Mapper::class)->getSchema();
136 4
        });
137
138
        $this->getContainer()->share(StreamConnection::class, function () {
139 4
            $config = $this->getContainer()->get(Config::class);
140 4
            return new StreamConnection($config['tarantool']);
141 4
        });
142
143 4
        $this->getContainer()->share(TarantoolClient::class, function () {
144
            return $this->getContainer()->get(Client::class);
145 4
        });
146 4
    }
147
}
148