Skip to content

Commit

Permalink
feat: add teaching sets
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Mitchell committed Sep 16, 2022
1 parent 5d0e855 commit 4d312f1
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/Controllers/TeachingSetController.php
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);
}
}
26 changes: 26 additions & 0 deletions src/Wrappers/TeachingSet.php
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;
}
}
33 changes: 33 additions & 0 deletions src/Wrappers/TeachingSetList.php
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();
}
}

0 comments on commit 4d312f1

Please sign in to comment.