Skip to content
114 changes: 103 additions & 11 deletions src/Classes/Api/V1/HttpRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace RobinTheHood\ModifiedModuleLoaderClient\Api\V1;

use RuntimeException;
use RobinTheHood\ModifiedModuleLoaderClient\Logger\LogLevel;
use RobinTheHood\ModifiedModuleLoaderClient\Logger\StaticLogger;

Expand All @@ -27,7 +28,107 @@ public function isServerAvailable(string $url): bool
return false;
}

public function sendPostRequest(string $url, $data)
public function sendPostRequest(string $url, $data): string
{
$result = $this->sendCurlPostRequest($url, $data);
return $result;
}


public function sendGetRequest(string $url): string
{
$result = $this->sendCurlGetRequest($url);
return $result;
}

public static function createQuery(array $queryValues): string
{
$query = '';
foreach ($queryValues as $name => $value) {
$query .= $name . '=' . urlencode($value) . '&';
}
return $query;
}

/**
* @throws RuntimeException
*/
private function sendCurlGetRequest(string $url): string
{
// HTTP GET-Request konfigurieren
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERAGENT, 'Modified Module Loader Client');

StaticLogger::log(
LogLevel::DEBUG,
"Send GET request to $url"
);

// Request senden und Ergebnis erhalten
$result = curl_exec($curl);

// HTTP-Statuscode und Fehler prüfen
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($result === false) {
$error = curl_error($curl);
curl_close($curl);
StaticLogger::log(LogLevel::ERROR, "$httpCode Error-Response from $url\n$error");
throw new RuntimeException('Fehler beim Senden des GET-Requests: ' . $error);
} elseif ($httpCode < 200 || $httpCode >= 300) {
curl_close($curl);
StaticLogger::log(LogLevel::ERROR, "$httpCode Error-Response from $url");
throw new RuntimeException('Fehler beim Senden des GET-Requests: HTTP-Statuscode ' . $httpCode);
}

// Request beenden
curl_close($curl);

StaticLogger::log(LogLevel::DEBUG, "$httpCode Response from $url\n" . print_r($result, true));

return (string) $result;
}

/**
* @throws RuntimeException
*/
private function sendCurlPostRequest(string $url, $data): string
{
// HTTP POST-Request konfigurieren
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERAGENT, 'Modified Module Loader Client');

StaticLogger::log(
LogLevel::DEBUG,
"Send POST request to $url\n[DATA]\n" . print_r($data, true)
);

// Request senden und Ergebnis erhalten
$result = curl_exec($curl);

// HTTP-Statuscode und Fehler prüfen
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($result === false) {
$error = curl_error($curl);
curl_close($curl);
StaticLogger::log(LogLevel::ERROR, "$httpCode Error-Response from $url\n$error");
throw new RuntimeException('Fehler beim Senden des POST-Requests: ' . $error);
} elseif ($httpCode < 200 || $httpCode >= 300) {
curl_close($curl);
StaticLogger::log(LogLevel::ERROR, "$httpCode Error-Response from $url");
throw new RuntimeException('Fehler beim Senden des POST-Requests: HTTP-Statuscode ' . $httpCode);
}

// Request beenden
curl_close($curl);

return (string) $result;
}

private function sendFileGetContentsPostRequest(string $url, $data): string
{
// http verwenden, auch wenn die Url mit https://... beginnt
$options = [
Expand Down Expand Up @@ -56,7 +157,7 @@ public function sendPostRequest(string $url, $data)
return $result;
}

public function sendGetRequest($url)
private function sendFileGetContentsGetRequest(string $url): string
{
// http verwenden, auch wenn die Url mit https://... beginnt
$options = [
Expand All @@ -82,13 +183,4 @@ public function sendGetRequest($url)

return $result;
}

public static function createQuery(array $queryValues): string
{
$query = '';
foreach ($queryValues as $name => $value) {
$query .= $name . '=' . urlencode($value) . '&';
}
return $query;
}
}
43 changes: 43 additions & 0 deletions tests/unit/HttpRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

/*
* This file is part of MMLC - ModifiedModuleLoaderClient.
*
* (c) Robin Wieschendorf <mail@robinwieschendorf.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace RobinTheHood\ModifiedModuleLoaderClient\Tests\Unit;

use PHPUnit\Framework\TestCase;
use RobinTheHood\ModifiedModuleLoaderClient\Api\V1\HttpRequest;

class HttpRequestTest extends TestCase
{
public function testSendPostRequestSuccess()
{
$httpClient = new HttpRequest();
$response = $httpClient->sendPostRequest('https://postman-echo.com/post', ['foo' => 'bar']);

$this->assertNotNull($response);
$this->assertIsString($response);
$this->assertStringContainsString('application/x-www-form-urlencoded', $response);
$this->assertStringContainsString('"foo": "bar"', $response);
}

public function testSendPostRequestFailure()
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage(
'Fehler beim Senden des POST-Requests: Could not resolve host: this-url-does-not-exist.com'
);

$httpClient = new HttpRequest();
$response = $httpClient->sendPostRequest('https://this-url-does-not-exist.com', []);
$this->assertIsString($response);
}
}