Completed
Push — master ( 2ebf44...463964 )
by Vladimir
05:33
created

ApiCommonTrait::creatResponse()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 4
nc 4
nop 4
crap 4
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\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
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
21
 * @author Vladimir Turnaev <[email protected]>
22
 */
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...
23
trait ApiCommonTrait
24
{
25
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
26
     * @var RunnerManager
27
     */
28
    protected $runnerManager;
29
30
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
31
     * @var ReporterManager
32
     */
33
    protected $reporterManager;
34
35
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
36
     * @var Serializer
37
     */
38
    protected $serializer;
39
40 60
    public function __construct(RunnerManager $runnerManager, ReporterManager $reporterManager, Serializer $serializer)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
41
    {
42 60
        $this->runnerManager = $runnerManager;
43 60
        $this->reporterManager = $reporterManager;
44 60
        $this->serializer = $serializer;
45 60
    }
46
47
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $request should have a doc-comment as per coding-style.
Loading history...
48
     * return array [$ids, $checks, $groups, $tags].
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
49
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
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
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function creatResponse()
Loading history...
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