Completed
Push — master ( 4848aa...64d9da )
by Dmitry
03:44
created

PoolProvider::register()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 35
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 35
ccs 0
cts 27
cp 0
rs 8.8571
c 1
b 0
f 0
cc 2
eloc 25
nc 1
nop 0
crap 6
1
<?php
2
3
namespace Basis\Provider;
4
5
use Basis\Service;
6
use League\Container\ServiceProvider\AbstractServiceProvider;
7
use Tarantool\Client\Connection\StreamConnection;
8
use Tarantool\Client\Packer\PurePacker;
9
use Tarantool\Client\Request\DeleteRequest;
10
use Tarantool\Client\Request\InsertRequest;
11
use Tarantool\Client\Request\ReplaceRequest;
12
use Tarantool\Client\Request\UpdateRequest;
13
use Tarantool\Mapper\Client;
14
use Tarantool\Mapper\Mapper;
15
use Tarantool\Mapper\Plugin;
16
use Tarantool\Mapper\Plugin\Sequence;
17
use Tarantool\Mapper\Plugin\Spy;
18
use Tarantool\Mapper\Plugin\Temporal;
19
use Tarantool\Mapper\Pool;
20
use Tarantool\Mapper\Schema;
21
22
class PoolProvider extends AbstractServiceProvider
23
{
24
    protected $provides = [
25
        Pool::class,
26
    ];
27
28
    public function register()
29
    {
30
        $this->getContainer()->share(Pool::class, function () {
31
            $container = $this->getContainer();
32
33
            $mapper = $container->get(Mapper::class);
34
35
            $service = $container->get(Service::class);
36
            $mapper->serviceName = $service->getName();
37
38
            $pool = new Pool();
39
            $pool->register('default', $mapper);
40
            $pool->register($mapper->serviceName, $mapper);
41
42
            $pool->registerResolver(function ($name) use ($service) {
43
                if (in_array($name, $service->listServices())) {
44
                    $connection = new StreamConnection('tcp://'.$name.'-db:3301');
45
                    $packer = new PurePacker();
46
                    $client = new Client($connection, $packer);
47
                    $client->disableRequest(DeleteRequest::class);
48
                    $client->disableRequest(InsertRequest::class);
49
                    $client->disableRequest(ReplaceRequest::class);
50
                    $client->disableRequest(UpdateRequest::class);
51
                    $mapper = new Mapper($client);
52
                    $mapper->getPlugin(Sequence::class);
53
                    $mapper->getPlugin(Spy::class);
54
                    $mapper->getPlugin(Temporal::class);
55
                    $mapper->serviceName = $name;
0 ignored issues
show
Bug introduced by
The property serviceName 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...
56
                    return $mapper;
57
                }
58
            });
59
60
            return $pool;
61
        });
62
    }
63
}
64