Completed
Push — master ( 29f67a...08ea87 )
by Dmitry
03:47
created

TarantoolProvider::register()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 96
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 36
CRAP Score 1.0499

Importance

Changes 3
Bugs 1 Features 0
Metric Value
dl 0
loc 96
ccs 36
cts 57
cp 0.6316
rs 8.3859
c 3
b 1
f 0
cc 1
crap 1.0499
eloc 55
nc 1
nop 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A TarantoolProvider.php$0 ➔ afterInstantiate() 0 3 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Bootsrap;
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
        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
            $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 3
        });
79
80
        $this->getContainer()->share(Client::class, function () {
81 3
            return new Client(
82 3
                $this->getContainer()->get(Connection::class),
83 3
                $this->getContainer()->get(Packer::class)
84
            );
85 3
        });
86
87
        $this->getContainer()->share(Connection::class, function () {
88 3
            return $this->getContainer()->get(StreamConnection::class);
89 3
        });
90
91
        $this->getContainer()->share(Mapper::class, function () {
92 3
            $mapper = new Mapper($this->getContainer()->get(Client::class));
93
94 3
            $annotation = $mapper->addPlugin(Annotation::class);
95
96 3
            $filesystem = $this->getContainer()->get(Filesystem::class);
97 3
            foreach ($filesystem->listClasses('Entity') as $class) {
98 3
                $annotation->register($class);
99
            }
100 3
            foreach ($filesystem->listClasses('Repository') as $class) {
101 3
                $annotation->register($class);
102
            }
103
104
105 3
            $mapper->addPlugin(NestedSet::class);
106 3
            $mapper->addPlugin(Sequence::class);
107 3
            $mapper->addPlugin(Spy::class);
108
109 3
            $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...
110
111
            $mapper->addPlugin(new class($mapper) extends Plugin {
112 1
                public function afterInstantiate (Entity $entity) {
113 1
                    $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...
114 1
                }
115
            });
116
117 3
            return $mapper;
118 3
        });
119
120
        $this->getContainer()->share(Spy::class, function () {
121 1
            return $this->getContainer()->get(Mapper::class)->getPlugin(Spy::class);
122 3
        });
123
124
        $this->getContainer()->share(Packer::class, function () {
125 3
            return new PurePacker();
126 3
        });
127
128
        $this->getContainer()->share(Schema::class, function () {
129
            return $this->getContainer()->get(Mapper::class)->getSchema();
130 3
        });
131
132
        $this->getContainer()->share(StreamConnection::class, function () {
133 3
            $config = $this->getContainer()->get(Config::class);
134 3
            return new StreamConnection($config['tarantool']);
135 3
        });
136
137 3
        $this->getContainer()->share(TarantoolClient::class, function () {
138
            return $this->getContainer()->get(Client::class);
139 3
        });
140 3
    }
141
}
142