summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFbenas <philbeansburton@gmail.com>2018-05-07 15:02:39 +0100
committerFbenas <philbeansburton@gmail.com>2018-05-07 15:02:39 +0100
commit8350b6bd36d3a4bb3a99dfbf67e746f4f815927c (patch)
treed7cf04d2343ca031a8f241f204e4227eb6b48016
parent33f00442f1667d57f94433834e0da4985670025b (diff)
Add some simple exception handling for bad inputs
-rw-r--r--src/Model/Collection.php14
-rw-r--r--src/Script/Console.php15
-rw-r--r--src/Script/Input.php8
-rw-r--r--src/Script/Output.php6
4 files changed, 30 insertions, 13 deletions
diff --git a/src/Model/Collection.php b/src/Model/Collection.php
index 803e777..62bbf98 100644
--- a/src/Model/Collection.php
+++ b/src/Model/Collection.php
@@ -8,7 +8,6 @@ use Exception;
use Iterator;
use App\Script\Input;
use DateTime;
-use DateTimeZone;
use Countable;
/**
@@ -192,19 +191,18 @@ class Collection implements ArrayAccess, Iterator, Countable
if ($covers = (int) $input->getOption('c')) {
$this->filterByCovers($covers);
}
-
-
- // day - delivery day (dd/mm/yy)
- // time - delivery time in 24h format (hh:mm)
- // location - delivery location (postcode without spaces, e.g. NW43QB)
- // covers - number of people to feed
}
public function filterByDateTime(string $date, string $time)
{
$dateTime = DateTime::createFromFormat('d/m/y G:i', $date . ' ' . $time);
- $dateTime->setTImezone(new DateTImeZone('Europe/London'));
+ if (!$dateTime) {
+ throw new Exception(
+ 'Unable to parse datetime. Required time format: `G:i` (e.g. 05:30). '
+ . 'Required date format: `d/m/y` (e.g. 22/06/18)'
+ );
+ }
$out = [];
foreach ($this->models as $model) {
if ($model->checkDate($dateTime)) {
diff --git a/src/Script/Console.php b/src/Script/Console.php
index e1d7df5..3228750 100644
--- a/src/Script/Console.php
+++ b/src/Script/Console.php
@@ -5,6 +5,7 @@ namespace App\Script;
use App\Model\Vendor;
use App\Script\Input;
use App\Script\Output;
+use Exception;
/**
* Main application class
@@ -44,12 +45,16 @@ class Console
*/
public function exec()
{
- $input = new Input;
- $vendors = Vendor::loadAll($input->getOption('f'));
+ $output = new Output;
+ try {
+ $input = new Input;
+ $vendors = Vendor::loadAll($input->getOption('f'));
- $vendors->filterByInput($input);
+ $vendors->filterByInput($input);
- $output = new Output;
- $output->printCollection($vendors);
+ $output->printCollection($vendors);
+ } catch (Exception $e) {
+ $output->printException($e);
+ }
}
}
diff --git a/src/Script/Input.php b/src/Script/Input.php
index 4e437dd..772bb8c 100644
--- a/src/Script/Input.php
+++ b/src/Script/Input.php
@@ -63,6 +63,14 @@ class Input
if ((array_key_exists('d', $this->options) <=> array_key_exists('t', $this->options)) !== 0) {
throw new Exception('Both day and time options (`-d` and `-t`) are required for time based filtering');
}
+
+ if (array_key_exists('c', $this->options) && !is_numeric($this->options['c'])) {
+ throw new Exception('Value for option `-c` must be an integer');
+ }
+
+ if (array_key_exists('l', $this->options) && is_numeric($this->options['l'])) {
+ throw new Exception('Value for option `-l` must be a string');
+ }
}
/**
diff --git a/src/Script/Output.php b/src/Script/Output.php
index 8f7a689..e61cff8 100644
--- a/src/Script/Output.php
+++ b/src/Script/Output.php
@@ -3,6 +3,7 @@
namespace App\Script;
use App\Model\Collection;
+use Exception;
class Output
{
@@ -20,4 +21,9 @@ class Output
echo implode("\n\n", $out) . "\n";
}
+
+ public function printException(Exception $e)
+ {
+ echo "An unexpected exception occured:\n" . $e->getMessage() . "\n";
+ }
}