Completed
Push — master ( a906fb...46ae11 )
by Dan Michael O.
02:48
created

Bibs   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 78
rs 10
wmc 8
lcom 1
cbo 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A fromBarcode() 0 12 3
A fromHoldingsId() 0 8 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\ResourceList;
6
7
class Bibs extends ResourceList
8
{
9
    protected $resourceName = Bib::class;
10
11
    /**
12
     * Get a Bib object from a item barcode.
13
     *
14
     * @param string $barcode
15
     * @return Bib
16
     */
17
    public function fromBarcode($barcode)
18
    {
19
        $destinationUrl = $this->client->getRedirectLocation('/items', ['item_barcode' => $barcode]);
20
21
        // Extract the MMS ID from the redirect target URL.
22
        // Example: https://api-eu.hosted.exlibrisgroup.com/almaws/v1/bibs/999211285764702204/holdings/22156746440002204/items/23156746430002204
23
        if (!is_null($destinationUrl) && preg_match('$bibs/([0-9]+)/holdings/([0-9]+)/items/([0-9]+)$', $destinationUrl, $matches)) {
24
            $mmsId = $matches[1];
25
26
            return $this->get($mmsId);
27
        }
28
    }
29
30
    /**
31
     * Get a Bib object from a holdings ID.
32
     *
33
     * @param string $holdings_id
34
     * @return Bib
35
     */
36
    public function fromHoldingsId($holdings_id)
37
    {
38
        $response = $this->client->getXML('/bibs', ['holdings_id' => $holdings_id]);
39
        $bib_data = $response->first('bib');
40
        $mms_id = $bib_data->text('mms_id');
41
42
        return $this->get($mms_id, null, null, $bib_data);
43
    }
44
45
    /**
46
     * Get Bib records from SRU search. You must have an SRU client connected
47
     * to the Alma client (see `Client::setSruClient()`).
48
     * Returns a generator that handles continuation under the hood.
49
     *
50
     * @param string $cql  The CQL query
51
     * @param int $batchSize  Number of records to return in each batch.
52
     * @return \Generator|Bib[]
53
     */
54
    public function search($cql, $batchSize = 10)
55
    {
56
        $this->client->assertHasSruClient();
57
58
        foreach ($this->client->sru->all($cql, $batchSize) as $sruRecord) {
59
            yield Bib::fromSruRecord($sruRecord, $this->client);
60
        }
61
    }
62
63
    /**
64
     * Returns the first result from a SRU search or null if no results.
65
     *
66
     * @param string $cql
67
     * @return Bib
68
     */
69
    public function findOne($cql)
70
    {
71
        return $this->search($cql, 1)->current();
72
    }
73
74
    /**
75
     * Get a Bib object from an ISBN value. Returns null if no Bib record found.
76
     *
77
     * @param string $isbn
78
     * @return Bib
79
     */
80
    public function fromIsbn($isbn)
81
    {
82
        return $this->findOne('alma.isbn="' . $isbn . '"');
83
    }
84
}
85