Completed
Push — master ( 0abcf2...c12050 )
by Dan Michael O.
03:13
created

Bib   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 172
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 0
loc 172
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 8

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A fetchData() 0 4 1
A setMarcRecord() 0 7 1
A fromSruRecord() 0 8 1
A getHoldings() 0 4 1
A save() 0 18 1
A getNzMmsId() 0 13 2
A getNzRecord() 0 4 1
A getRecord() 0 7 2
A onData() 0 4 1
A isInitialized() 0 4 1
A urlBase() 0 4 1
A __get() 0 12 2
1
<?php
2
3
namespace Scriptotek\Alma\Bibs;
4
5
use Danmichaelo\QuiteSimpleXMLElement\QuiteSimpleXMLElement;
6
use Scriptotek\Alma\Client;
7
use Scriptotek\Alma\Exception\NoLinkedNetworkZoneRecordException;
8
use Scriptotek\Alma\Model\LazyResource;
9
use Scriptotek\Marc\Record as MarcRecord;
10
use Scriptotek\Sru\Record as SruRecord;
11
12
/**
13
 * A single Bib resource.
14
 */
15
class Bib extends LazyResource
16
{
17
    /** @var string */
18
    public $mms_id;
19
20
    /* @var Holdings */
21
    public $holdings;
22
23
    /* @var MarcRecord */
24
    protected $_marc;
25
26
    public function __construct(Client $client = null, $mms_id = null)
27
    {
28
        parent::__construct($client);
0 ignored issues
show
Bug introduced by
It seems like $client defined by parameter $client on line 26 can be null; however, Scriptotek\Alma\Model\Model::__construct() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
29
        $this->mms_id = $mms_id;
30
        $this->holdings = Holdings::make($this->client, $this);
31
    }
32
33
    /**
34
     * Get the model data. This API does not support JSON for editing,
35
     * so we fetch XML instead.
36
     */
37
    protected function fetchData()
38
    {
39
        return $this->client->getXML($this->url());
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->client->getXML($this->url()); (Danmichaelo\QuiteSimpleX...t\QuiteSimpleXMLElement) is incompatible with the return type of the parent method Scriptotek\Alma\Model\LazyResource::fetchData of type stdClass.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
40
    }
41
42
    /**
43
     * Load MARC record onto this Bib object. Chainable method.
44
     *
45
     * @param string $xml
46
     *
47
     * @return Bib
48
     */
49
    public function setMarcRecord($xml)
50
    {
51
        $this->_marc = MarcRecord::fromString($xml);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Scriptotek\Marc\Record::fromString($xml) of type object<Scriptotek\Marc\Collection> is incompatible with the declared type object<Scriptotek\Marc\Record> of property $_marc.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
52
        // Note: do not marc as initialized, since we miss some other data from the Bib record. Oh, Alma :/
53
54
        return $this;
55
    }
56
57
    /**
58
     * Initialize from SRU record without having to fetch the Bib record.
59
     * @param SruRecord $record
60
     * @param Client|null $client
61
     * @return Bib
62
     */
63
    public static function fromSruRecord(SruRecord $record, Client $client = null)
64
    {
65
        $record->data->registerXPathNamespace('marc', 'http://www.loc.gov/MARC21/slim');
66
        $mmsId = $record->data->text('.//controlfield[@tag="001"]');
67
68
        return (new self($client, $mmsId))
69
            ->setMarcRecord($record->data->asXML());
70
    }
71
72
    /**
73
     * For backwards-compability.
74
     */
75
    public function getHoldings()
76
    {
77
        return $this->holdings;
78
    }
79
80
    /**
81
     * Save the MARC record.
82
     */
83
    public function save()
84
    {
85
        // If initialized from an SRU record, we need to fetch the
86
        // remaining parts of the Bib record.
87
        $this->init();
88
89
        // Inject the MARC record
90
        $marcXml = new QuiteSimpleXMLElement($this->_marc->toXML('UTF-8', false, false));
91
        $this->data->first('record')->replace($marcXml);
92
93
        // Serialize
94
        $newData = $this->data->asXML();
95
96
        // Alma doesn't like namespaces
97
        $newData = str_replace(' xmlns="http://www.loc.gov/MARC21/slim"', '', $newData);
98
99
        return $this->client->putXML($this->url(), $newData);
100
    }
101
102
    /**
103
     * Get the MMS ID of the linked record in network zone.
104
     */
105
    public function getNzMmsId()
106
    {
107
        // If initialized from an SRU record, we need to fetch the
108
        // remaining parts of the Bib record.
109
        $this->init();
110
111
        $nz_mms_id = $this->data->text("linked_record_id[@type='NZ']");
112
        if (!$nz_mms_id) {
113
            throw new NoLinkedNetworkZoneRecordException("Record $this->mms_id is not linked to a network zone record.");
114
        }
115
116
        return $nz_mms_id;
117
    }
118
119
    /**
120
     * Get the Bib of the linked record in network zone.
121
     */
122
    public function getNzRecord()
123
    {
124
        return $this->client->nz->bibs->get($this->getNzMmsId());
125
    }
126
127
    /**
128
     * Returns the MARC record. Load it if we don't have it yet.
129
     */
130
    public function getRecord()
131
    {
132
        if (is_null($this->_marc)) {
133
            $this->init();
134
        }
135
        return $this->_marc;
136
    }
137
138
    /**
139
     * Called when data is available to be processed.
140
     *
141
     * @param mixed $data
142
     */
143
    protected function onData($data)
144
    {
145
        $this->setMarcRecord($data->first('record')->asXML());
146
    }
147
148
    /**
149
     * Check if we have the full representation of our data object.
150
     *
151
     * @param $data
152
     * @return boolean
153
     */
154
    protected function isInitialized($data)
155
    {
156
        return $data->has('record');
157
    }
158
159
    /**
160
     * Generate the base URL for this resource.
161
     *
162
     * @return string
163
     */
164
    protected function urlBase()
165
    {
166
        return "/bibs/{$this->mms_id}";
167
    }
168
169
    /**
170
     * Magic!
171
     * @param string $key
172
     * @return mixed
173
     */
174
    public function __get($key)
175
    {
176
        // If there's a getter method, call it.
177
        if ($key == 'record') {
178
            return $this->getRecord();
179
        }
180
181
        $this->init();
182
183
        // If the property is defined in our data object, return it.
184
        return $this->data->text($key);
185
    }
186
}
187