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\JsonResponse; |
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
use JMS\Serializer\Serializer; |
17
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
18
|
|
|
use Tvi\MonitorBundle\Exception\HttpException; |
19
|
|
|
use Tvi\MonitorBundle\Reporter\ReporterManager; |
20
|
|
|
use Tvi\MonitorBundle\Runner\RunnerManager; |
21
|
|
|
|
22
|
|
|
/** |
|
|
|
|
23
|
|
|
* @property RunnerManager $runnerManager |
24
|
|
|
* @property ReporterManager $reporterManager |
25
|
|
|
* @property Serializer $serializer |
26
|
|
|
* |
27
|
|
|
* @author Vladimir Turnaev <[email protected]> |
28
|
|
|
*/ |
|
|
|
|
29
|
|
|
trait ApiInfoGroupTrait |
30
|
|
|
{ |
31
|
|
|
public function groupInfoAction(Request $request, string $id): JsonResponse |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
try { |
34
|
|
|
$groups = $this->runnerManager->findGroups($id); |
35
|
|
|
$groups = array_values($groups); |
36
|
|
|
|
37
|
|
|
if (1 === \count($groups)) { |
38
|
|
|
$group = current($groups); |
39
|
|
|
$json = $this->serializer->serialize($group, 'json'); |
40
|
|
|
|
41
|
|
|
return JsonResponse::fromJsonString($json); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
throw new NotFoundHttpException(sprintf('Group "%s" not found', $id)); |
45
|
|
|
} catch (NotFoundHttpException $e) { |
46
|
|
|
$e = new HttpException(404, $e->getMessage()); |
47
|
|
|
$json = $this->serializer->serialize($e->toArray(), 'json'); |
48
|
|
|
|
49
|
|
|
return JsonResponse::fromJsonString($json, $e->getStatusCode()); |
50
|
|
|
} catch (\Exception $e) { |
51
|
|
|
$e = new HttpException(500, $e->getMessage()); |
52
|
|
|
|
53
|
|
|
$data = $this->serializer->serialize($e->toArray(), 'json'); |
54
|
|
|
|
55
|
|
|
return JsonResponse::fromJsonString($data, $e->getStatusCode()); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function groupInfosAction(Request $request): JsonResponse |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
try { |
62
|
|
|
$ids = $this->getFilterIds($request); |
|
|
|
|
63
|
|
|
$groups = array_values($this->runnerManager->findGroups($ids)); |
64
|
|
|
$json = $this->serializer->serialize($groups, 'json'); |
65
|
|
|
|
66
|
|
|
return JsonResponse::fromJsonString($json); |
67
|
|
|
} catch (\Exception $e) { |
68
|
|
|
$e = new HttpException(500, $e->getMessage()); |
69
|
|
|
$json = $this->serializer->serialize($e->toArray(), 'json'); |
70
|
|
|
|
71
|
|
|
return new JsonResponse($json, $e->getStatusCode()); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|