|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * |
| 5 | + * OneRoster class |
| 6 | + * |
| 7 | + * @copyright Apereo |
| 8 | + * @category OpenLRW |
| 9 | + * @package Entity |
| 10 | + * @author Xavier Chopin <bonjour@xavierchop.in> |
| 11 | + * @license http://www.osedu.org/licenses/ECL-2.0 ECL-2.0 License |
| 12 | + */ |
| 13 | + |
| 14 | +namespace OpenLRW\Model; |
| 15 | + |
| 16 | +use OpenLRW\OpenLRW; |
| 17 | +use OpenLRW\Exception\InternalServerErrorException; |
| 18 | + |
| 19 | +abstract class OneRoster extends Model |
| 20 | +{ |
| 21 | + |
| 22 | + const PREFIX = 'api/'; |
| 23 | + |
| 24 | + protected static $collection; |
| 25 | + |
| 26 | + /** |
| 27 | + * @return array |
| 28 | + */ |
| 29 | + protected static function generateHeader(): array |
| 30 | + { |
| 31 | + try { |
| 32 | + $jwt = OpenLRW::smartJwt(); |
| 33 | + } catch (\Exception $e) { |
| 34 | + throw new InternalServerErrorException('Impossible to create a JWT. Server might be down or you provided invalid credentials.'); |
| 35 | + } |
| 36 | + |
| 37 | + return [ |
| 38 | + 'X-Requested-With' => 'XMLHttpRequest', |
| 39 | + 'Authorization' => "Bearer $jwt" |
| 40 | + ]; |
| 41 | + } |
| 42 | + |
| 43 | + public static function find($id) |
| 44 | + { |
| 45 | + $header = self::generateHeader(); |
| 46 | + $json = OpenLRW::httpGet(self::PREFIX . static::$collection . "/$id", $header); |
| 47 | + return new static((array)$json); |
| 48 | + } |
| 49 | + |
| 50 | + public static function all() |
| 51 | + { |
| 52 | + $header = self::generateHeader(); |
| 53 | + $json_array = OpenLRW::httpGet(self::PREFIX . static::$collection, $header); |
| 54 | + $results = []; |
| 55 | + foreach ($json_array as $json) { |
| 56 | + $results[] = new static((array)$json); |
| 57 | + } |
| 58 | + |
| 59 | + return $results; |
| 60 | + } |
| 61 | + |
| 62 | + public static function save($data = null) |
| 63 | + { |
| 64 | + if ($data === null) { |
| 65 | + $data = static::toJson(); |
| 66 | + } |
| 67 | + $header = self::generateHeader(); |
| 68 | + return OpenLRW::httpPost(self::PREFIX . static::$collection, $header, $data); |
| 69 | + } |
| 70 | + |
| 71 | + public static function update($id, $data) |
| 72 | + { |
| 73 | + $header = self::generateHeader(); |
| 74 | + return OpenLRW::httpPatch(self::PREFIX . static::$collection ."/$id", $header, $data); |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + /** |
| 79 | + * Destroy the model for the given ID. |
| 80 | + * |
| 81 | + * @return int |
| 82 | + */ |
| 83 | + public static function destroy($id) |
| 84 | + { |
| 85 | + $header = self::generateHeader(); |
| 86 | + return OpenLRW::httpDelete(self::PREFIX . static::$collection . "/$id", $header); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Delete the model from the database. |
| 91 | + * |
| 92 | + */ |
| 93 | + public function delete() |
| 94 | + { |
| 95 | + return self::destroy($this->getPrimaryKeyValue()); |
| 96 | + } |
| 97 | + |
| 98 | + public static function get($route) |
| 99 | + { |
| 100 | + $header = self::generateHeader(); |
| 101 | + return OpenLRW::httpGet(self::PREFIX . $route, $header); |
| 102 | + } |
| 103 | + |
| 104 | + public static function post($route, $data) |
| 105 | + { |
| 106 | + $header = self::generateHeader(); |
| 107 | + return OpenLRW::httpPost(self::PREFIX . $route, $header, $data); |
| 108 | + } |
| 109 | +} |
0 commit comments