Completed
Push — master ( 9d90d4...cee823 )
by Dan Michael O.
03:06
created

Items   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 0
loc 82
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A convertToResource() 0 5 1
A urlBase() 0 4 1
A fromBarcode() 0 19 3
1
<?php
2
3
namespace Scriptotek\Alma\Bibs;
4
5
use Scriptotek\Alma\Model\ReadOnlyArrayAccess;
6
use Scriptotek\Alma\Client;
7
use Scriptotek\Alma\Model\LazyResourceList;
8
use Scriptotek\Alma\Model\IterableCollection;
9
10
/**
11
 * Iterable collection of Item resources belonging to some Holding resource.
12
 */
13
class Items extends LazyResourceList implements \Countable, \Iterator, \ArrayAccess
14
{
15
    use ReadOnlyArrayAccess;
16
    use IterableCollection;
17
18
    /**
19
     * The Bib this Items list belongs to.
20
     *
21
     * @var Bib
22
     */
23
    public $bib;
24
25
    /**
26
     * The Holding this Items list belongs to.
27
     *
28
     * @var Holding
29
     */
30
    public $holding;
31
32
    /**
33
     * Items constructor.
34
     *
35
     * @param Client $client
36
     * @param Bib $bib
37
     * @param Holding $holding
38
     */
39
    public function __construct(Client $client, Bib $bib = null, Holding $holding = null)
40
    {
41
        parent::__construct($client, 'item');
42
        $this->bib = $bib;
43
        $this->holding = $holding;
44
    }
45
46
    /**
47
     * Convert a data element to a resource object.
48
     *
49
     * @param $data
50
     * @return Item
51
     */
52
    protected function convertToResource($data)
53
    {
54
        return Item::make($this->client, $this->bib, $this->holding, $data->item_data->pid)
55
            ->init($data);
56
    }
57
58
    /**
59
     * Generate the base URL for this resource.
60
     *
61
     * @return string
62
     */
63
    protected function urlBase()
64
    {
65
        return "/bibs/{$this->bib->mms_id}/holdings/{$this->holding->holding_id}/items";
66
    }
67
68
    /**
69
     * Get an Item object from a barcode.
70
     *
71
     * @param string $barcode
72
     *
73
     * @return Item|null
74
     */
75
    public function fromBarcode($barcode)
76
    {
77
        $destinationUrl = $this->client->getRedirectLocation('/items', ['item_barcode' => $barcode]);
78
79
        // Extract the MMS ID from the redirect target URL.
80
        // Example: https://api-eu.hosted.exlibrisgroup.com/almaws/v1/bibs/999211285764702204/holdings/22156746440002204/items/23156746430002204
81
        if (!is_null($destinationUrl) && preg_match('$bibs/([0-9]+)/holdings/([0-9]+)/items/([0-9]+)$', $destinationUrl, $matches)) {
82
            $mms_id = $matches[1];
83
            $holding_id = $matches[2];
84
            $item_id = $matches[3];
85
86
            $bib = Bib::make($this->client, $mms_id);
87
            $holding = Holding::make($this->client, $bib, $holding_id);
88
89
            return Item::make($this->client, $bib, $holding, $item_id);
90
        }
91
92
        return null;
93
    }
94
}
95