Skip to content

Commit

Permalink
fix(SimpleXMLElement): factory throw unify exception
Browse files Browse the repository at this point in the history
  • Loading branch information
h4kuna committed Jul 22, 2024
1 parent f5ad474 commit 8c5772a
Show file tree
Hide file tree
Showing 7 changed files with 262 additions and 11 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"phpstan/phpstan": "^1.10",
"phpstan/phpstan-deprecation-rules": "^1.1.3",
"phpstan/phpstan-strict-rules": "^1.5",
"strictphp/http-clients": "^0.1.4",
"tracy/tracy": "^2.10"
},
"autoload": {
Expand All @@ -41,7 +42,7 @@
"guzzlehttp/psr7": "Minimum ^2.4 for guzzle.",
"h4kuna/dir": "If you want to use build-in factory.",
"nette/caching": "If you have not own PSR-6 implementation.",
"ext-simplexml": "If you want to use h4kuna\\Exchange\\Driver\\Ecb."
"ext-simplexml": "If you want to use h4kuna\\Exchange\\Driver\\Ecb or h4kuna\\Exchange\\Driver\\RB."
},
"autoload-dev": {
"psr-4": {
Expand Down
5 changes: 1 addition & 4 deletions src/Driver/Ecb/Day.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ public function getTimeZone(): DateTimeZone

public function createSourceData(ResponseInterface $response): SourceData
{
$xml = simplexml_load_string($response->getBody()->getContents());
if ($xml === false) {
throw new Exchange\Exceptions\InvalidStateException('Invalid source xml.');
}
$xml = Exchange\Utils::createSimpleXMLElement($response);

// including EUR
$eur = $xml->Cube->Cube->addChild('Cube');
Expand Down
7 changes: 1 addition & 6 deletions src/Driver/RB/Day.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,7 @@ public function getTimeZone(): DateTimeZone

public function createSourceData(ResponseInterface $response): SourceData
{
$data = $response->getBody()->getContents();
$xml = simplexml_load_string($data);

if ($xml === false) {
throw new InvalidStateException('Invalid source xml.');
}
$xml = Utils::createSimpleXMLElement($response);

// add CZK
$czk = $xml->exchangeRateList->exchangeRates->addChild('exchangeRate');
Expand Down
11 changes: 11 additions & 0 deletions src/Exceptions/XmlResponseFailedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types=1);

namespace h4kuna\Exchange\Exceptions;

use Exception;
use Psr\Http\Client\ClientExceptionInterface;

final class XmlResponseFailedException extends Exception implements ClientExceptionInterface
{

}
15 changes: 15 additions & 0 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
use DateTimeInterface;
use DateTimeZone;
use h4kuna\Exchange\Exceptions\InvalidStateException;
use h4kuna\Exchange\Exceptions\XmlResponseFailedException;
use Nette\StaticClass;
use Nette\Utils\DateTime as NetteDateTime;
use Psr\Http\Message\ResponseInterface;
use SimpleXMLElement;

final class Utils
{
Expand All @@ -29,6 +32,18 @@ public static function stroke2point(string $str): string
}


public static function createSimpleXMLElement(ResponseInterface $response): SimpleXMLElement
{
$xml = @simplexml_load_string($response->getBody()->getContents());

if ($xml === false) {
throw new XmlResponseFailedException((string) $response->getStatusCode());
}

return $xml;
}


public static function createTimeZone(string|DateTimeZone $timeZone): DateTimeZone
{
return is_string($timeZone) ? new DateTimeZone($timeZone) : $timeZone;
Expand Down
221 changes: 221 additions & 0 deletions tests/Fixtures/rb.failed.no.xml.html

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions tests/src/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use DateTimeImmutable;
use DateTimeInterface;
use DateTimeZone;
use GuzzleHttp\Psr7\Response;
use h4kuna\Exchange\Exceptions\XmlResponseFailedException;
use h4kuna\Exchange\Utils;
use Tester\Assert;
use Tester\TestCase;
Expand Down Expand Up @@ -177,6 +179,15 @@ public function assertToImmutable(
Assert::same($expected, Utils::toImmutable($date, new DateTimeZone('Europe/Prague'))?->format(DateTimeInterface::RFC3339));
}


public function testCreateSimpleXMLElement(): void
{
$response = new Response(403, body: (string) file_get_contents(__DIR__ . '/../Fixtures/rb.failed.no.xml.html'));

Assert::exception(static fn (
) => Utils::createSimpleXMLElement($response), XmlResponseFailedException::class, '403');
}

}

(new UtilsTest())->run();

0 comments on commit 8c5772a

Please sign in to comment.