Completed
Push — master ( 5ced43...d61bac )
by Shagiakhmetov
23:36
created

AjaxPages::getSourceAppList()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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