66
77use App \Enum \HttpMethod ;
88use App \Enum \WebserviceEndpoint ;
9+ use App \RemoteSite \Responses \FinalizeUpdate as FinalizeUpdateResponse ;
910use App \RemoteSite \Responses \HealthCheck as HealthCheckResponse ;
11+ use App \RemoteSite \Responses \GetUpdate as GetUpdateResponse ;
12+ use App \RemoteSite \Responses \PrepareUpdate as PrepareUpdateResponse ;
13+ use App \RemoteSite \Responses \ResponseInterface ;
1014use GuzzleHttp \Client ;
1115use GuzzleHttp \Exception \RequestException ;
1216use GuzzleHttp \Psr7 \Request ;
1317use GuzzleHttp \Psr7 \Response ;
1418use Illuminate \Support \Facades \App ;
1519use Psr \Http \Message \RequestInterface ;
1620
21+ /**
22+ * @method HealthCheckResponse checkHealth()
23+ * @method GetUpdateResponse getUpdate()
24+ * @method PrepareUpdateResponse prepareUpdate(string $targetVersion)
25+ * @method FinalizeUpdateResponse finalizeUpdate()
26+ */
1727class Connection
1828{
1929 public function __construct (protected readonly string $ baseUrl , protected readonly string $ key )
2030 {
2131 }
2232
23- public function checkHealth ( ): HealthCheckResponse
33+ public function __call ( string $ method , array $ arguments ): ResponseInterface
2434 {
25- $ healthData = $ this ->performWebserviceRequest (
26- HttpMethod::GET ,
27- WebserviceEndpoint::HEALTH_CHECK
35+ $ endpoint = WebserviceEndpoint::tryFromName ($ method );
36+
37+ if (is_null ($ endpoint )) {
38+ throw new \BadMethodCallException ();
39+ }
40+
41+ // Call
42+ $ data = $ this ->performWebserviceRequest (
43+ $ endpoint ->getMethod (),
44+ $ endpoint ->getUrl (),
45+ ...$ arguments
2846 );
2947
30- return HealthCheckResponse::from ($ healthData ['data ' ]['attributes ' ]);
48+ $ responseClass = $ endpoint ->getResponseClass ();
49+
50+ return $ responseClass ::from ($ data );
3151 }
3252
3353 public function performExtractionRequest (array $ requestData ): array
3454 {
3555 $ request = new Request (
3656 'POST ' ,
37- $ this ->baseUrl . 'extract.php '
57+ $ this ->baseUrl . '/ extract.php '
3858 );
3959
4060 $ data ['password ' ] = $ this ->key ;
@@ -55,12 +75,12 @@ public function performExtractionRequest(array $requestData): array
5575
5676 protected function performWebserviceRequest (
5777 HttpMethod $ method ,
58- WebserviceEndpoint $ endpoint ,
78+ string $ endpoint ,
5979 array $ requestData = []
6080 ): array {
6181 $ request = new Request (
6282 $ method ->name ,
63- $ this ->baseUrl . $ endpoint-> value ,
83+ $ this ->baseUrl . $ endpoint ,
6484 [
6585 'X-JUpdate-Token ' => $ this ->key
6686 ]
@@ -85,7 +105,7 @@ protected function performWebserviceRequest(
85105 );
86106 }
87107
88- return $ responseData ;
108+ return $ responseData[ ' data ' ][ ' attributes ' ] ;
89109 }
90110
91111 protected function performHttpRequest (
0 commit comments