Skip to content

Commit

Permalink
Added the option to throw exceptions for errors or just return as array.
Browse files Browse the repository at this point in the history
  • Loading branch information
B3none committed Feb 16, 2018
1 parent e6d42a3 commit 2b6dd34
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
26 changes: 23 additions & 3 deletions src/EMTClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@ class EMTClient

const DEFAULT_SESSION_ID = "hoetyz55hfy5t1554cepm32i";

/**
* @var bool
*/
protected $throwException;

/**
* @var Client
*/
protected $client;

public function __construct()
public function __construct(bool $throwException = false)
{
$this->throwException = $throwException;
$this->client = new Client(['cookies' => true]);
}

Expand Down Expand Up @@ -110,9 +116,23 @@ protected function processResponse(string $result) : array
protected function formatAndValidateResponse(array $response) : array
{
if ($response['originnotfound']) {
throw new \Exception("The origin station was not found.");
if ($this->throwException) {
throw new \Exception('The origin station was not found.');
} else {
return ['errorMessage' => 'The origin station was not found.'];
}
} else if ($response['destnotfound']) {
throw new \Exception("The destination station was not found.");
if ($this->throwException) {
throw new \Exception('The destination station was not found.');
} else {
return ['errorMessage' => 'The destination station was not found.'];
}
} else if (!$response['buses'] && !$response['trains']) {
if ($this->throwException) {
throw new \Exception('There are no trains or buses.');
} else {
return ['errorMessage' => 'There are no trains or buses.'];
}
}

foreach ($response as $paramKey => $responseParam) {
Expand Down
3 changes: 1 addition & 2 deletions tests/EMTClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ class EMTClientTest extends TestCase
public function test()
{
$client = new EMTClient();
$testData = $client->getJourneys('Derby', 'Nottingham');

$testData = $client->getJourneys('Derby', 'Burton-Upon-Trent');
$this->assertTrue(is_array($testData));
}
}

0 comments on commit 2b6dd34

Please sign in to comment.