AbstractApiController::getValidMethod()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.351

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 14
ccs 5
cts 9
cp 0.5556
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.351
1
<?php
2
3
namespace Xervice\Api\Communication\Controller;
4
5
use DataProvider\ApiRequestDataProvider;
0 ignored issues
show
Bug introduced by
The type DataProvider\ApiRequestDataProvider was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use DataProvider\ApiResponseDataProvider;
0 ignored issues
show
Bug introduced by
The type DataProvider\ApiResponseDataProvider was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Symfony\Component\HttpFoundation\Request;
8
use Symfony\Component\HttpFoundation\Response;
9
use Xervice\Api\Business\Exception\ApiException;
10
use Xervice\Controller\Communication\Controller\AbstractController;
11
use Xervice\DataProvider\Business\Model\DataProvider\DataProviderInterface;
12
13
abstract class AbstractApiController extends AbstractController
14
{
15
    /**
16
     * @var \Symfony\Component\HttpFoundation\Request
17
     */
18
    protected $request;
19
20
    /**
21
     * @param \Symfony\Component\HttpFoundation\Request $request
22
     * @param string $method
23
     * @param mixed ...$params
24
     *
25
     * @return \Symfony\Component\HttpFoundation\Response
26
     * @throws \Xervice\Api\Business\Exception\ApiException
27
     */
28 1
    public function apiAction(Request $request, string $method, ...$params): Response
29
    {
30 1
        $this->request = $request;
31
32 1
        $execMethod = $this->getValidMethod($method);
33
34 1
        $requestData = $this->getValidDataProvider();
35
36 1
        if ($requestData) {
37 1
            $apiRequest = new ApiRequestDataProvider();
38 1
            $apiRequest->fromArray($requestData);
39
40 1
            $result = $this->$execMethod($apiRequest->getBody(), ...$params);
41
        } else {
42
            $result = $this->$execMethod(...$params);
43
        }
44
45 1
        return $result;
46
    }
47
48
    /**
49
     * @param \Xervice\DataProvider\Business\Model\DataProvider\DataProviderInterface $dataProvider
50
     * @param int $status
51
     * @param array $header
52
     *
53
     * @return \Symfony\Component\HttpFoundation\Response
54
     * @throws \InvalidArgumentException
55
     */
56 1
    public function apiResponse(DataProviderInterface $dataProvider, int $status = 200, array $header = []): Response
57
    {
58 1
        $apiDataProvider = new ApiResponseDataProvider();
59 1
        $apiDataProvider->setBody($dataProvider);
60
61 1
        return new Response(
62 1
            json_encode($apiDataProvider->toArray()),
63 1
            $status,
64 1
            $header
65
        );
66
    }
67
68
    /**
69
     * @param string $string
70
     * @param bool $capitalizeFirstCharacter
71
     *
72
     * @return string
73
     */
74 1
    protected function dashesToCamelCase(string $string, bool $capitalizeFirstCharacter = false): string
75
    {
76 1
        $str = str_replace(' ', '', ucwords(str_replace('-', ' ', $string)));
77
78 1
        if (!$capitalizeFirstCharacter) {
79 1
            $str[0] = strtolower($str[0]);
80
        }
81
82 1
        return $str;
83
    }
84
85
    /**
86
     * @param string $method
87
     *
88
     * @return string
89
     * @throws \Xervice\Api\Business\Exception\ApiException
90
     */
91 1
    protected function getValidMethod(string $method): string
92
    {
93 1
        $method = $this->dashesToCamelCase($method);
94 1
        $execMethod = $method . 'Action';
95
96 1
        if (!method_exists($this, $execMethod)) {
97
            throw new ApiException(
98
                sprintf(
99
                    'API method %s was not found',
100
                    $method
101
                )
102
            );
103
        }
104 1
        return $execMethod;
105
    }
106
107
    /**
108
     * @return null|array
109
     */
110 1
    protected function getValidDataProvider(): ?array
111
    {
112 1
        $jsonBody = $this->request->getContent();
113
114 1
        return json_decode($jsonBody, true);
115
    }
116
117
}