Completed
Push — master ( 9d8998...346ad6 )
by Dmitry
03:39
created

TarantoolProvider   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Test Coverage

Coverage 94.52%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 3
dl 0
loc 121
ccs 69
cts 73
cp 0.9452
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 104 2
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 22
        $this->container->share(Bootstrap::class, function () {
49 22
            return $this->container->get(Mapper::class)->getBootstrap();
50 22
        });
51
52 22
        $this->getContainer()->share(Client::class, function () {
53 22
            return new Client(
54 22
                $this->getContainer()->get(Connection::class),
55 22
                $this->getContainer()->get(Packer::class)
56
            );
57 22
        });
58
59 22
        $this->getContainer()->share(Connection::class, function () {
60 22
            return $this->getContainer()->get(StreamConnection::class);
61 22
        });
62
63 22
        $this->getContainer()->share(Mapper::class, function () {
64 22
            $mapper = new Mapper($this->getContainer()->get(Client::class));
65 22
            $filesystem = $this->getContainer()->get(Filesystem::class);
66
67 22
            $mapperCache = $filesystem->getPath('.cache/mapper-meta.php');
68 22
            if (file_exists($mapperCache)) {
69
                $meta = include $mapperCache;
70
                $mapper->setMeta($meta);
71
            }
72
73 22
            $annotation = $mapper->getPlugin(Annotation::class);
74
75 22
            foreach ($filesystem->listClasses('Entity') as $class) {
76 22
                $annotation->register($class);
77
            }
78 22
            foreach ($filesystem->listClasses('Repository') as $class) {
79 22
                $annotation->register($class);
80
            }
81
82 22
            $mapper->getPlugin(Sequence::class);
83 22
            $mapper->getPlugin(Spy::class);
84 22
            $mapper->getPlugin(Temporal::class);
85
86 22
            $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 22
                public function afterInstantiate(Entity $entity)
90
                {
91 22
                    $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 22
                }
93
            });
94
95 22
            return $mapper;
96 22
        });
97
98 22
        $this->getContainer()->share(Packer::class, function () {
99 22
            return new PurePacker();
100 22
        });
101
102 22
        $this->getContainer()->share(Pool::class, function () {
103 2
            $container = $this->getContainer();
104
105 2
            $pool = new Pool();
106 2
            $pool->register('default', $container->get(Mapper::class));
107
108 2
            $service = $container->get(Service::class);
109 2
            $pool->register($service->getName(), $container->get(Mapper::class));
110
111 2
            $pool->registerResolver(function ($name) use ($service) {
112 2
                if (in_array($name, $service->listServices())) {
113 1
                    $connection = new StreamConnection('tcp://'.$name.'-db:3301');
114 1
                    $packer = new PurePacker();
115 1
                    $client = new Client($connection, $packer);
116 1
                    $client->disableRequest(DeleteRequest::class);
117 1
                    $client->disableRequest(InsertRequest::class);
118 1
                    $client->disableRequest(ReplaceRequest::class);
119 1
                    $client->disableRequest(UpdateRequest::class);
120 1
                    $mapper = new Mapper($client);
121 1
                    $mapper->getPlugin(Temporal::class);
122 1
                    return $mapper;
123
                }
124 2
            });
125
126 2
            return $pool;
127 22
        });
128
129 22
        $this->getContainer()->share(Schema::class, function () {
130
            return $this->getContainer()->get(Mapper::class)->getSchema();
131 22
        });
132
133 22
        $this->getContainer()->share(Spy::class, function () {
134 2
            return $this->getContainer()->get(Mapper::class)->getPlugin(Spy::class);
135 22
        });
136
137 22
        $this->getContainer()->share(StreamConnection::class, function () {
138 22
            $config = $this->getContainer()->get(Config::class);
139 22
            return new StreamConnection($config['tarantool.connection'], $config['tarantool.params']);
140 22
        });
141
142 22
        $this->getContainer()->share(TarantoolClient::class, function () {
143 15
            return $this->getContainer()->get(Client::class);
144 22
        });
145
146 22
        $this->getContainer()->share(Temporal::class, function () {
147
            return $this->getContainer()->get(Mapper::class)->getPlugin(Temporal::class);
148 22
        });
149 22
    }
150
}
151