Completed
Push — master ( 157bd4...792762 )
by Dmitry
05:45
created

Toolkit::insert()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

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
eloc 2
nc 1
nop 3
crap 1
1
<?php
2
3
namespace Basis;
4
5
use Tarantool\Mapper\Entity;
6
use Tarantool\Mapper\Mapper;
7
use Tarantool\Queue\Queue;
8
9
trait Toolkit
10
{
11
    protected $app;
12
13 43
    public function __construct(Application $app)
14
    {
15 43
        $this->app = $app;
16 43
    }
17
18 3
    protected function create(string $space, array $data) : Entity
19
    {
20 3
        return $this->get(Mapper::class)->create($space, $data);
21
    }
22
23 43
    protected function dispatch(string $job, array $params = [], string $service = null)
24
    {
25 43
        return $this->app->dispatch($job, $params, $service);
26
    }
27
28 1
    protected function find(string $space, $params = []) : array
29
    {
30 1
        return $this->get(Mapper::class)->find($space, $params);
31
    }
32
33 1
    protected function findOne(string $space, $params = []) : ?Entity
34
    {
35 1
        return $this->get(Mapper::class)->findOne($space, $params);
36
    }
37
38 1
    protected function findOrCreate(string $space, $params = []) : Entity
39
    {
40 1
        return $this->get(Mapper::class)->findOrCreate($space, $params);
41
    }
42
43 2
    protected function findOrFail(string $space, $params = []) : Entity
44
    {
45 2
        return $this->get(Mapper::class)->findOrFail($space, $params);
46
    }
47
48
    protected function fire(string $event, array $context)
49
    {
50
        return $this->get(Event::class)->fire($event, $context);
51
    }
52
53 20
    protected function get(string $class)
54
    {
55 20
        return $this->app->get($class);
56
    }
57
58 4
    protected function getMapper()
59
    {
60 4
        return $this->get(Mapper::class);
61
    }
62
63
    protected function getQueue($tube)
64
    {
65
        $alias = "queue.$tube";
66
        if (!$this->app->hasShared($alias, true)) {
67
            $client = $this->getMapper()->getClient();
68
            $client->evaluate("
69
                if queue == nil then
70
                    queue = require('queue')
71
                end
72
            ");
73
            $this->app->share($alias, new Queue($client, $tube));
74
        }
75
76
        return $this->app->get($alias);
77
    }
78
79 1
    protected function remove(string $space, array $params = [])
80
    {
81 1
        return $this->get(Mapper::class)->remove($space, $params);
82
    }
83
84 2
    protected function select($fields, string $table, array $params)
85
    {
86 2
        return $this->get(Clickhouse::class)->select($fields, $table, $params);
87
    }
88
89 2
    protected function insert(string $table, array $data, array $headers)
90
    {
91 2
        return $this->get(Clickhouse::class)->insert($table, $data, $headers);
92
    }
93
94
    public function __debugInfo()
95
    {
96
        $info = get_object_vars($this);
97
        unset($info['app']);
98
        return $info;
99
    }
100
}
101