Bibs   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 105
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 5

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 0 7 1
A fromBarcode() 0 12 3
A fromHoldingsId() 0 7 1
A search() 0 8 2
A findOne() 0 4 1
A fromIsbn() 0 4 1
1
<?php
2
3
namespace Scriptotek\Alma\Bibs;
4
5
use Scriptotek\Alma\Client;
6
use Scriptotek\Alma\Model\ReadOnlyArrayAccess;
7
8
/**
9
 * Non-iterable collection of Bib resources.
10
 */
11
class Bibs implements \ArrayAccess
12
{
13
    use ReadOnlyArrayAccess;
14
15
    protected $client;
16
17
    public function __construct(Client $client)
18
    {
19
        $this->client = $client;
20
    }
21
22
    /**
23
     * Get a Bib object.
24
     *
25
     * @param string $mms_id
26
     * @param array  $expand Expand the bibliographic record with additional information.
27
     *
28
     * @return Bib
29
     */
30
    public function get($mms_id, $expand = null)
31
    {
32
        $params = ['expand' => $expand];
33
34
        return Bib::make($this->client, $mms_id)
35
            ->setParams($params);
36
    }
37
38
    /**
39
     * Get a Bib object from a item barcode.
40
     *
41
     * @param string $barcode
42
     *
43
     * @return Bib
44
     */
45
    public function fromBarcode($barcode)
46
    {
47
        $destinationUrl = $this->client->getRedirectLocation('/items', ['item_barcode' => $barcode]);
48
49
        // Extract the MMS ID from the redirect target URL.
50
        // Example: https://api-eu.hosted.exlibrisgroup.com/almaws/v1/bibs/999211285764702204/holdings/22156746440002204/items/23156746430002204
51
        if (!is_null($destinationUrl) && preg_match('$bibs/([0-9]+)/holdings/([0-9]+)/items/([0-9]+)$', $destinationUrl, $matches)) {
52
            $mmsId = $matches[1];
53
54
            return $this->get($mmsId);
55
        }
56
    }
57
58
    /**
59
     * Get a Bib object from a holdings ID.
60
     *
61
     * @param string $holdings_id
62
     *
63
     * @return Bib
64
     */
65
    public function fromHoldingsId($holdings_id)
66
    {
67
        $data = $this->client->getXML('/bibs', ['holdings_id' => $holdings_id]);
68
69
        return $this->get($data->text('bib/mms_id'))
70
            ->init($data->first('bib'));
71
    }
72
73
    /**
74
     * Get Bib records from SRU search. You must have an SRU client connected
75
     * to the Alma client (see `Client::setSruClient()`).
76
     * Returns a generator that handles continuation under the hood.
77
     *
78
     * @param string $cql       The CQL query
79
     * @param int    $batchSize Number of records to return in each batch.
80
     *
81
     * @return \Generator|Bib[]
82
     */
83
    public function search($cql, $batchSize = 10)
84
    {
85
        $this->client->assertHasSruClient();
86
87
        foreach ($this->client->sru->all($cql, $batchSize) as $sruRecord) {
88
            yield Bib::fromSruRecord($sruRecord, $this->client);
89
        }
90
    }
91
92
    /**
93
     * Returns the first result from a SRU search or null if no results.
94
     *
95
     * @param string $cql
96
     *
97
     * @return Bib
98
     */
99
    public function findOne($cql)
100
    {
101
        return $this->search($cql, 1)->current();
102
    }
103
104
    /**
105
     * Get a Bib object from an ISBN value. Returns null if no Bib record found.
106
     *
107
     * @param string $isbn
108
     *
109
     * @return Bib
110
     */
111
    public function fromIsbn($isbn)
112
    {
113
        return $this->findOne('alma.isbn="' . $isbn . '"');
114
    }
115
}
116