Completed
Push — master ( 442eb8...3b7604 )
by Dmitry
02:43
created

Toolkit::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Basis;
4
5
use ClickHouseDB\Client;
6
use Tarantool\Mapper\Entity;
7
use Tarantool\Mapper\Mapper;
8
9
trait Toolkit
10
{
11
    protected $app;
12
13 26
    public function __construct(Application $app)
14
    {
15 26
        $this->app = $app;
16 26
    }
17
18 1
    public function create(string $space, array $data) : Entity
19
    {
20 1
        return $this->get(Mapper::class)->create($space, $data);
21
    }
22
23 26
    public function dispatch(string $job, array $params = [], string $service = null)
24
    {
25 26
        return $this->app->dispatch($job, $params, $service);
26
    }
27
28 1
    public function find(string $space, $params = []) : array
29
    {
30 1
        return $this->get(Mapper::class)->find($space, $params);
31
    }
32
33 1
    public function findOne(string $space, $params = []) : ?Entity
34
    {
35 1
        return $this->get(Mapper::class)->findOne($space, $params);
36
    }
37
38 1
    public function findOrCreate(string $space, $params = []) : Entity
39
    {
40 1
        return $this->get(Mapper::class)->findOrCreate($space, $params);
41
    }
42
43 1
    public function findOrFail(string $space, $params = []) : Entity
44
    {
45 1
        return $this->get(Mapper::class)->findOrFail($space, $params);
46
    }
47
48
    public function fire(string $event, array $context)
49
    {
50
        return $this->get(Event::class)->fire($event, $context);
51
    }
52
53 7
    public function get(string $class)
54
    {
55 7
        return $this->app->get($class);
56
    }
57
58
    public function getMapper()
59
    {
60
        return $this->get(Mapper::class);
61
    }
62
63 1
    public function remove(string $space, array $params = [])
64
    {
65 1
        return $this->get(Mapper::class)->remove($space, $params);
66
    }
67
68
    public function select($fields, string $table, array $params)
69
    {
70
        if (is_array($fields)) {
71
            $fields = implode(', ', $fields);
72
        }
73
74
        $binds = [];
75
        $where = [];
76
        foreach ($params as $k => $v) {
77
            $binds[$k] = (array) $v;
78
            $where[] = $k.' in (:'.$k.')';
79
        }
80
81
        $where = implode(' and ', $where);
82
83
        $query = "SELECT $fields FROM $table where $where";
84
85
        return $this->get(Client::class)->select($query, $binds);
86
    }
87
88
    public function __debugInfo()
89
    {
90
        $info = get_object_vars($this);
91
        unset($info['app']);
92
        return $info;
93
    }
94
}
95