Completed
Push — master ( 88729d...356279 )
by Dmitry
02:54
created

Toolkit::__debugInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Basis;
4
5
use GuzzleHttp\Client;
6
use Tarantool\Mapper\Entity;
7
use Tarantool\Mapper\Mapper;
8
use Tarantool\Mapper\Pool;
9
use Tarantool\Queue\Queue;
10
11
trait Toolkit
12
{
13
    protected $app;
14
15 48
    public function __construct(Application $app)
16
    {
17 48
        $this->app = $app;
18 48
    }
19
20 4
    protected function create(string $space, array $data)
21
    {
22 4
        return $this->getRepository($space)->create($data)->save();
23
    }
24
25 48
    protected function dispatch(string $job, array $params = [], string $service = null)
26
    {
27 48
        return $this->app->dispatch($job, $params, $service);
28
    }
29
30 3
    protected function find(string $space, $params = []) : array
31
    {
32 3
        return $this->getRepository($space)->find($params);
33
    }
34
35 2
    protected function findOne(string $space, $params = [])
36
    {
37 2
        return $this->getRepository($space)->findOne($params);
38
    }
39
40 2
    protected function findOrCreate(string $space, $params = [])
41
    {
42 2
        return $this->getRepository($space)->findOrCreate($params);
43
    }
44
45 3
    protected function findOrFail(string $space, $params = [])
46
    {
47 3
        return $this->getRepository($space)->findOrFail($params);
48
    }
49
50
    protected function fire(string $event, array $context)
51
    {
52
        return $this->get(Event::class)->fire($event, $context);
53
    }
54
55 25
    protected function get(string $class)
56
    {
57 25
        return $this->app->get($class);
58
    }
59
60 1
    protected function getDate()
61
    {
62 1
        return call_user_func_array([$this->get(Converter::class), 'getDate'], func_get_args());
63
    }
64
65 4
    protected function getMapper()
66
    {
67 4
        return $this->get(Mapper::class);
68
    }
69
70 5
    protected function getRepository($space)
71
    {
72 5
        if (strpos($space, '.') !== false) {
73 2
            return $this->get(Pool::class)->getRepository($space);
74
        }
75 3
        return $this->get(Mapper::class)->getRepository($space);
76
    }
77
78
    protected function getQueue($tube)
79
    {
80
        $alias = "queue.$tube";
81
        if (!$this->app->hasInstance($alias, true)) {
82
            $client = $this->getMapper()->getClient();
83
            $client->evaluate("
84
                if queue == nil then
85
                    queue = require('queue')
86
                end
87
            ");
88
            $this->app->share($alias, new Queue($client, $tube));
89
        }
90
91
        return $this->app->get($alias);
92
    }
93
94 2
    protected function remove(string $space, array $params = [])
95
    {
96 2
        return $this->getRepository($space)->remove($params);
97
    }
98
99 2
    protected function select($fields, string $table, array $params)
100
    {
101 2
        return $this->get(Clickhouse::class)->select($fields, $table, $params);
102
    }
103
104 2
    protected function insert(string $table, array $data, array $headers)
105
    {
106 2
        return $this->get(Clickhouse::class)->insert($table, $data, $headers);
107
    }
108
109
    protected function upload(string $filename, $contents) : string
110
    {
111
        $client = new Client();
112
        $response = $client->request('POST', 'http://storage/storage/upload', [
113
            'multipart' => [
114
                [
115
                    'name' => 'file',
116
                    'filename' => $filename,
117
                    'contents' => $contents,
118
                ],
119
            ]
120
        ]);
121
122
        return json_decode($response->getBody())->hash[0];
123
    }
124
125
    public function __debugInfo()
126
    {
127
        $info = get_object_vars($this);
128
        unset($info['app']);
129
        return $info;
130
    }
131
}
132