Completed
Push — master ( 8cfc77...3ca914 )
by Vladimir
06:29
created

ApiInfoGroupTrait::groupInfosAction()   A

Complexity

Conditions 2
Paths 5

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 13
ccs 0
cts 9
cp 0
rs 9.9666
c 0
b 0
f 0
cc 2
nc 5
nop 1
crap 6
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
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
Coding Style introduced by
Missing @link tag in file comment
Loading history...
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
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
23
 * @property RunnerManager   $runnerManager
24
 * @property ReporterManager $reporterManager
25
 * @property Serializer      $serializer
26
 *
27
 * @author Vladimir Turnaev <[email protected]>
28
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
29
trait ApiInfoGroupTrait
30
{
31
    public function groupInfoAction(Request $request, string $id): JsonResponse
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

31
    public function groupInfoAction(/** @scrutinizer ignore-unused */ Request $request, string $id): JsonResponse

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Coding Style introduced by
Missing doc comment for function groupInfoAction()
Loading history...
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
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function groupInfosAction()
Loading history...
60
    {
61
        try {
62
            $ids = $this->getFilterIds($request);
0 ignored issues
show
Bug introduced by
It seems like getFilterIds() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
            /** @scrutinizer ignore-call */ 
63
            $ids = $this->getFilterIds($request);
Loading history...
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