1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the `tvi/monitor-bundle` project. |
5
|
|
|
* |
6
|
|
|
* (c) https://github.com/turnaev/monitor-bundle/graphs/contributors |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
|
|
|
|
11
|
|
|
|
12
|
|
|
namespace Tvi\MonitorBundle\Controller; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
use JMS\Serializer\Serializer; |
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
17
|
|
|
use Tvi\MonitorBundle\Reporter\ReporterManager; |
18
|
|
|
use Tvi\MonitorBundle\Runner\RunnerManager; |
19
|
|
|
|
20
|
|
|
/** |
|
|
|
|
21
|
|
|
* @author Vladimir Turnaev <[email protected]> |
22
|
|
|
*/ |
|
|
|
|
23
|
|
|
trait ApiCommonTrait |
24
|
|
|
{ |
25
|
|
|
/** |
|
|
|
|
26
|
|
|
* @var RunnerManager |
27
|
|
|
*/ |
28
|
|
|
protected $runnerManager; |
29
|
|
|
|
30
|
|
|
/** |
|
|
|
|
31
|
|
|
* @var ReporterManager |
32
|
|
|
*/ |
33
|
|
|
protected $reporterManager; |
34
|
|
|
|
35
|
|
|
/** |
|
|
|
|
36
|
|
|
* @var Serializer |
37
|
|
|
*/ |
38
|
|
|
protected $serializer; |
39
|
|
|
|
40
|
60 |
|
public function __construct(RunnerManager $runnerManager, ReporterManager $reporterManager, Serializer $serializer) |
|
|
|
|
41
|
|
|
{ |
42
|
60 |
|
$this->runnerManager = $runnerManager; |
43
|
60 |
|
$this->reporterManager = $reporterManager; |
44
|
60 |
|
$this->serializer = $serializer; |
45
|
60 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
|
|
|
|
48
|
|
|
* return array [$ids, $checks, $groups, $tags]. |
|
|
|
|
49
|
|
|
*/ |
|
|
|
|
50
|
34 |
|
protected function getFilterParams(Request $request): array |
51
|
|
|
{ |
52
|
34 |
|
$id = $request->get('id', []); |
53
|
34 |
|
if (\is_scalar($id)) { |
54
|
4 |
|
$id = $id ? [$id] : []; |
55
|
|
|
} |
56
|
|
|
|
57
|
34 |
|
$ids = !\is_array($id) ? [$id] : $id; |
58
|
|
|
|
59
|
34 |
|
$checks = $request->get('check', []); |
60
|
34 |
|
if (\is_scalar($checks)) { |
61
|
3 |
|
$checks = $checks ? [$checks] : []; |
62
|
|
|
} |
63
|
34 |
|
$checks = !\is_array($checks) ? [$checks] : $checks; |
64
|
|
|
|
65
|
34 |
|
$groups = $request->get('group', []); |
66
|
34 |
|
if (\is_scalar($groups)) { |
67
|
8 |
|
$groups = $groups ? [$groups] : []; |
68
|
|
|
} |
69
|
34 |
|
$groups = !\is_array($groups) ? [$groups] : $groups; |
70
|
|
|
|
71
|
34 |
|
$tags = $request->get('tag', []); |
72
|
34 |
|
if (\is_scalar($tags)) { |
73
|
4 |
|
$tags = $tags ? [$tags] : []; |
74
|
|
|
} |
75
|
34 |
|
$tags = !\is_array($tags) ? [$tags] : $tags; |
76
|
|
|
|
77
|
34 |
|
return [$ids, $checks, $groups, $tags]; |
78
|
|
|
} |
79
|
|
|
|
80
|
60 |
|
protected function creatResponse($data = null, int $status = Response::HTTP_OK, bool $json = false, array $headers = []): Response |
|
|
|
|
81
|
|
|
{ |
82
|
60 |
|
if ($json && !\is_string($json)) { |
83
|
32 |
|
$data = $this->serializer->serialize($data, 'json'); |
84
|
|
|
} |
85
|
|
|
|
86
|
60 |
|
if ($json) { |
87
|
32 |
|
$headers['Content-Type'] = 'application/json'; |
88
|
|
|
} |
89
|
|
|
|
90
|
60 |
|
return new Response($data, $status, $headers); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|