Completed
Push — master ( f94778...2afcdb )
by Shagiakhmetov
01:55
created

AjaxPages::getMethodUsedApps()   B

Complexity

Conditions 8
Paths 84

Size

Total Lines 56

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
cc 8
nc 84
nop 1
dl 0
loc 56
ccs 0
cts 37
cp 0
crap 72
rs 7.7155
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
2
3
/**
4
 * @maintainer Timur Shagiakhmetov <[email protected]>
5
 */
6
7
namespace Badoo\LiveProfilerUI\Pages;
8
9
use Badoo\LiveProfilerUI\Aggregator;
10
use Badoo\LiveProfilerUI\DataProviders\Interfaces\SourceInterface;
11
use Badoo\LiveProfilerUI\DataProviders\Interfaces\JobInterface;
12
use Badoo\LiveProfilerUI\DataProviders\Interfaces\MethodInterface;
13
use Badoo\LiveProfilerUI\DataProviders\Interfaces\MethodDataInterface;
14
use Badoo\LiveProfilerUI\DataProviders\Interfaces\SnapshotInterface;
15
use Badoo\LiveProfilerUI\FieldList;
16
17
class AjaxPages
18
{
19
    /** @var SnapshotInterface */
20
    protected $Snapshot;
21
    /** @var MethodInterface */
22
    protected $Method;
23
    /** @var MethodDataInterface */
24
    protected $MethodData;
25
    /** @var JobInterface */
26
    protected $Job;
27
    /** @var Aggregator */
28
    protected $Aggregator;
29
    /** @var SourceInterface */
30
    protected $Source;
31
    /** @var FieldList */
32
    protected $FieldList;
33
    /** @var bool */
34
    protected $use_jobs;
35
36 1
    public function __construct(
37
        SnapshotInterface $Snapshot,
38
        MethodInterface $Method,
39
        MethodDataInterface $MethodData,
40
        JobInterface $Job,
41
        Aggregator $Aggregator,
42
        SourceInterface $Source,
43
        FieldList $FieldList,
44
        bool $use_jobs = false
45
    ) {
46 1
        $this->Snapshot = $Snapshot;
47 1
        $this->Method = $Method;
48 1
        $this->MethodData = $MethodData;
49 1
        $this->Job = $Job;
50 1
        $this->Aggregator = $Aggregator;
51 1
        $this->Source = $Source;
52 1
        $this->FieldList = $FieldList;
53 1
        $this->use_jobs = $use_jobs;
54 1
    }
55
56 6
    public function rebuildSnapshot(string $app, string $label, string $date) : array
57
    {
58 6
        $status = false;
59 6
        if ($this->use_jobs) {
60
            try {
61 3
                $this->Job->getJob(
62 3
                    $app,
63 3
                    $label,
64 3
                    $date,
65 3
                    [JobInterface::STATUS_NEW, JobInterface::STATUS_PROCESSING]
66
                );
67 1
                $message = "Job for snapshot ($app, $label, $date) is already exists";
68 2
            } catch (\InvalidArgumentException $Ex) {
69 2
                if ($this->Job->add($app, $label, $date, 'manual')) {
70 1
                    $message = "Added a job for aggregating a snapshot ($app, $label, $date)";
71 1
                    $status = true;
72
                } else {
73 3
                    $message = "Error in the snapshot ($app, $label, $date) aggregating";
74
                }
75
            }
76
        } else {
77
            try {
78 3
                $result = $this->Aggregator->setApp($app)
79 3
                    ->setLabel($label)
80 3
                    ->setDate($date)
81 3
                    ->setIsManual(true)
82 3
                    ->process();
83 2
                if (!empty($result)) {
84 1
                    $status = true;
85 1
                    $message = "Job for the snapshot ($app, $label, $date) is finished";
86
                } else {
87 1
                    $last_error = $this->Aggregator->getLastError();
88 2
                    $message = "Error in the snapshot ($app, $label, $date) aggregating: " . $last_error;
89
                }
90 1
            } catch (\Throwable $Ex) {
91 1
                $message = "Error in the snapshot ($app, $label, $date) aggregating: " . $Ex->getMessage();
92
            }
93
        }
94
95
        return [
96 6
            'status' => $status,
97 6
            'message' => $message,
98
        ];
99
    }
100
101 7
    public function checkSnapshot(string $app, string $label, string $date) : array
102
    {
103 7
        if (!$this->use_jobs) {
104
            try {
105 2
                $this->Snapshot->getOneByAppAndLabelAndDate($app, $label, $date);
106 1
                $is_processing = false;
107 1
                $message = "Job for the snapshot ($app, $label, $date) is finished";
108 1
            } catch (\InvalidArgumentException $Ex) {
109 1
                $is_processing = true;
110 1
                $message = "Job for the snapshot ($app, $label, $date) is processing now";
111
            }
112
113
            return [
114 2
                'is_new' => false,
115 2
                'is_processing' => $is_processing,
116
                'is_error' => false,
117 2
                'is_finished' => !$is_processing,
118 2
                'message' => $message
119
            ];
120
        }
121
122 5
        $is_new = $is_processing = $is_error = $is_finished = false;
123
124
        try {
125 5
            $ExistsJob = $this->Job->getJob(
126 5
                $app,
127 5
                $label,
128 5
                $date,
129
                [
130 5
                    JobInterface::STATUS_NEW,
131
                    JobInterface::STATUS_PROCESSING,
132
                    JobInterface::STATUS_FINISHED,
133
                    JobInterface::STATUS_ERROR
134
                ]
135
            );
136 4
            if ($ExistsJob->getStatus() === JobInterface::STATUS_NEW) {
137 1
                $is_new = true;
138 1
                $message = "Added a job for aggregating snapshot ($app, $label, $date)";
139 3
            } elseif ($ExistsJob->getStatus() === JobInterface::STATUS_PROCESSING) {
140 1
                $is_processing = true;
141 1
                $message = "Job for the snapshot ($app, $label, $date) is processing now";
142 2
            } elseif ($ExistsJob->getStatus() === JobInterface::STATUS_ERROR) {
143 1
                $is_error = true;
144 1
                $message = "Job for the snapshot ($app, $label, $date) is finished with error";
145
            } else {
146 1
                $is_finished = true;
147 4
                $message = "Job for the snapshot ($app, $label, $date) is finished";
148
            }
149 1
        } catch (\InvalidArgumentException $Ex) {
150 1
            $is_finished = true;
151 1
            $message = "Job for the snapshot ($app, $label, $date) is finished";
152
        }
153
154
        return [
155 5
            'is_new' => $is_new,
156 5
            'is_processing' => $is_processing,
157 5
            'is_error' => $is_error,
158 5
            'is_finished' => $is_finished,
159 5
            'message' => $message
160
        ];
161
    }
162
163 2
    public function searchMethods(string $term) : array
164
    {
165 2
        $term = ltrim($term, '\\');
166
        try {
167 2
            return $this->Method->findByName($term);
168 1
        } catch (\Throwable $Ex) {
169 1
            return [];
170
        }
171
    }
172
173
    public function getMethodUsedApps(string $method_name) : array
174
    {
175
        $method_name = ltrim($method_name, '\\');
176
        try {
177
            $methods = $this->Method->findByName($method_name, true);
178
            if (!$methods) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $methods of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
179
                return [];
180
            }
181
182
            $method = current($methods);
183
184
            $last_two_days = \Badoo\LiveProfilerUI\DateGenerator::getDatesArray(date('Y-m-d'), 2, 2);
185
            $start_snapshot_id = in_array(current($methods)['date'], $last_two_days, true)
186
                ? $this->Snapshot->getMinSnapshotIdByDates($last_two_days)
187
                : 0;
188
189
            $method_data = $this->MethodData->getDataByMethodIdsAndSnapshotIds(
190
                [],
191
                [$method['id']],
192
                100,
193
                $start_snapshot_id
194
            );
195
196
            $snapshot_ids = [];
197
            foreach ($method_data as $Row) {
198
                $snapshot_id = $Row->getSnapshotId();
199
                $snapshot_ids[$snapshot_id] = $snapshot_id;
200
            }
201
            $snapshots = $this->Snapshot->getListByIds($snapshot_ids);
202
203
            $fields = $this->FieldList->getFields();
204
205
            $results = [];
206
            foreach ($method_data as $Row) {
207
                $result = [];
208
                $result['app'] = $snapshots[$Row->getSnapshotId()]['app'];
209
                $result['label'] = $snapshots[$Row->getSnapshotId()]['label'];
210
211
                $uniq_key = $result['app'] . '_' . $result['label'];
212
                if (!empty($results[$uniq_key])) {
213
                    continue;
214
                }
215
216
                $values = $Row->getFormattedValues();
217
                foreach ($fields as $field) {
218
                    $result['fields'][$field] = $values[$field];
219
                }
220
                $result['fields']['calls_count'] = $snapshots[$Row->getSnapshotId()]['calls_count'];
221
222
                $results[$uniq_key] = $result;
223
            }
224
            return array_values($results);
225
        } catch (\Throwable $Ex) {
226
            return [];
227
        }
228
    }
229
230
    public function allMethods() : array
231
    {
232
        try {
233
            $methods = $this->Method->all();
234
235
            $result = [];
236
            foreach ($methods as $method) {
237
                $result[$method['name']] = $method['date'];
238
            }
239
240
            return $result;
241
        } catch (\Throwable $Ex) {
242
            return [];
243
        }
244
    }
245
246 2
    public function getSourceAppList() : array
247
    {
248
        try {
249 2
            return $this->Source->getAppList();
250 1
        } catch (\Exception $Ex) {
251 1
            return [];
252
        }
253
    }
254
255 2
    public function getSourceLabelList() : array
256
    {
257
        try {
258 2
            return $this->Source->getLabelList();
259 1
        } catch (\Exception $Ex) {
260 1
            return [];
261
        }
262
    }
263
}
264