Completed
Push — master ( c70864...b6d4e5 )
by Dmitry
03:30 queued 27s
created

Toolkit   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 68.18%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 1
dl 0
loc 105
ccs 30
cts 44
cp 0.6818
rs 10
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 4 1
A dispatch() 0 4 1
A find() 0 4 1
A findOne() 0 4 1
A findOrCreate() 0 4 1
A findOrFail() 0 4 1
A fire() 0 4 1
A get() 0 4 1
A getDate() 0 4 1
A getMapper() 0 4 1
A getRepository() 0 7 2
A getQueue() 0 15 2
A remove() 0 4 1
A select() 0 4 1
A insert() 0 4 1
A __debugInfo() 0 6 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 45
    public function __construct(Application $app)
15
    {
16 45
        $this->app = $app;
17 45
    }
18
19 3
    protected function create(string $space, array $data) : Entity
20
    {
21 3
        return $this->getRepository($space)->create($data)->save();
22
    }
23
24 45
    protected function dispatch(string $job, array $params = [], string $service = null)
25
    {
26 45
        return $this->app->dispatch($job, $params, $service);
27
    }
28
29 1
    protected function find(string $space, $params = []) : array
30
    {
31 1
        return $this->getRepository($space)->find($params);
32
    }
33
34 1
    protected function findOne(string $space, $params = []) : ?Entity
35
    {
36 1
        return $this->getRepository($space)->findOne($params);
37
    }
38
39 1
    protected function findOrCreate(string $space, $params = []) : Entity
40
    {
41 1
        return $this->getRepository($space)->findOrCreate($params);
42
    }
43
44 2
    protected function findOrFail(string $space, $params = []) : Entity
45
    {
46 2
        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 23
    protected function get(string $class)
55
    {
56 23
        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 4
    protected function getMapper()
65
    {
66 4
        return $this->get(Mapper::class);
67
    }
68
69 3
    protected function getRepository($space)
70
    {
71 3
        if (strpos($space, '.') !== false) {
72
            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->hasShared($alias, true)) {
81
            $client = $this->getMapper()->getClient();
82
            $client->evaluate("
83
                if queue == nil then
84
                    queue = require('queue')
85
                end
86
            ");
87
            $this->app->share($alias, new Queue($client, $tube));
88
        }
89
90
        return $this->app->get($alias);
91
    }
92
93 1
    protected function remove(string $space, array $params = [])
94
    {
95 1
        return $this->getRepository($space)->remove($params);
96
    }
97
98 2
    protected function select($fields, string $table, array $params)
99
    {
100 2
        return $this->get(Clickhouse::class)->select($fields, $table, $params);
101
    }
102
103 2
    protected function insert(string $table, array $data, array $headers)
104
    {
105 2
        return $this->get(Clickhouse::class)->insert($table, $data, $headers);
106
    }
107
108
    public function __debugInfo()
109
    {
110
        $info = get_object_vars($this);
111
        unset($info['app']);
112
        return $info;
113
    }
114
}
115