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

AjaxPages::allMethods()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
cc 3
nc 5
nop 0
ccs 0
cts 8
cp 0
crap 12
rs 9.7666
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\SnapshotInterface;
14
15
class AjaxPages
16
{
17
    /** @var SnapshotInterface */
18
    protected $Snapshot;
19
    /** @var MethodInterface */
20
    protected $Method;
21
    /** @var JobInterface */
22
    protected $Job;
23
    /** @var Aggregator */
24
    protected $Aggregator;
25
    /** @var SourceInterface */
26
    protected $Source;
27
    /** @var bool */
28
    protected $use_jobs;
29
30 1
    public function __construct(
31
        SnapshotInterface $Snapshot,
32
        MethodInterface $Method,
33
        JobInterface $Job,
34
        Aggregator $Aggregator,
35
        SourceInterface $Source,
36
        bool $use_jobs = false
37
    ) {
38 1
        $this->Snapshot = $Snapshot;
39 1
        $this->Method = $Method;
40 1
        $this->Job = $Job;
41 1
        $this->Aggregator = $Aggregator;
42 1
        $this->Source = $Source;
43 1
        $this->use_jobs = $use_jobs;
44 1
    }
45
46 6
    public function rebuildSnapshot(string $app, string $label, string $date) : array
47
    {
48 6
        $status = false;
49 6
        if ($this->use_jobs) {
50
            try {
51 3
                $this->Job->getJob(
52 3
                    $app,
53 3
                    $label,
54 3
                    $date,
55 3
                    [JobInterface::STATUS_NEW, JobInterface::STATUS_PROCESSING]
56
                );
57 1
                $message = "Job for snapshot ($app, $label, $date) is already exists";
58 2
            } catch (\InvalidArgumentException $Ex) {
59 2
                if ($this->Job->add($app, $label, $date, 'manual')) {
60 1
                    $message = "Added a job for aggregating a snapshot ($app, $label, $date)";
61 1
                    $status = true;
62
                } else {
63 3
                    $message = "Error in the snapshot ($app, $label, $date) aggregating";
64
                }
65
            }
66
        } else {
67
            try {
68 3
                $result = $this->Aggregator->setApp($app)
69 3
                    ->setLabel($label)
70 3
                    ->setDate($date)
71 3
                    ->setIsManual(true)
72 3
                    ->process();
73 2
                if (!empty($result)) {
74 1
                    $status = true;
75 1
                    $message = "Job for the snapshot ($app, $label, $date) is finished";
76
                } else {
77 1
                    $last_error = $this->Aggregator->getLastError();
78 2
                    $message = "Error in the snapshot ($app, $label, $date) aggregating: " . $last_error;
79
                }
80 1
            } catch (\Throwable $Ex) {
81 1
                $message = "Error in the snapshot ($app, $label, $date) aggregating: " . $Ex->getMessage();
82
            }
83
        }
84
85
        return [
86 6
            'status' => $status,
87 6
            'message' => $message,
88
        ];
89
    }
90
91 7
    public function checkSnapshot(string $app, string $label, string $date) : array
92
    {
93 7
        if (!$this->use_jobs) {
94
            try {
95 2
                $this->Snapshot->getOneByAppAndLabelAndDate($app, $label, $date);
96 1
                $is_processing = false;
97 1
                $message = "Job for the snapshot ($app, $label, $date) is finished";
98 1
            } catch (\InvalidArgumentException $Ex) {
99 1
                $is_processing = true;
100 1
                $message = "Job for the snapshot ($app, $label, $date) is processing now";
101
            }
102
103
            return [
104 2
                'is_new' => false,
105 2
                'is_processing' => $is_processing,
106
                'is_error' => false,
107 2
                'is_finished' => !$is_processing,
108 2
                'message' => $message
109
            ];
110
        }
111
112 5
        $is_new = $is_processing = $is_error = $is_finished = false;
113
114
        try {
115 5
            $ExistsJob = $this->Job->getJob(
116 5
                $app,
117 5
                $label,
118 5
                $date,
119
                [
120 5
                    JobInterface::STATUS_NEW,
121
                    JobInterface::STATUS_PROCESSING,
122
                    JobInterface::STATUS_FINISHED,
123
                    JobInterface::STATUS_ERROR
124
                ]
125
            );
126 4
            if ($ExistsJob->getStatus() === JobInterface::STATUS_NEW) {
127 1
                $is_new = true;
128 1
                $message = "Added a job for aggregating snapshot ($app, $label, $date)";
129 3
            } elseif ($ExistsJob->getStatus() === JobInterface::STATUS_PROCESSING) {
130 1
                $is_processing = true;
131 1
                $message = "Job for the snapshot ($app, $label, $date) is processing now";
132 2
            } elseif ($ExistsJob->getStatus() === JobInterface::STATUS_ERROR) {
133 1
                $is_error = true;
134 1
                $message = "Job for the snapshot ($app, $label, $date) is finished with error";
135
            } else {
136 1
                $is_finished = true;
137 4
                $message = "Job for the snapshot ($app, $label, $date) is finished";
138
            }
139 1
        } catch (\InvalidArgumentException $Ex) {
140 1
            $is_finished = true;
141 1
            $message = "Job for the snapshot ($app, $label, $date) is finished";
142
        }
143
144
        return [
145 5
            'is_new' => $is_new,
146 5
            'is_processing' => $is_processing,
147 5
            'is_error' => $is_error,
148 5
            'is_finished' => $is_finished,
149 5
            'message' => $message
150
        ];
151
    }
152
153 2
    public function searchMethods(string $term) : array
154
    {
155 2
        $term = ltrim($term, '\\');
156
        try {
157 2
            return $this->Method->findByName($term);
158 1
        } catch (\Throwable $Ex) {
159 1
            return [];
160
        }
161
    }
162
163
    public function allMethods() : array
164
    {
165
        try {
166
            $methods = $this->Method->all();
167
168
            $result = [];
169
            foreach ($methods as $method) {
170
                $result[$method['name']] = $method['date'];
171
            }
172
173
            return $result;
174
        } catch (\Throwable $Ex) {
175
            return [];
176
        }
177
    }
178
179 2
    public function getSourceAppList() : array
180
    {
181
        try {
182 2
            return $this->Source->getAppList();
183 1
        } catch (\Exception $Ex) {
184 1
            return [];
185
        }
186
    }
187
188 2
    public function getSourceLabelList() : array
189
    {
190
        try {
191 2
            return $this->Source->getLabelList();
192 1
        } catch (\Exception $Ex) {
193 1
            return [];
194
        }
195
    }
196
}
197