Skip to content

Commit

Permalink
Check for exceptions at each chunk stage
Browse files Browse the repository at this point in the history
  • Loading branch information
vekien committed Nov 19, 2020
1 parent e8389ac commit 9b41bd0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
37 changes: 19 additions & 18 deletions src/Lodestone/Http/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Lodestone\Parser\Parser;
use Ramsey\Uuid\Uuid;
use Symfony\Component\HttpClient\CurlHttpClient;
use Symfony\Component\HttpClient\Exception\ClientException;

class Http
{
Expand Down Expand Up @@ -106,28 +105,30 @@ public function settle()
$responses = AsyncHandler::get();

foreach ($client->stream($responses) as $response => $chunk) {
// grab the user data
$userdata = $response->getInfo('user_data');

// grab request id
$requestId = $userdata['request_id'];

// if it wasn't a 200, return error
if ($response->getStatusCode() != 200) {
$content[$requestId] = (Object)[
'Error' => true,
'StatusCode' => $response->getStatusCode()
];
continue;
}

if ($chunk->isLast()) {
// grab the user data
$userdata = $response->getInfo('user_data');

// grab request id
$requestId = $userdata['request_id'];

// grab the parser class name
/** @var Parser $parser */
$parser = new $userdata['parser']($userdata);

try {
// handle response
$content[$requestId] = $parser->handle(
$response->getContent()
);
} catch (ClientException $ex) {
$content[$requestId] = (Object)[
'Error' => true,
'StatusCode' => $ex->getCode()
];
}
// handle response
$content[$requestId] = $parser->handle(
$response->getContent()
);
}
}

Expand Down
18 changes: 11 additions & 7 deletions src/Lodestone/Parser/ParseCharacterClassJobs.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,15 @@ public function handle(string $html)
if ($fieldname == 'Elemental Level') {
$elementalIndex = 0;
} else {
[$current, $max] = explode('/', $node->find('.character__job__exp')->text());
$current = filter_var(trim(str_ireplace('-', null, $current)) ?: 0, FILTER_SANITIZE_NUMBER_INT);
$bozjanString = trim($node->find('.character__job__exp')->text());

if ($bozjanString) {
[$current, $max] = explode('/', $bozjanString);
$current = filter_var(trim(str_ireplace('-', null, $current)) ?: 0, FILTER_SANITIZE_NUMBER_INT);

$bozjan->Level = (int)$node->find('.character__job__level')->text();
$bozjan->Mettle = $current;
$bozjan->Level = (int)$node->find('.character__job__level')->text();
$bozjan->Mettle = $current;
}
}

//
Expand All @@ -102,9 +106,9 @@ public function handle(string $html)
$elemental = new ClassJobEureka('Elemental Level');
$node = $this->dom->find('.character__job__list')[$elementalIndex];

$currentmax = explode('/', $node->find('.character__job__exp')->text());
$current = $currentmax[0] ?? null;
$max = $currentmax[1] ?? null;
$eurekaString = explode('/', $node->find('.character__job__exp')->text());
$current = $eurekaString[0] ?? null;
$max = $eurekaString[1] ?? null;

$current = filter_var(trim(str_ireplace('-', null, $current)) ?: 0, FILTER_SANITIZE_NUMBER_INT);
$max = filter_var(trim(str_ireplace('-', null, $max)) ?: 0, FILTER_SANITIZE_NUMBER_INT);
Expand Down

0 comments on commit 9b41bd0

Please sign in to comment.