ApiController::getSubscribedServices()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace jin2chen\ApiBundle\Controller;
6
7
use jin2chen\ApiBundle\Response\ResponseConverter;
8
use jin2chen\ApiBundle\Response\Type\CollectionType;
9
use jin2chen\ApiBundle\Response\Type\ObjectType;
10
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
11
use Symfony\Component\HttpFoundation\JsonResponse;
12
13
use function array_merge;
14
15
abstract class ApiController extends AbstractController
16
{
17
    /**
18
     * @inheritdoc
19
     */
20 2
    public static function getSubscribedServices(): array
21
    {
22 2
        return array_merge(
23 2
            parent::getSubscribedServices(),
24
            [
25 2
                ResponseConverter::class,
26
            ]
27
        );
28
    }
29
30
    /**
31
     * @noinspection PhpIncompatibleReturnTypeInspection
32
     * @psalm-suppress all
33
     */
34 2
    protected function responseConverter(): ResponseConverter
35
    {
36 2
        return $this->get(ResponseConverter::class);
37
    }
38
39 1
    final protected function item(ObjectType $resource): JsonResponse
40
    {
41 1
        return $this->responseConverter()->toJson($resource);
42
    }
43
44 1
    final protected function collection(CollectionType $resource): JsonResponse
45
    {
46 1
        return $this->responseConverter()->toJson($resource);
47
    }
48
}
49