Skip to content

Commit

Permalink
EA-5554: Update to deps and code to PHP8.2
Browse files Browse the repository at this point in the history
* dependencies update
* add rector for easy code updates
  • Loading branch information
sleipi committed Nov 30, 2023
1 parent 41f04c6 commit fd17a38
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
10 changes: 7 additions & 3 deletions src/HeartbeatApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,19 @@
/**
* HeartbeatApiClient constructor.
*/
public function __construct(private \JTL\OpsGenie\Client\HttpClient $client)
public function __construct(private HttpClient $client)
{
}

/**
* @return PingResponse|OpsGenieResponse
* @param PingRequest $request
* @return PingResponse&OpsGenieResponse
* @throws Exception\ApiRequestFailException
*/
public function sendPing(PingRequest $request): PingResponse
{
return $this->client->request($request, PingResponse::class);
/** @var PingResponse $response */
$response = $this->client->request($request, PingResponse::class);
return $response;
}
}
24 changes: 20 additions & 4 deletions src/HttpClient.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* This File is part of JTL-Software
*
Expand All @@ -14,6 +15,7 @@
use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\RequestOptions;
use JsonException;
use JTL\OpsGenie\Client\Exception\ApiRequestFailException;
use Psr\Http\Message\ResponseInterface;
use RuntimeException;
Expand Down Expand Up @@ -71,7 +73,6 @@ public function request(OpsGenieRequest $request, string $responseType): OpsGeni
];

if ($request->getHttpMethod() !== "GET") {

//add default empty body as default because php will otherwise create a [] as body
$option['body'] = "{}";

Expand All @@ -82,7 +83,7 @@ public function request(OpsGenieRequest $request, string $responseType): OpsGeni

$response = $this->client->request($request->getHttpMethod(), $request->getUrl(), $option);
} catch (BadResponseException $e) {
$response = $e->getResponse();
$response = $e->getResponse();
} catch (GuzzleException|Exception $e) {
$msg = $e::class . ": " . $e->getMessage();
throw new ApiRequestFailException($msg, $e->getCode(), $e);
Expand All @@ -92,15 +93,30 @@ public function request(OpsGenieRequest $request, string $responseType): OpsGeni
}

/**
* @param ResponseInterface $response
* @param string $objectName
* @return OpsGenieResponse
* @throws JsonException
*/
public function createResponse(ResponseInterface $response, string $objectName): OpsGenieResponse
private function createResponse(ResponseInterface $response, string $objectName): OpsGenieResponse
{
return new $objectName($response->getStatusCode(), $this->getBodyAsArray((string)$response->getBody()));
$responseObject = new $objectName(
$response->getStatusCode(),
$this->getBodyAsArray((string)$response->getBody())
);
if ($responseObject instanceof OpsGenieResponse) {
return $responseObject;
}

throw new RuntimeException(
"Response object must be an instance of " . OpsGenieResponse::class . " but got " . $objectName
);
}

/**
* @param string $body
* @return array
* @throws JsonException
*/
protected function getBodyAsArray(string $body): array
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Alert/AlertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function testAppendTag(): void
{
$alert = new Alert("entity", 'alias', "message", "source");
$alert->appendTag('foo')
->appendTag(2);
->appendTag('2');

$this->assertCount(2, $alert->getTags());
$this->assertEquals(['foo', '2'], $alert->getTags());
Expand Down

0 comments on commit fd17a38

Please sign in to comment.