Skip to content

Commit

Permalink
Merge branch 'czPechy-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
h4kuna committed Oct 3, 2018
2 parents a89ff46 + 4628c81 commit fc07f94
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/Request/Queue.php
Expand Up @@ -6,6 +6,7 @@
h4kuna\Fio,
h4kuna\Fio\Response\Pay,
Nette\Utils;
use Psr\Http\Message\ResponseInterface;

class Queue implements IQueue
{
Expand Down Expand Up @@ -117,6 +118,13 @@ private function request($token, $fallback, $action)
}
self::sleep($tempFile);
$next = true;
} catch (GuzzleHttp\Exception\ServerException $e) {
if($e->hasResponse()) {
self::detectDownloadResponse($e->getResponse());
}
throw self::createServiceUnavailableException();
} catch (GuzzleHttp\Exception\ConnectException $e) {
throw self::createServiceUnavailableException();
}
} while ($next);
fclose($file);
Expand All @@ -129,6 +137,10 @@ private function request($token, $fallback, $action)
return $response->getBody();
}

/**
* @param ResponseInterface $response
* @throws Fio\ServiceUnavailableException
*/
private static function detectDownloadResponse($response)
{
/* @var $contentTypeHeaders array */
Expand Down Expand Up @@ -170,12 +182,18 @@ private static function safeProtocol($filename)
}

/**
* @param $response
* @param ResponseInterface|GuzzleHttp\Psr7\Stream $response
* @return Pay\XMLResponse
*/
private static function createXmlResponse($response)
{
return new Pay\XMLResponse($response->getContents());
return new Pay\XMLResponse($response->getBody()->getContents());
}


private static function createServiceUnavailableException()
{
return new Fio\ServiceUnavailableException('Fio server does not response.');
}

}
1 change: 1 addition & 0 deletions tests/data/tests/server-exception.xml
@@ -0,0 +1 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.fio.cz/schema/response.xsd"> <result> <errorCode>15</errorCode> <status>error</status> <message>Služba je momentálně nedostupná</message> </result> </response>
24 changes: 24 additions & 0 deletions tests/src/Response/Read/ServerExceptionTest.phpt
@@ -0,0 +1,24 @@
<?php
namespace h4kuna\Fio;

use Tester,
Tester\Assert,
Salamium\Testinium;

require __DIR__ . '/../../../bootstrap.php';

/**
* @author Martin Pecha
*/
class ServerExceptionTest extends Tester\TestCase
{

public function testResponse()
{
$xml = Testinium\File::load('server-exception.xml');
$xmlResponse = new Response\Pay\XMLResponse($xml);
Assert::false($xmlResponse->isOk());
}
}

(new ServerExceptionTest())->run();

0 comments on commit fc07f94

Please sign in to comment.