-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Simon Mitchell
committed
Sep 16, 2022
1 parent
5d0e855
commit 4d312f1
Showing
3 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace spkm\isams\Controllers; | ||
|
||
use Exception; | ||
use GuzzleHttp\Exception\GuzzleException; | ||
use Illuminate\Http\JsonResponse; | ||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\Facades\Cache; | ||
use spkm\isams\Endpoint; | ||
use spkm\isams\Wrappers\TeachingSet; | ||
use spkm\isams\Wrappers\TeachingSetList; | ||
|
||
class TeachingSetController extends Endpoint | ||
{ | ||
/** | ||
* Set the URL the request is made to. | ||
* | ||
* @return void | ||
* | ||
* @throws Exception | ||
*/ | ||
protected function setEndpoint(): void | ||
{ | ||
$this->endpoint = $this->getDomain() . '/api/teaching/sets'; | ||
} | ||
|
||
/** | ||
* Display a listing of the resource. | ||
* | ||
* @return Collection | ||
* | ||
* @throws GuzzleException | ||
*/ | ||
public function index(): Collection | ||
{ | ||
$key = $this->institution->getConfigName() . 'teachingSubjects.index'; | ||
|
||
$response = $this->guzzle->request('GET', $this->endpoint, ['headers' => $this->getHeaders()]); | ||
|
||
return Cache::remember($key, $this->getCacheDuration(), function () use ($response) { | ||
return $this->wrapJson($response->getBody()->getContents(), 'sets', TeachingSet::class); | ||
}); | ||
} | ||
|
||
/** | ||
* Show the specified resource. | ||
* | ||
* @param int $id | ||
* @return TeachingSetList | ||
* | ||
* @throws GuzzleException | ||
*/ | ||
public function show(int $id): TeachingSetList | ||
{ | ||
$response = $this->guzzle->request('GET', $this->endpoint . '/' . $id .'/setList', ['headers' => $this->getHeaders()]); | ||
|
||
$decoded = json_decode($response->getBody()->getContents()); | ||
|
||
return new TeachingSetList($decoded); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace spkm\isams\Wrappers; | ||
|
||
use spkm\isams\Wrapper; | ||
|
||
/** | ||
* Wrapper for the array returned by the iSAMS REST API endpoint. | ||
*/ | ||
class TeachingSet extends Wrapper | ||
{ | ||
/* | ||
* @var bool | ||
*/ | ||
protected $isHidden; | ||
|
||
/** | ||
* Handle the data. | ||
* | ||
* @return void | ||
*/ | ||
protected function handle(): void | ||
{ | ||
$this->isHidden = (bool) optional($this->item)->hidden; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace spkm\isams\Wrappers; | ||
|
||
use spkm\isams\Wrapper; | ||
|
||
/** | ||
* Wrapper for the array returned by the iSAMS REST API endpoint. | ||
*/ | ||
class TeachingSetList extends Wrapper | ||
{ | ||
/* | ||
* @var bool | ||
*/ | ||
protected $isHidden; | ||
|
||
protected $item; | ||
|
||
/** | ||
* Handle the data. | ||
* | ||
* @return void | ||
*/ | ||
protected function handle(): void | ||
{ | ||
$this->isHidden = (bool) optional($this->item)->hidden; | ||
$this->item->students = collect($this->students)->map(function($item){ | ||
return $item->schoolId; | ||
}); | ||
unset($this->students); | ||
$this->students = $this->item->students->toArray(); | ||
} | ||
} |