Completed
Push — master ( 8b75ec...fe01cd )
by Dmitry
04:22
created

Toolkit::getMapper()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Basis;
4
5
use Tarantool\Mapper\Entity;
6
use Tarantool\Mapper\Mapper;
7
use Tarantool\Mapper\Pool;
8
use Tarantool\Queue\Queue;
9
10
trait Toolkit
11
{
12
    protected $app;
13
14 53
    public function __construct(Application $app)
15
    {
16 53
        $this->app = $app;
17 53
    }
18
19 5
    protected function create(string $space, array $data)
20
    {
21 5
        return $this->getRepository($space)->create($data)->save();
22
    }
23
24 53
    protected function dispatch(string $job, array $params = [], string $service = null)
25
    {
26 53
        return $this->app->dispatch($job, $params, $service);
27
    }
28
29 4
    protected function find(string $space, $params = []) : array
30
    {
31 4
        return $this->getRepository($space)->find($params);
32
    }
33
34 2
    protected function findOne(string $space, $params = [])
35
    {
36 2
        return $this->getRepository($space)->findOne($params);
37
    }
38
39 2
    protected function findOrCreate(string $space, $params = [])
40
    {
41 2
        return $this->getRepository($space)->findOrCreate($params);
42
    }
43
44 3
    protected function findOrFail(string $space, $params = [])
45
    {
46 3
        return $this->getRepository($space)->findOrFail($params);
47
    }
48
49
    protected function fire(string $event, array $context)
50
    {
51
        return $this->get(Event::class)->fire($event, $context);
52
    }
53
54 30
    protected function get(string $class)
55
    {
56 30
        return $this->app->get($class);
57
    }
58
59 1
    protected function getDate()
60
    {
61 1
        return call_user_func_array([$this->get(Converter::class), 'getDate'], func_get_args());
62
    }
63
64 5
    protected function getMapper()
65
    {
66 5
        return $this->get(Mapper::class);
67
    }
68
69 6
    protected function getRepository($space)
70
    {
71 6
        if (strpos($space, '.') !== false) {
72 3
            return $this->get(Pool::class)->getRepository($space);
73
        }
74 3
        return $this->get(Mapper::class)->getRepository($space);
75
    }
76
77
    protected function getQueue($tube)
78
    {
79
        $alias = "queue.$tube";
80
        if (!$this->app->hasInstance($alias, true)) {
81
            if (strpos($tube, '.') !== false) {
82
                [$service, $tube] = explode('.', $tube);
0 ignored issues
show
Bug introduced by
The variable $service does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
83
                $client = $this->get(Pool::class)->get($service)->getClient();
84
            } else {
85
                $client = $this->getMapper()->getClient();
86
            }
87
            $client->evaluate("
88
                if queue == nil then
89
                    queue = require('queue')
90
                end
91
            ");
92
            $this->app->share($alias, new Queue($client, $tube));
93
        }
94
95
        return $this->app->get($alias);
96
    }
97
98 2
    protected function remove(string $space, array $params = [])
99
    {
100 2
        return $this->getRepository($space)->remove($params);
101
    }
102
103 2
    protected function select($fields, string $table, array $params)
104
    {
105 2
        return $this->get(Clickhouse::class)->select($fields, $table, $params);
106
    }
107
108 2
    protected function insert(string $table, array $data, array $headers)
109
    {
110 2
        return $this->get(Clickhouse::class)->insert($table, $data, $headers);
111
    }
112
113 1
    protected function upload(string $filename, $contents) : string
114
    {
115 1
        return $this->get(Storage::class)->upload($filename, $contents);
116
    }
117
118 1
    protected function download(string $hash)
119
    {
120 1
        return $this->get(Storage::class)->download($hash);
121
    }
122
123
    public function __debugInfo()
124
    {
125
        $info = get_object_vars($this);
126
        unset($info['app']);
127
        return $info;
128
    }
129
}
130