Completed
Push — master ( 331eae...62f73e )
by Dmitry
03:50
created

TarantoolProvider   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Test Coverage

Coverage 60.38%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
c 2
b 0
f 0
lcom 0
cbo 7
dl 0
loc 100
ccs 32
cts 53
cp 0.6038
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B register() 0 84 5
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\EvaluateRequest;
16
use Tarantool\Client\Request\InsertRequest;
17
use Tarantool\Client\Request\ReplaceRequest;
18
use Tarantool\Client\Request\UpdateRequest;
19
use Tarantool\Client\Request\UpsertRequest;
20
use Tarantool\Mapper\Bootsrap;
21
use Tarantool\Mapper\Client;
22
use Tarantool\Mapper\Mapper;
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\Pool;
28
use Tarantool\Mapper\Schema;
29
30
class TarantoolProvider extends AbstractServiceProvider
31
{
32
    protected $provides = [
33
        Bootsrap::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 3
    public function register()
46
    {
47
        $this->container->share(Bootsrap::class, function () {
48
            return $this->container->get(Mapper::class)->getBootstrap();
49 3
        });
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
            $name = $this->getContainer()->get(Service::class)->getName();
58
            $pool->register($name, $mapper);
59
60
            foreach ($service->listServices() as $service) {
0 ignored issues
show
Bug introduced by
The variable $service seems to be defined by a foreach iteration on line 60. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
61
                if ($service != $name) {
62
                    $pool->register($service, function () use ($service) {
63
                        $connection = new StreamConnection('tcp://'.$service.'-db:3301');
64
                        $packer = new PurePacker();
65
                        $client = new Client($connection, $packer);
66
                        $client->disableRequest(DeleteRequest::class);
67
                        $client->disableRequest(EvaluateRequest::class);
68
                        $client->disableRequest(InsertRequest::class);
69
                        $client->disableRequest(ReplaceRequest::class);
70
                        $client->disableRequest(UpdateRequest::class);
71
                        $client->disableRequest(UpsertRequest::class);
72
                        return new Mapper($client);
73
                    });
74
                }
75
            }
76 3
        });
77
78
        $this->getContainer()->share(Client::class, function () {
79 3
            return new Client(
80 3
                $this->getContainer()->get(Connection::class),
81 3
                $this->getContainer()->get(Packer::class)
82
            );
83 3
        });
84
85
        $this->getContainer()->share(Connection::class, function () {
86 3
            return $this->getContainer()->get(StreamConnection::class);
87 3
        });
88
89
        $this->getContainer()->share(Mapper::class, function () {
90 3
            $mapper = new Mapper($this->getContainer()->get(Client::class));
91
92 3
            $annotation = $mapper->addPlugin(Annotation::class);
93
94 3
            $filesystem = $this->getContainer()->get(Filesystem::class);
95 3
            foreach ($filesystem->listClasses('Entity') as $class) {
96 3
                $annotation->register($class);
97
            }
98 3
            foreach ($filesystem->listClasses('Repository') as $class) {
99 3
                $annotation->register($class);
100
            }
101
102 3
            $mapper->addPlugin(NestedSet::class);
103 3
            $mapper->addPlugin(Sequence::class);
104 3
            $mapper->addPlugin(Spy::class);
105 3
            return $mapper;
106 3
        });
107
108
        $this->getContainer()->share(Spy::class, function () {
109 1
            return $this->getContainer()->get(Mapper::class)->getPlugin(Spy::class);
110 3
        });
111
112
        $this->getContainer()->share(Packer::class, function () {
113 3
            return new PurePacker();
114 3
        });
115
116
        $this->getContainer()->share(Schema::class, function () {
117
            return $this->getContainer()->get(Mapper::class)->getSchema();
118 3
        });
119
120
        $this->getContainer()->share(StreamConnection::class, function () {
121 3
            $config = $this->getContainer()->get(Config::class);
122 3
            return new StreamConnection($config['tarantool']);
123 3
        });
124
125 3
        $this->getContainer()->share(TarantoolClient::class, function () {
126
            return $this->getContainer()->get(Client::class);
127 3
        });
128 3
    }
129
}
130