Completed
Push — master ( 40a2ec...a041ae )
by Shagiakhmetov
02:15
created

Method::setLastUsedDate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 12
ccs 6
cts 7
cp 0.8571
crap 2.0116
rs 9.8666
c 0
b 0
f 0
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']] = trim($row['name']);
39
            }
40
        }
41
42 2
        return $methods;
43
    }
44
45 2
    public function getListByNames(array $names) : array
46
    {
47 2
        if (empty($names)) {
48 1
            return [];
49
        }
50
51 1
        $result = $this->AggregatorStorage->getAll(
52 1
            self::TABLE_NAME,
53 1
            ['all'],
54
            [
55
                'filter' => [
56 1
                    ['name', $names]
57
                ]
58
            ]
59
        );
60
61 1
        return $result;
62
    }
63
64 3
    public function getListByIds(array $ids) : array
65
    {
66 3
        if (empty($ids)) {
67 1
            return [];
68
        }
69
70 2
        $methods = [];
71 2
        while (!empty($ids)) {
72 2
            $ids_to_het = \array_slice($ids, 0, 500);
73 2
            $ids = \array_slice($ids, 500);
74
75 2
            $result = $this->AggregatorStorage->getAll(
76 2
                self::TABLE_NAME,
77 2
                ['all'],
78
                [
79
                    'filter' => [
80 2
                        ['id', $ids_to_het]
81
                    ]
82
                ]
83
            );
84
85 2
            if (!empty($result)) {
86 1
                foreach ($result as $method) {
87 1
                    $methods[$method['id']] = $method['name'];
88
                }
89
            }
90
        }
91
92 2
        return $methods;
93
    }
94
95 2
    public function insertMany(array $inserts) : bool
96
    {
97 2
        if (empty($inserts)) {
98 1
            return false;
99
        }
100
101 1
        return $this->AggregatorStorage->insertMany(self::TABLE_NAME, $inserts);
102
    }
103
104 1
    public function injectMethodNames(array $data) : array
105
    {
106 1
        $method_ids = [];
107 1
        foreach ($data as $Item) {
108 1
            $method_ids[$Item->getMethodId()] = $Item->getMethodId();
109
        }
110
111 1
        $methods = $this->getListByIds($method_ids);
112
113 1
        if (!empty($methods)) {
114 1
            foreach ($data as $key => $Item) {
115 1
                $Item->setMethodName(
116 1
                    isset($methods[$Item->getMethodId()])
117 1
                        ? trim($methods[$Item->getMethodId()])
118 1
                        : '?'
119
                );
120 1
                $data[$key] = $Item;
121
            }
122
        }
123
124 1
        return $data;
125
    }
126
127 1
    public function setLastUsedDate(array $ids, string $date) : bool
128
    {
129 1
        if (empty($ids)) {
130
            return false;
131
        }
132
133 1
        return $this->AggregatorStorage->update(
134 1
            self::TABLE_NAME,
135 1
            ['date' => $date],
136 1
            ['id' => $ids, 'date' => ['value' => $date, 'operator' => '!=']]
137
        );
138
    }
139
}
140