1
0
Fork 0
mirror of https://codeberg.org/demostf/api.git synced 2026-06-03 09:54:17 +02:00
api/src/Demo/RawParser.php
2017-07-30 15:57:14 +02:00

42 lines
1.2 KiB
PHP

<?php
declare(strict_types=1);
namespace Demostf\API\Demo;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
/**
* Wrapper around demo.js parser.
*
* Doesn't do any post-processing on the result
*/
class RawParser {
/** @var string */
private $parserUrl;
public function __construct(string $parserUrl) {
$this->parserUrl = $parserUrl;
}
public function parse(string $path): ?array {
try {
$client = new Client();
$response = $client->post($this->parserUrl, [
'body' => fopen($path, 'r'),
]);
$result = json_decode($response->getBody()->getContents(), true);
if (null === $result) {
throw new \Exception('Failed to parse demo, unexpected result from parser');
} else {
return $result;
}
} catch (RequestException $e) {
if (strpos($e->getMessage(), 'cURL error 52') !== false) {
throw new \Exception('Failed to parse demo, can\'t reach demo parser');
}
throw new \Exception('Failed to parse demo, ' . $e->getMessage());
}
}
}