Skip to content

Commit 8831605

Browse files
committed
Added readonly codetables
1 parent 0cab0b1 commit 8831605

File tree

8 files changed

+349
-0
lines changed

8 files changed

+349
-0
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ If the package doesn't fit your needs, you might take a look at the alternative
4343
* [Listing jobs](#listing-jobs)
4444
* [Retrieving information about a specific job](#retrieving-information-about-a-specific-job)
4545
* [Submitting a job](#submitting-a-job)
46+
* [Code Tables](#code-tables)
47+
* [Getting a single code table](#getting-a-codetable)
4648
* [Automatic retries on errors](#automatic-retries-on-errors)
4749
* [Laravel integration](#laravel-integration)
4850
* [Customizing the HTTP client stack](#customizing-the-http-client-stack)
@@ -578,6 +580,20 @@ $job = $alma->jobs['M43'];
578580
$instance = $alma->jobs['M43']->submit();
579581
```
580582

583+
## Code Tables
584+
585+
### Getting a Code Table
586+
587+
To fetch a code table
588+
589+
```php
590+
$ct = $alma->codetables->get('systemJobStatus');
591+
echo "Rows for ".$ct->sub_system->value."'s ".$ct->name."\n";
592+
foreach ($ct->row as $row) {
593+
echo "code: ".$row->code.", description: ".$row->description."\n";
594+
}
595+
```
596+
581597
## Automatic retries on errors
582598

583599
If the client receives a 429 (rate limiting) response from Alma, it will sleep for a short time (0.5 seconds by default)

spec/Conf/CodeTableSpec.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace spec\Scriptotek\Alma\CodeTable;
4+
5+
use PhpSpec\ObjectBehavior;
6+
use Prophecy\Argument;
7+
use Scriptotek\Alma\Conf\CodeTable;
8+
use Scriptotek\Alma\Conf\CodeTables;
9+
use Scriptotek\Alma\Client as AlmaClient;
10+
use Scriptotek\Alma\Exception\ResourceNotFound;
11+
use spec\Scriptotek\Alma\SpecHelper;
12+
13+
class CodeTableSpec extends ObjectBehavior
14+
{
15+
public function let(AlmaClient $client)
16+
{
17+
$this->beConstructedWith($client, 'systemJobStatus');
18+
}
19+
20+
protected function expectRequest($client)
21+
{
22+
$client->getXML('/conf/code-table/systemJobStatus')
23+
->shouldBeCalled()
24+
->willReturn(SpecHelper::getDummyData('codetable_response.json'));
25+
}
26+
27+
public function it_is_lazy(AlmaClient $client)
28+
{
29+
SpecHelper::expectNoRequests($client);
30+
$this->shouldHaveType(CodeTable::class);
31+
}
32+
33+
public function it_fetches_record_data_when_needed(AlmaClient $client)
34+
{
35+
$this->expectRequest($client);
36+
37+
$this->name->('systemJobStatus');
38+
$this->subSystem->value->shouldBe('INFRA');
39+
}
40+
41+
public function it_can_exist(AlmaClient $client)
42+
{
43+
$this->expectRequest($client);
44+
45+
$this->exists()->shouldBe(true);
46+
}
47+
}

spec/Conf/CodeTablesSpec.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace spec\Scriptotek\Alma\CodeTables;
4+
5+
use PhpSpec\ObjectBehavior;
6+
use Prophecy\Argument;
7+
use Scriptotek\Alma\Conf\CodeTable;
8+
use Scriptotek\Alma\Client as AlmaClient;
9+
use spec\Scriptotek\Alma\SpecHelper;
10+
11+
class CodeTablesSpec extends ObjectBehavior
12+
{
13+
public function let(AlmaClient $client)
14+
{
15+
$this->beConstructedWith($client);
16+
}
17+
18+
public function it_provides_a_lazy_interface_to_codetable_objects(AlmaClient $client)
19+
{
20+
SpecHelper::expectNoRequests($client);
21+
22+
$ctid = 'myCodeTable'; // str_random();
23+
$bib = $this->get($ctid);
24+
25+
$bib->shouldHaveType(CodeTable::class);
26+
$bib->code->shouldBe($ctid);
27+
}
28+
29+
public function it_provides_a_lazy_array_interface_to_codetable_objects(AlmaClient $client)
30+
{
31+
SpecHelper::expectNoRequests($client);
32+
33+
$ctid = 'myCodeTable'; // str_random();
34+
$ct = $this[$ctid];
35+
36+
$ct->shouldHaveType(CodeTable::class);
37+
$ct->code->shouldBe($ctid);
38+
}
39+
40+
}

spec/data/codetable_response.json

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
{
2+
"name": "systemJobStatus",
3+
"description": "System Job Status",
4+
"sub_system": {
5+
"value": "INFRA",
6+
"desc": "Infra"
7+
},
8+
"patron_facing": true,
9+
"language": {
10+
"value": "en",
11+
"desc": "English"
12+
},
13+
"scope": {
14+
"institution_id": {
15+
"value": "01MY_INST",
16+
"desc": "My Institution"
17+
},
18+
"library_id": {
19+
"value": "",
20+
"desc": ""
21+
}
22+
},
23+
"row": [
24+
{
25+
"code": "QUEUED",
26+
"description": "Queued",
27+
"default": false,
28+
"enabled": true
29+
},
30+
{
31+
"code": "PENDING",
32+
"description": "Pending",
33+
"default": false,
34+
"enabled": true
35+
},
36+
{
37+
"code": "INITIALIZING",
38+
"description": "Initializing",
39+
"default": false,
40+
"enabled": true
41+
},
42+
{
43+
"code": "RUNNING",
44+
"description": "Running",
45+
"default": false,
46+
"enabled": true
47+
},
48+
{
49+
"code": "MANUAL_HANDLING_REQUIRED",
50+
"description": "Manual Handling Required",
51+
"default": false,
52+
"enabled": true
53+
},
54+
{
55+
"code": "FINALIZING",
56+
"description": "Finalizing",
57+
"default": false,
58+
"enabled": true
59+
},
60+
{
61+
"code": "COMPLETED_SUCCESS",
62+
"description": "Completed Successfully",
63+
"default": false,
64+
"enabled": true
65+
},
66+
{
67+
"code": "COMPLETED_NO_BULKS",
68+
"description": "Completed with no Bulks",
69+
"default": false,
70+
"enabled": true
71+
},
72+
{
73+
"code": "COMPLETED_FAILED",
74+
"description": "Completed with Errors",
75+
"default": false,
76+
"enabled": true
77+
},
78+
{
79+
"code": "FAILED",
80+
"description": "Failed",
81+
"default": false,
82+
"enabled": true
83+
},
84+
{
85+
"code": "COMPLETED_WARNING",
86+
"description": "Completed with Warnings",
87+
"default": false,
88+
"enabled": true
89+
},
90+
{
91+
"code": "USER_ABORTED",
92+
"description": "Aborted by User",
93+
"default": false,
94+
"enabled": true
95+
},
96+
{
97+
"code": "SYSTEM_ABORTED",
98+
"description": "Aborted by System",
99+
"default": false,
100+
"enabled": true
101+
},
102+
{
103+
"code": "SKIPPED",
104+
"description": "Skipped",
105+
"default": false,
106+
"enabled": true
107+
}
108+
]
109+
}

src/Client.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Scriptotek\Alma\Conf\Jobs;
2525
use Scriptotek\Alma\Conf\Libraries;
2626
use Scriptotek\Alma\Conf\Library;
27+
use Scriptotek\Alma\Conf\CodeTables;
2728
use Scriptotek\Alma\Exception\ClientException as AlmaClientException;
2829
use Scriptotek\Alma\Exception\InvalidApiKey;
2930
use Scriptotek\Alma\Exception\MaxNumberOfAttemptsExhausted;
@@ -103,6 +104,11 @@ class Client
103104
*/
104105
public $jobs;
105106

107+
/**
108+
* @var CodeTables
109+
*/
110+
public $codetables;
111+
106112
/**
107113
* @var TaskLists
108114
*/
@@ -152,6 +158,7 @@ public function __construct(
152158
$this->conf = new Conf($this);
153159
$this->libraries = $this->conf->libraries; // shortcut
154160
$this->jobs = $this->conf->jobs; // shortcut
161+
$this->codetables = $this->conf->codetables; // shortcut
155162

156163
$this->taskLists = new TaskLists($this);
157164

src/Conf/CodeTable.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace Scriptotek\Alma\Conf;
4+
5+
use Scriptotek\Alma\Client;
6+
use Scriptotek\Alma\Model\LazyResource;
7+
8+
/**
9+
* A single CodeTable resource.
10+
*/
11+
class CodeTable extends LazyResource
12+
{
13+
/** @var string */
14+
public $code;
15+
16+
/**
17+
* CodeTable constructor.
18+
*
19+
* @param Client $client
20+
* @param string $code
21+
*/
22+
public function __construct(Client $client, $code)
23+
{
24+
parent::__construct($client);
25+
$this->code = $code;
26+
}
27+
28+
/**
29+
* Return a list of rows referring to the code of the rows in the table.
30+
*
31+
* @param string $code - The code of the row in the Table we want to pull.
32+
*
33+
* @return array $found - The rows in the code table that match the code passed in.
34+
*/
35+
public function getRowByCode($code)
36+
{
37+
$found = array();
38+
$codeTable = json_decode($this->client->get($this->urlBase()));
39+
foreach ($codeTable->row as $row) {
40+
if ($row->code == $code) {
41+
array_push($found,$row);
42+
}
43+
}
44+
return($found);
45+
}
46+
47+
/**
48+
* Check if we have the full representation of our data object.
49+
*
50+
* @param \stdClass $data
51+
*
52+
* @return bool
53+
*/
54+
protected function isInitialized($data)
55+
{
56+
return isset($data->name);
57+
}
58+
59+
/**
60+
* Generate the base URL for this resource.
61+
*
62+
* @return string
63+
*/
64+
protected function urlBase()
65+
{
66+
return "/conf/code-tables/{$this->code}";
67+
}
68+
69+
}

src/Conf/CodeTables.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Scriptotek\Alma\Conf;
4+
5+
use Scriptotek\Alma\Client;
6+
use Scriptotek\Alma\Model\ReadOnlyArrayAccess;
7+
8+
/**
9+
* A non-iterable collection of CodeTable resources
10+
*/
11+
class CodeTables implements \ArrayAccess
12+
{
13+
use ReadOnlyArrayAccess;
14+
15+
protected $client;
16+
17+
/**
18+
* CodeTables constructor.
19+
*
20+
* @param Client $client
21+
*/
22+
public function __construct(Client $client)
23+
{
24+
$this->client = $client;
25+
}
26+
27+
/**
28+
* Get a CodeTable by identifier
29+
*
30+
* @param $code The identifier of a CodeTable
31+
*
32+
* @return CodeTable
33+
*/
34+
public function get($code)
35+
{
36+
return CodeTable::make($this->client, $code);
37+
}
38+
39+
/**
40+
* Return a object containing a list of code tables.
41+
*
42+
* @return CodeTable ojbect list.
43+
*/
44+
public function getCodeTables()
45+
{
46+
return json_decode($this->client->get($this->urlBase()));
47+
}
48+
49+
/**
50+
* Generate the base URL for this resource.
51+
*
52+
* @return string
53+
*/
54+
protected function urlBase()
55+
{
56+
return '/conf/code-tables';
57+
}
58+
59+
}

0 commit comments

Comments
 (0)