Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
fredbradley authored Oct 15, 2021
2 parents 021afb9 + 7efce45 commit 48119a8
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 33 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/AdmissionApplicantContactController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
50 changes: 39 additions & 11 deletions src/Controllers/SchoolTermsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}
8 changes: 4 additions & 4 deletions src/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
4 changes: 0 additions & 4 deletions src/Exceptions/FailedtoFindNearestTerm.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,4 @@

class FailedtoFindNearestTerm extends \Exception
{
public function __construct($message = '')
{
$this->message = $message;
}
}
19 changes: 8 additions & 11 deletions src/Facade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php


namespace spkm\isams;

use Illuminate\Support\Str;
Expand All @@ -10,10 +9,7 @@

class Facade
{
/**
*
*/
private const CONTROLLER_NAMESPACE = "spkm\\isams\\Controllers\\";
private const CONTROLLER_NAMESPACE = 'spkm\\isams\\Controllers\\';
/**
* @var Institution
*/
Expand Down Expand Up @@ -46,11 +42,12 @@ public function endpoint(string $controller): self
{
$controllerClass = $this->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
*
Expand All @@ -67,10 +64,10 @@ private function getController(string $controllerClassName): string
return self::CONTROLLER_NAMESPACE . $controllerClassName;
}

throw new ControllerNotFound(
"Could not find Controller: " . $controllerClassName,
500
);

throw new ControllerNotFound('Could not find Controller: ' . $controllerClassName,
500);

}

/**
Expand All @@ -83,7 +80,7 @@ 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);
Expand Down
1 change: 0 additions & 1 deletion src/Facades/Isams.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php


namespace spkm\isams\Facades;

use Illuminate\Support\Facades\Facade;
Expand Down
1 change: 1 addition & 0 deletions src/IsamsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function boot(): void
__DIR__ . '/Config/config.php' => config_path('isams.php'),
], 'config');
}

public function register()
{
$this->app->bind('isams', function ($app) {
Expand Down

0 comments on commit 48119a8

Please sign in to comment.