|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Xervice\Api\Communication\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use DataProvider\ApiRequestDataProvider; |
|
|
|
|
|
|
6
|
|
|
use DataProvider\ApiResponseDataProvider; |
|
|
|
|
|
|
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
|
|
|
} |
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths