Passed
Push — master ( f2085b...b46880 )
by Dan Michael O.
08:55
created

Bib::__get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
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\Alma\Users\Requests;
10
use Scriptotek\Marc\Record as MarcRecord;
11
use Scriptotek\Sru\Record as SruRecord;
12
13
/**
14
 * A single Bib resource.
15
 */
16
class Bib extends LazyResource
17
{
18
    /** @var string */
19
    public $mms_id;
20
21
    /* @var Holdings */
22
    public $holdings;
23
24
    /* @var MarcRecord */
25
    protected $_marc;
26
27
    /** @var Requests */
28
    public $requests;
29
30
    public function __construct(Client $client = null, $mms_id = null)
31
    {
32
        parent::__construct($client);
0 ignored issues
show
Bug introduced by
It seems like $client defined by parameter $client on line 30 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...
33
        $this->mms_id = $mms_id;
34
        $this->holdings = Holdings::make($this->client, $this);
35
        $this->requests = Requests::make($this->client, $this->url('/requests'));
36
    }
37
38
    /**
39
     * Get the model data. This API does not support JSON for editing,
40
     * so we fetch XML instead.
41
     */
42
    protected function fetchData()
43
    {
44
        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...
45
    }
46
47
    /**
48
     * Load MARC record onto this Bib object. Chainable method.
49
     *
50
     * @param string $xml
51
     *
52
     * @return Bib
53
     */
54
    public function setMarcRecord($xml)
55
    {
56
        $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...
57
        // Note: do not marc as initialized, since we miss some other data from the Bib record. Oh, Alma :/
58
59
        return $this;
60
    }
61
62
    /**
63
     * Initialize from SRU record without having to fetch the Bib record.
64
     *
65
     * @param SruRecord   $record
66
     * @param Client|null $client
67
     *
68
     * @return Bib
69
     */
70
    public static function fromSruRecord(SruRecord $record, Client $client = null)
71
    {
72
        $record->data->registerXPathNamespace('marc', 'http://www.loc.gov/MARC21/slim');
73
        $mmsId = $record->data->text('.//controlfield[@tag="001"]');
74
75
        return (new self($client, $mmsId))
76
            ->setMarcRecord($record->data->asXML());
77
    }
78
79
    /**
80
     * For backwards-compability.
81
     */
82
    public function getHoldings()
83
    {
84
        return $this->holdings;
85
    }
86
87
    /**
88
     * Save the MARC record.
89
     */
90
    public function save()
91
    {
92
        // If initialized from an SRU record, we need to fetch the
93
        // remaining parts of the Bib record.
94
        $this->init();
95
96
        // Inject the MARC record
97
        $marcXml = new QuiteSimpleXMLElement($this->_marc->toXML('UTF-8', false, false));
98
        $this->data->first('record')->replace($marcXml);
99
100
        // Serialize
101
        $newData = $this->data->asXML();
102
103
        // Alma doesn't like namespaces
104
        $newData = str_replace(' xmlns="http://www.loc.gov/MARC21/slim"', '', $newData);
105
106
        return $this->client->putXML($this->url(), $newData);
107
    }
108
109
    /**
110
     * Get the MMS ID of the linked record in network zone.
111
     */
112
    public function getNzMmsId()
113
    {
114
        // If initialized from an SRU record, we need to fetch the
115
        // remaining parts of the Bib record.
116
        $this->init();
117
118
        $nz_mms_id = $this->data->text("linked_record_id[@type='NZ']");
119
        if (!$nz_mms_id) {
120
            throw new NoLinkedNetworkZoneRecordException("Record $this->mms_id is not linked to a network zone record.");
121
        }
122
123
        return $nz_mms_id;
124
    }
125
126
    /**
127
     * Get the Bib of the linked record in network zone.
128
     */
129
    public function getNzRecord()
130
    {
131
        return $this->client->nz->bibs->get($this->getNzMmsId());
132
    }
133
134
    /**
135
     * Returns the MARC record. Load it if we don't have it yet.
136
     */
137
    public function getRecord()
138
    {
139
        if (is_null($this->_marc)) {
140
            $this->init();
141
        }
142
143
        return $this->_marc;
144
    }
145
146
    /**
147
     * Called when data is available to be processed.
148
     *
149
     * @param mixed $data
150
     */
151
    protected function onData($data)
152
    {
153
        $this->setMarcRecord($data->first('record')->asXML());
154
    }
155
156
    /**
157
     * Check if we have the full representation of our data object.
158
     *
159
     * @param $data
160
     *
161
     * @return bool
162
     */
163
    protected function isInitialized($data)
164
    {
165
        return is_a($data, QuiteSimpleXMLElement::class) && $data->has('record');
166
    }
167
168
    /**
169
     * Generate the base URL for this resource.
170
     *
171
     * @return string
172
     */
173
    protected function urlBase()
174
    {
175
        return "/bibs/{$this->mms_id}";
176
    }
177
}
178