Completed
Push — master ( dc8a1c...bb44ed )
by Dmitry
03:57
created

TarantoolProvider   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Test Coverage

Coverage 92.31%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 2
dl 0
loc 94
ccs 48
cts 52
cp 0.9231
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 77 1
1
<?php
2
3
namespace Basis\Provider;
4
5
use Basis\Config;
6
use Basis\Filesystem;
7
use League\Container\ServiceProvider\AbstractServiceProvider;
8
use Tarantool\Client\Client as TarantoolClient;
9
use Tarantool\Client\Connection\Connection;
10
use Tarantool\Client\Connection\StreamConnection;
11
use Tarantool\Client\Packer\Packer;
12
use Tarantool\Client\Packer\PurePacker;
13
use Tarantool\Mapper\Bootstrap;
14
use Tarantool\Mapper\Client;
15
use Tarantool\Mapper\Entity;
16
use Tarantool\Mapper\Mapper;
17
use Tarantool\Mapper\Plugin;
18
use Tarantool\Mapper\Plugin\Annotation;
19
use Tarantool\Mapper\Plugin\Sequence;
20
use Tarantool\Mapper\Plugin\Spy;
21
use Tarantool\Mapper\Plugin\Temporal;
22
use Tarantool\Mapper\Schema;
23
24
class TarantoolProvider extends AbstractServiceProvider
25
{
26
    protected $provides = [
27
        Bootstrap::class,
28
        Client::class,
29
        Connection::class,
30
        Mapper::class,
31
        Packer::class,
32
        Pool::class,
33
        Schema::class,
34
        Spy::class,
35
        StreamConnection::class,
36
        TarantoolClient::class,
37
        Temporal::class,
38
    ];
39
40
    public function register()
41
    {
42 20
        $this->container->share(Bootstrap::class, function () {
43 20
            return $this->container->get(Mapper::class)->getBootstrap();
44 20
        });
45
46 20
        $this->getContainer()->share(Client::class, function () {
47 20
            return new Client(
48 20
                $this->getContainer()->get(Connection::class),
49 20
                $this->getContainer()->get(Packer::class)
50
            );
51 20
        });
52
53 20
        $this->getContainer()->share(Connection::class, function () {
54 20
            return $this->getContainer()->get(StreamConnection::class);
55 20
        });
56
57 20
        $this->getContainer()->share(Mapper::class, function () {
58 20
            $mapper = new Mapper($this->getContainer()->get(Client::class));
59 20
            $filesystem = $this->getContainer()->get(Filesystem::class);
60
61 20
            $mapperCache = $filesystem->getPath('.cache/mapper-meta.php');
62 20
            if (file_exists($mapperCache)) {
63
                $meta = include $mapperCache;
64
                $mapper->setMeta($meta);
65
            }
66
67 20
            $annotation = $mapper->getPlugin(Annotation::class);
68
69 20
            foreach ($filesystem->listClasses('Entity') as $class) {
70 20
                $annotation->register($class);
71
            }
72 20
            foreach ($filesystem->listClasses('Repository') as $class) {
73 20
                $annotation->register($class);
74
            }
75
76 20
            $mapper->getPlugin(Sequence::class);
77 20
            $mapper->getPlugin(Spy::class);
78 20
            $mapper->getPlugin(Temporal::class);
79
80 20
            $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...
81
82
            $mapper->getPlugin(new class($mapper) extends Plugin {
83 20
                public function afterInstantiate(Entity $entity)
84
                {
85 20
                    $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...
86 20
                }
87
            });
88
89 20
            return $mapper;
90 20
        });
91
92 20
        $this->getContainer()->share(Packer::class, function () {
93 20
            return new PurePacker();
94 20
        });
95
96 20
        $this->getContainer()->share(Schema::class, function () {
97
            return $this->getContainer()->get(Mapper::class)->getSchema();
98 20
        });
99
100 20
        $this->getContainer()->share(Spy::class, function () {
101 2
            return $this->getContainer()->get(Mapper::class)->getPlugin(Spy::class);
102 20
        });
103
104 20
        $this->getContainer()->share(StreamConnection::class, function () {
105 20
            $config = $this->getContainer()->get(Config::class);
106 20
            return new StreamConnection($config['tarantool.connection'], $config['tarantool.params']);
107 20
        });
108
109 20
        $this->getContainer()->share(TarantoolClient::class, function () {
110 14
            return $this->getContainer()->get(Client::class);
111 20
        });
112
113 20
        $this->getContainer()->share(Temporal::class, function () {
114
            return $this->getContainer()->get(Mapper::class)->getPlugin(Temporal::class);
115 20
        });
116 20
    }
117
}
118