Test Failed
Push — master ( c6b6d3...528954 )
by Dan Michael O.
03:21
created

Items::isInitialized()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Scriptotek\Alma\Bibs;
4
5
use Scriptotek\Alma\ArrayAccessResource;
6
use Scriptotek\Alma\Client;
7
use Scriptotek\Alma\CountableGhostModelList;
8
use Scriptotek\Alma\IterableResource;
9
10
class Items extends CountableGhostModelList implements \Countable, \Iterator, \ArrayAccess
11
{
12
    use ArrayAccessResource;
13
    use IterableResource;
14
15
    /* @var Bib */
16
    public $bib;
17
18
    /* @var Holding*/
19
    public $holding;
20
21
    public function __construct(Client $client, Bib $bib = null, Holding $holding = null)
22
    {
23
        parent::__construct($client);
24
        $this->bib = $bib;
25
        $this->holding = $holding;
26
    }
27
28
    public function setData(\stdClass $data)
29
    {
30
        $this->resources = array_map(
31
            function (\stdClass $item) {
32
                return Item::make($this->client, $this->bib, $this->holding, $item->item_data->pid)
33
                    ->init($item);
34
            },
35
            $data->item
36
        );
37
    }
38
39
    /**
40
     * Get an Item object from a barcode.
41
     *
42
     * @param string $barcode
43
     *
44
     * @return Item|null
45
     */
46
    public function fromBarcode($barcode)
47
    {
48
        $destinationUrl = $this->client->getRedirectLocation('/items', ['item_barcode' => $barcode]);
49
50
        // Extract the MMS ID from the redirect target URL.
51
        // Example: https://api-eu.hosted.exlibrisgroup.com/almaws/v1/bibs/999211285764702204/holdings/22156746440002204/items/23156746430002204
52
        if (!is_null($destinationUrl) && preg_match('$bibs/([0-9]+)/holdings/([0-9]+)/items/([0-9]+)$', $destinationUrl, $matches)) {
53
            $mms_id = $matches[1];
54
            $holding_id = $matches[2];
55
            $item_id = $matches[3];
56
57
            $bib = Bib::make($this->client, $mms_id);
58
            $holding = Holding::make($this->client, $bib, $holding_id);
59
60
            return Item::make($this->client, $bib, $holding, $item_id);
61
        }
62
63
        return null;
64
    }
65
66
    /**
67
     * Check if we have the full representation of our data object.
68
     *
69
     * @param \stdClass $data
70
     * @return boolean
71
     */
72
    protected function isInitialized($data)
73
    {
74
        return isset($data->total_record_count);
75
    }
76
77
    /**
78
     * Generate the base URL for this resource.
79
     *
80
     * @return string
81
     */
82
    protected function urlBase()
83
    {
84
        return "/bibs/{$this->bib->mms_id}/holdings/{$this->holding->holding_id}/items";
85
    }
86
}
87