From 65aaf8175d020cd000c55640cfb6ee0d51cd362c Mon Sep 17 00:00:00 2001 From: Fred Bradley Date: Thu, 29 Jul 2021 08:35:23 +0100 Subject: [PATCH 1/4] additional methods to SchoolTermsController --- src/Controllers/SchoolTermsController.php | 50 +++++++++++++++++----- src/Exceptions/FailedtoFindNearestTerm.php | 5 +-- 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/src/Controllers/SchoolTermsController.php b/src/Controllers/SchoolTermsController.php index f8b220f..392cc39 100644 --- a/src/Controllers/SchoolTermsController.php +++ b/src/Controllers/SchoolTermsController.php @@ -3,6 +3,7 @@ namespace spkm\isams\Controllers; use Carbon\Carbon; +use Carbon\CarbonInterface; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Cache; use spkm\isams\Endpoint; @@ -35,18 +36,22 @@ protected function setEndpoint(): void */ public function thisTerm(): object { - Cache::forget('termDatesThisTerm_' . $this->institution->short_code); - - return Cache::remember('termDatesThisTerm_' . $this->institution->short_code, now()->addWeek(), function () { - $currentTerm = $this->getCurrentTerm(); - array_walk($currentTerm, function (&$item, $key) { - if ($key == 'startDate' || $key == 'finishDate') { - $item = Carbon::parse($item); - } + try { + return Cache::remember('termDatesThisTerm_'.$this->institution->short_code, now()->addWeek(), function () { + $currentTerm = $this->getCurrentTerm(); + array_walk($currentTerm, function (&$item, $key) { + if ($key == 'startDate' || $key == 'finishDate') { + $item = Carbon::parse($item); + } + }); + + return $currentTerm; }); + } catch (FailedtoFindNearestTerm $exception) { + Cache::forget('termDatesThisTerm_'.$this->institution->short_code); - return $currentTerm; - }); + return $this->thisTerm(); + } } /** @@ -112,7 +117,7 @@ public function getYear(int $year): Collection /** * @return SchoolTerm * - * @throws \App\Exceptions\FailedToFindNearestTerm + * @throws \spkm\isams\Exceptions\FailedtoFindNearestTerm * @throws \GuzzleHttp\Exception\GuzzleException */ private function getNearestTerm(): SchoolTerm @@ -149,4 +154,27 @@ private function getNearestTerm(): SchoolTerm throw new FailedtoFindNearestTerm('Could not find nearest term', 500); } } + + /** + * @param \Carbon\CarbonInterface $date + * + * @return bool + * @throws \GuzzleHttp\Exception\GuzzleException + */ + public function isDateInTermTime(CarbonInterface $date): bool + { + $list = $this->index(); + + $lastYear = $date->format('Y') - 1; + + $whittledDownList = $list->where('schoolYear', '>=', $lastYear); // purpose here is to limit the foreach loop + + foreach ($whittledDownList as $term) { + if ($date > $term->startDate && $date < $term->finishDate->addDay()) { + return true; + } + } + + return false; + } } diff --git a/src/Exceptions/FailedtoFindNearestTerm.php b/src/Exceptions/FailedtoFindNearestTerm.php index 2fde489..3555771 100644 --- a/src/Exceptions/FailedtoFindNearestTerm.php +++ b/src/Exceptions/FailedtoFindNearestTerm.php @@ -4,8 +4,5 @@ class FailedtoFindNearestTerm extends \Exception { - public function __construct($message = '') - { - $this->message = $message; - } + } From 81e03c0627bb57ff8208bcd313c56da799970a6d Mon Sep 17 00:00:00 2001 From: Fred Bradley Date: Thu, 29 Jul 2021 07:51:43 +0000 Subject: [PATCH 2/4] Apply fixes from StyleCI [ci skip] [skip ci] --- .../AdmissionApplicantContactController.php | 2 +- .../AdmissionApplicantController.php | 2 +- src/Endpoint.php | 8 ++++---- src/Exceptions/ControllerNotFound.php | 1 - src/Exceptions/MethodNotFound.php | 1 - src/Facade.php | 20 +++++++------------ src/Facades/Isams.php | 2 -- src/IsamsServiceProvider.php | 3 ++- 8 files changed, 15 insertions(+), 24 deletions(-) diff --git a/src/Controllers/AdmissionApplicantContactController.php b/src/Controllers/AdmissionApplicantContactController.php index 99e5781..ee511fb 100644 --- a/src/Controllers/AdmissionApplicantContactController.php +++ b/src/Controllers/AdmissionApplicantContactController.php @@ -2,8 +2,8 @@ namespace spkm\isams\Controllers; -use Illuminate\Http\Response; use Illuminate\Http\JsonResponse; +use Illuminate\Http\Response; use Illuminate\Support\Collection; use spkm\isams\Endpoint; use spkm\isams\Wrappers\PupilContact; diff --git a/src/Controllers/AdmissionApplicantController.php b/src/Controllers/AdmissionApplicantController.php index 667ab4a..9838959 100644 --- a/src/Controllers/AdmissionApplicantController.php +++ b/src/Controllers/AdmissionApplicantController.php @@ -16,7 +16,7 @@ class AdmissionApplicantController extends Endpoint * @return void * @throws \Exception */ - protected function setEndpoint():void + protected function setEndpoint(): void { $this->endpoint = $this->getDomain() . '/api/admissions/applicants'; } diff --git a/src/Endpoint.php b/src/Endpoint.php index fe7b2db..c89bd23 100644 --- a/src/Endpoint.php +++ b/src/Endpoint.php @@ -179,13 +179,13 @@ protected function response(int $expectedStatusCode, $response, $data, array $er 'errors' => $errors, ]; - if (isset($response->getHeaders()[ 'Location' ])) { - $location = $response->getHeaders()[ 'Location' ][ 0 ]; + if (isset($response->getHeaders()['Location'])) { + $location = $response->getHeaders()['Location'][0]; $id = ltrim(str_replace($this->endpoint, '', $location), '\//'); - $json[ 'location' ] = $location; + $json['location'] = $location; if (! empty($id)) { - $json[ 'id' ] = $id; + $json['id'] = $id; } } diff --git a/src/Exceptions/ControllerNotFound.php b/src/Exceptions/ControllerNotFound.php index 0cb56db..7d78205 100644 --- a/src/Exceptions/ControllerNotFound.php +++ b/src/Exceptions/ControllerNotFound.php @@ -4,5 +4,4 @@ class ControllerNotFound extends \Exception { - } diff --git a/src/Exceptions/MethodNotFound.php b/src/Exceptions/MethodNotFound.php index db27913..847010c 100644 --- a/src/Exceptions/MethodNotFound.php +++ b/src/Exceptions/MethodNotFound.php @@ -4,5 +4,4 @@ class MethodNotFound extends \Exception { - } diff --git a/src/Facade.php b/src/Facade.php index 2340273..7841402 100644 --- a/src/Facade.php +++ b/src/Facade.php @@ -1,9 +1,7 @@ getController($controller); $this->controller = new $controllerClass($this->institution); + return $this; } /** - * Sanatizes the controller name for us, so people can use ::class notation if they wish + * Sanatizes the controller name for us, so people can use ::class notation if they wish. * * @param string $controllerClassName * @@ -64,11 +60,11 @@ private function getController(string $controllerClassName): string return $controllerClassName; } - if (class_exists(self::CONTROLLER_NAMESPACE.$controllerClassName)) { - return self::CONTROLLER_NAMESPACE.$controllerClassName; + if (class_exists(self::CONTROLLER_NAMESPACE . $controllerClassName)) { + return self::CONTROLLER_NAMESPACE . $controllerClassName; } - throw new ControllerNotFound("Could not find Controller: ".$controllerClassName, + throw new ControllerNotFound('Could not find Controller: ' . $controllerClassName, 500); } @@ -82,11 +78,9 @@ private function getController(string $controllerClassName): string public function callMethod(string $method, array $args = []) { if (! method_exists($this->controller, $method)) { - throw new MethodNotFound("Method ".$method." not found on ".get_class($this->controller)); + throw new MethodNotFound('Method ' . $method . ' not found on ' . get_class($this->controller)); } return call_user_func_array([$this->controller, $method], $args); } - - } diff --git a/src/Facades/Isams.php b/src/Facades/Isams.php index 5ffa5d5..d72a8a0 100644 --- a/src/Facades/Isams.php +++ b/src/Facades/Isams.php @@ -1,9 +1,7 @@ config_path('isams.php'), ], 'config'); } + public function register() { - $this->app->bind('isams', function($app) { + $this->app->bind('isams', function ($app) { return new Facade(); }); } From 80f827f072e883709b100b80808fee8267e49bda Mon Sep 17 00:00:00 2001 From: Fred Bradley Date: Thu, 29 Jul 2021 07:55:48 +0000 Subject: [PATCH 3/4] Apply fixes from StyleCI [ci skip] [skip ci] --- src/Controllers/SchoolTermsController.php | 4 ++-- src/Exceptions/FailedtoFindNearestTerm.php | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Controllers/SchoolTermsController.php b/src/Controllers/SchoolTermsController.php index 392cc39..6b8a2d5 100644 --- a/src/Controllers/SchoolTermsController.php +++ b/src/Controllers/SchoolTermsController.php @@ -37,7 +37,7 @@ protected function setEndpoint(): void public function thisTerm(): object { try { - return Cache::remember('termDatesThisTerm_'.$this->institution->short_code, now()->addWeek(), function () { + return Cache::remember('termDatesThisTerm_' . $this->institution->short_code, now()->addWeek(), function () { $currentTerm = $this->getCurrentTerm(); array_walk($currentTerm, function (&$item, $key) { if ($key == 'startDate' || $key == 'finishDate') { @@ -48,7 +48,7 @@ public function thisTerm(): object return $currentTerm; }); } catch (FailedtoFindNearestTerm $exception) { - Cache::forget('termDatesThisTerm_'.$this->institution->short_code); + Cache::forget('termDatesThisTerm_' . $this->institution->short_code); return $this->thisTerm(); } diff --git a/src/Exceptions/FailedtoFindNearestTerm.php b/src/Exceptions/FailedtoFindNearestTerm.php index 3555771..8b23001 100644 --- a/src/Exceptions/FailedtoFindNearestTerm.php +++ b/src/Exceptions/FailedtoFindNearestTerm.php @@ -4,5 +4,4 @@ class FailedtoFindNearestTerm extends \Exception { - } From 7efce45153e0870c6aa43281dc981c6f284166fa Mon Sep 17 00:00:00 2001 From: Fred Bradley Date: Fri, 6 Aug 2021 14:09:31 +0100 Subject: [PATCH 4/4] Update readme.md Correct code snippet to reference new type hint that was added in in version v2.3.10 --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index c63df15..d3cc3d6 100644 --- a/readme.md +++ b/readme.md @@ -42,7 +42,7 @@ class School extends Model implements \spkm\isams\Contracts\Institution /** * Define the name used to identify this Schools entry in the config */ - public function getConfigName() + public function getConfigName(): string { return 'cranleighSandbox'; }