Completed
Push — master ( 8032f7...0523e8 )
by Shagiakhmetov
01:45
created

Method::all()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 16
c 0
b 0
f 0
cc 3
nc 2
nop 0
ccs 0
cts 10
cp 0
crap 12
rs 9.7333
1
<?php declare(strict_types=1);
2
3
/**
4
 * @maintainer Timur Shagiakhmetov <[email protected]>
5
 */
6
7
namespace Badoo\LiveProfilerUI\DataProviders;
8
9
use Badoo\LiveProfilerUI\DataProviders\Interfaces\MethodInterface;
10
11
class Method extends Base implements MethodInterface
12
{
13
    const MAX_METHODS_BY_NAME = 20;
14
    const TABLE_NAME = 'aggregator_metods';
15
16 3
    public function findByName(string $method_name, bool $strict = false) : array
17
    {
18 3
        if (!$method_name) {
19 1
            return [];
20
        }
21
22 2
        $name_filter = ['name', $method_name];
23 2
        if (!$strict) {
24 2
            $name_filter[] = 'like';
25
        }
26
27 2
        $result = $this->AggregatorStorage->getAll(
28 2
            self::TABLE_NAME,
29 2
            ['all'],
30
            [
31 2
                'filter' => [$name_filter],
32 2
                'limit' => self::MAX_METHODS_BY_NAME
33
            ]
34
        );
35 2
        $methods = [];
36 2
        if (!empty($result)) {
37 2
            foreach ($result as $row) {
38 2
                $methods[$row['id']] = $row;
39
            }
40
        }
41
42 2
        return $methods;
43
    }
44
45
    public function all() : array
46
    {
47
        $result = $this->AggregatorStorage->getAll(
48
            self::TABLE_NAME,
49
            ['all'],
50
            []
51
        );
52
        $methods = [];
53
        if (!empty($result)) {
54
            foreach ($result as $row) {
55
                $methods[$row['id']] = $row;
56
            }
57
        }
58
59
        return $methods;
60
    }
61
62 2
    public function getListByNames(array $names) : array
63
    {
64 2
        if (empty($names)) {
65 1
            return [];
66
        }
67
68 1
        $result = $this->AggregatorStorage->getAll(
69 1
            self::TABLE_NAME,
70 1
            ['all'],
71
            [
72
                'filter' => [
73 1
                    ['name', $names]
74
                ]
75
            ]
76
        );
77
78 1
        return $result;
79
    }
80
81 3
    public function getListByIds(array $ids) : array
82
    {
83 3
        if (empty($ids)) {
84 1
            return [];
85
        }
86
87 2
        $methods = [];
88 2
        while (!empty($ids)) {
89 2
            $ids_to_het = \array_slice($ids, 0, 500);
90 2
            $ids = \array_slice($ids, 500);
91
92 2
            $result = $this->AggregatorStorage->getAll(
93 2
                self::TABLE_NAME,
94 2
                ['all'],
95
                [
96
                    'filter' => [
97 2
                        ['id', $ids_to_het]
98
                    ]
99
                ]
100
            );
101
102 2
            if (!empty($result)) {
103 1
                foreach ($result as $method) {
104 1
                    $methods[$method['id']] = $method['name'];
105
                }
106
            }
107
        }
108
109 2
        return $methods;
110
    }
111
112 2
    public function insertMany(array $inserts) : bool
113
    {
114 2
        if (empty($inserts)) {
115 1
            return false;
116
        }
117
118 1
        return $this->AggregatorStorage->insertMany(self::TABLE_NAME, $inserts);
119
    }
120
121 1
    public function injectMethodNames(array $data) : array
122
    {
123 1
        $method_ids = [];
124 1
        foreach ($data as $Item) {
125 1
            $method_ids[$Item->getMethodId()] = $Item->getMethodId();
126
        }
127
128 1
        $methods = $this->getListByIds($method_ids);
129
130 1
        if (!empty($methods)) {
131 1
            foreach ($data as $key => $Item) {
132 1
                $Item->setMethodName(
133 1
                    isset($methods[$Item->getMethodId()])
134 1
                        ? trim($methods[$Item->getMethodId()])
135 1
                        : '?'
136
                );
137 1
                $data[$key] = $Item;
138
            }
139
        }
140
141 1
        return $data;
142
    }
143
144 1
    public function setLastUsedDate(array $ids, string $date) : bool
145
    {
146 1
        if (empty($ids)) {
147
            return false;
148
        }
149
150 1
        return $this->AggregatorStorage->update(
151 1
            self::TABLE_NAME,
152 1
            ['date' => $date],
153 1
            ['id' => $ids]
154
        );
155
    }
156
}
157