From 883160522b1f46454c69e148079bb24cb4c84521 Mon Sep 17 00:00:00 2001 From: Brian Gregg Date: Wed, 26 May 2021 16:48:40 -0400 Subject: [PATCH] Added readonly codetables --- README.md | 16 +++++ spec/Conf/CodeTableSpec.php | 47 +++++++++++++ spec/Conf/CodeTablesSpec.php | 40 +++++++++++ spec/data/codetable_response.json | 109 ++++++++++++++++++++++++++++++ src/Client.php | 7 ++ src/Conf/CodeTable.php | 69 +++++++++++++++++++ src/Conf/CodeTables.php | 59 ++++++++++++++++ src/Conf/Conf.php | 2 + 8 files changed, 349 insertions(+) create mode 100644 spec/Conf/CodeTableSpec.php create mode 100644 spec/Conf/CodeTablesSpec.php create mode 100644 spec/data/codetable_response.json create mode 100644 src/Conf/CodeTable.php create mode 100644 src/Conf/CodeTables.php diff --git a/README.md b/README.md index dcfb52f..aa49760 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,8 @@ If the package doesn't fit your needs, you might take a look at the alternative * [Listing jobs](#listing-jobs) * [Retrieving information about a specific job](#retrieving-information-about-a-specific-job) * [Submitting a job](#submitting-a-job) + * [Code Tables](#code-tables) + * [Getting a single code table](#getting-a-codetable) * [Automatic retries on errors](#automatic-retries-on-errors) * [Laravel integration](#laravel-integration) * [Customizing the HTTP client stack](#customizing-the-http-client-stack) @@ -578,6 +580,20 @@ $job = $alma->jobs['M43']; $instance = $alma->jobs['M43']->submit(); ``` +## Code Tables + +### Getting a Code Table + +To fetch a code table + +```php +$ct = $alma->codetables->get('systemJobStatus'); +echo "Rows for ".$ct->sub_system->value."'s ".$ct->name."\n"; +foreach ($ct->row as $row) { + echo "code: ".$row->code.", description: ".$row->description."\n"; +} +``` + ## Automatic retries on errors If the client receives a 429 (rate limiting) response from Alma, it will sleep for a short time (0.5 seconds by default) diff --git a/spec/Conf/CodeTableSpec.php b/spec/Conf/CodeTableSpec.php new file mode 100644 index 0000000..8b3b2b1 --- /dev/null +++ b/spec/Conf/CodeTableSpec.php @@ -0,0 +1,47 @@ +beConstructedWith($client, 'systemJobStatus'); + } + + protected function expectRequest($client) + { + $client->getXML('/conf/code-table/systemJobStatus') + ->shouldBeCalled() + ->willReturn(SpecHelper::getDummyData('codetable_response.json')); + } + + public function it_is_lazy(AlmaClient $client) + { + SpecHelper::expectNoRequests($client); + $this->shouldHaveType(CodeTable::class); + } + + public function it_fetches_record_data_when_needed(AlmaClient $client) + { + $this->expectRequest($client); + + $this->name->('systemJobStatus'); + $this->subSystem->value->shouldBe('INFRA'); + } + + public function it_can_exist(AlmaClient $client) + { + $this->expectRequest($client); + + $this->exists()->shouldBe(true); + } +} diff --git a/spec/Conf/CodeTablesSpec.php b/spec/Conf/CodeTablesSpec.php new file mode 100644 index 0000000..cc2c490 --- /dev/null +++ b/spec/Conf/CodeTablesSpec.php @@ -0,0 +1,40 @@ +beConstructedWith($client); + } + + public function it_provides_a_lazy_interface_to_codetable_objects(AlmaClient $client) + { + SpecHelper::expectNoRequests($client); + + $ctid = 'myCodeTable'; // str_random(); + $bib = $this->get($ctid); + + $bib->shouldHaveType(CodeTable::class); + $bib->code->shouldBe($ctid); + } + + public function it_provides_a_lazy_array_interface_to_codetable_objects(AlmaClient $client) + { + SpecHelper::expectNoRequests($client); + + $ctid = 'myCodeTable'; // str_random(); + $ct = $this[$ctid]; + + $ct->shouldHaveType(CodeTable::class); + $ct->code->shouldBe($ctid); + } + +} diff --git a/spec/data/codetable_response.json b/spec/data/codetable_response.json new file mode 100644 index 0000000..f31288d --- /dev/null +++ b/spec/data/codetable_response.json @@ -0,0 +1,109 @@ +{ + "name": "systemJobStatus", + "description": "System Job Status", + "sub_system": { + "value": "INFRA", + "desc": "Infra" + }, + "patron_facing": true, + "language": { + "value": "en", + "desc": "English" + }, + "scope": { + "institution_id": { + "value": "01MY_INST", + "desc": "My Institution" + }, + "library_id": { + "value": "", + "desc": "" + } + }, + "row": [ + { + "code": "QUEUED", + "description": "Queued", + "default": false, + "enabled": true + }, + { + "code": "PENDING", + "description": "Pending", + "default": false, + "enabled": true + }, + { + "code": "INITIALIZING", + "description": "Initializing", + "default": false, + "enabled": true + }, + { + "code": "RUNNING", + "description": "Running", + "default": false, + "enabled": true + }, + { + "code": "MANUAL_HANDLING_REQUIRED", + "description": "Manual Handling Required", + "default": false, + "enabled": true + }, + { + "code": "FINALIZING", + "description": "Finalizing", + "default": false, + "enabled": true + }, + { + "code": "COMPLETED_SUCCESS", + "description": "Completed Successfully", + "default": false, + "enabled": true + }, + { + "code": "COMPLETED_NO_BULKS", + "description": "Completed with no Bulks", + "default": false, + "enabled": true + }, + { + "code": "COMPLETED_FAILED", + "description": "Completed with Errors", + "default": false, + "enabled": true + }, + { + "code": "FAILED", + "description": "Failed", + "default": false, + "enabled": true + }, + { + "code": "COMPLETED_WARNING", + "description": "Completed with Warnings", + "default": false, + "enabled": true + }, + { + "code": "USER_ABORTED", + "description": "Aborted by User", + "default": false, + "enabled": true + }, + { + "code": "SYSTEM_ABORTED", + "description": "Aborted by System", + "default": false, + "enabled": true + }, + { + "code": "SKIPPED", + "description": "Skipped", + "default": false, + "enabled": true + } + ] +} diff --git a/src/Client.php b/src/Client.php index d3b1714..1bd16d2 100644 --- a/src/Client.php +++ b/src/Client.php @@ -24,6 +24,7 @@ use Scriptotek\Alma\Conf\Jobs; use Scriptotek\Alma\Conf\Libraries; use Scriptotek\Alma\Conf\Library; +use Scriptotek\Alma\Conf\CodeTables; use Scriptotek\Alma\Exception\ClientException as AlmaClientException; use Scriptotek\Alma\Exception\InvalidApiKey; use Scriptotek\Alma\Exception\MaxNumberOfAttemptsExhausted; @@ -103,6 +104,11 @@ class Client */ public $jobs; + /** + * @var CodeTables + */ + public $codetables; + /** * @var TaskLists */ @@ -152,6 +158,7 @@ public function __construct( $this->conf = new Conf($this); $this->libraries = $this->conf->libraries; // shortcut $this->jobs = $this->conf->jobs; // shortcut + $this->codetables = $this->conf->codetables; // shortcut $this->taskLists = new TaskLists($this); diff --git a/src/Conf/CodeTable.php b/src/Conf/CodeTable.php new file mode 100644 index 0000000..d2ced0a --- /dev/null +++ b/src/Conf/CodeTable.php @@ -0,0 +1,69 @@ +code = $code; + } + + /** + * Return a list of rows referring to the code of the rows in the table. + * + * @param string $code - The code of the row in the Table we want to pull. + * + * @return array $found - The rows in the code table that match the code passed in. + */ + public function getRowByCode($code) + { + $found = array(); + $codeTable = json_decode($this->client->get($this->urlBase())); + foreach ($codeTable->row as $row) { + if ($row->code == $code) { + array_push($found,$row); + } + } + return($found); + } + + /** + * Check if we have the full representation of our data object. + * + * @param \stdClass $data + * + * @return bool + */ + protected function isInitialized($data) + { + return isset($data->name); + } + + /** + * Generate the base URL for this resource. + * + * @return string + */ + protected function urlBase() + { + return "/conf/code-tables/{$this->code}"; + } + +} diff --git a/src/Conf/CodeTables.php b/src/Conf/CodeTables.php new file mode 100644 index 0000000..0149528 --- /dev/null +++ b/src/Conf/CodeTables.php @@ -0,0 +1,59 @@ +client = $client; + } + + /** + * Get a CodeTable by identifier + * + * @param $code The identifier of a CodeTable + * + * @return CodeTable + */ + public function get($code) + { + return CodeTable::make($this->client, $code); + } + + /** + * Return a object containing a list of code tables. + * + * @return CodeTable ojbect list. + */ + public function getCodeTables() + { + return json_decode($this->client->get($this->urlBase())); + } + + /** + * Generate the base URL for this resource. + * + * @return string + */ + protected function urlBase() + { + return '/conf/code-tables'; + } + +} diff --git a/src/Conf/Conf.php b/src/Conf/Conf.php index 65d44e3..8c2e4d3 100644 --- a/src/Conf/Conf.php +++ b/src/Conf/Conf.php @@ -4,6 +4,7 @@ use Scriptotek\Alma\Client; use Scriptotek\Alma\Conf\Jobs; +use Scriptotek\Alma\Conf\CodeTables; class Conf { @@ -12,5 +13,6 @@ public function __construct(Client $client) $this->client = $client; $this->libraries = new Libraries($client); $this->jobs = new Jobs($client); + $this->codetables = new CodeTables($client); } }