Holding   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 92
rs 10
c 0
b 0
f 0
wmc 9
lcom 2
cbo 5

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A fetchData() 0 4 1
A isInitialized() 0 4 2
A setMarcRecord() 0 6 1
A onData() 0 4 1
A urlBase() 0 4 1
A getRecord() 0 4 1
A getItems() 0 4 1
1
<?php
2
3
namespace Scriptotek\Alma\Bibs;
4
5
use Danmichaelo\QuiteSimpleXMLElement\QuiteSimpleXMLElement;
6
use Scriptotek\Alma\Client;
7
use Scriptotek\Alma\Model\LazyResource;
8
use Scriptotek\Marc\Record as MarcRecord;
9
10
/**
11
 * A single Holding resource.
12
 */
13
class Holding extends LazyResource
14
{
15
    /* @var string */
16
    public $holding_id;
17
18
    /* @var Bib */
19
    public $bib;
20
21
    /* @var Items */
22
    public $items;
23
24
    /* @var MarcRecord */
25
    protected $_marc;
26
27
    public function __construct(Client $client, Bib $bib, $holding_id)
28
    {
29
        parent::__construct($client);
30
        $this->bib = $bib;
31
        $this->holding_id = $holding_id;
32
        $this->items = Items::make($this->client, $bib, $this);
33
    }
34
35
    /**
36
     * Get the model data.
37
     */
38
    protected function fetchData()
39
    {
40
        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...
41
    }
42
43
    /**
44
     * Check if we have the full representation of our data object.
45
     *
46
     * @param $data
47
     *
48
     * @return bool
49
     */
50
    protected function isInitialized($data)
51
    {
52
        return is_a($data, QuiteSimpleXMLElement::class) && $data->has('record');
53
    }
54
55
    /**
56
     * Update the MARC record on this holding object. Chainable method.
57
     *
58
     * @param string $xml
59
     *
60
     * @return Holding
61
     */
62
    public function setMarcRecord($xml)
63
    {
64
        $this->_marc = MarcRecord::fromString($xml);
65
66
        return $this;
67
    }
68
69
    /**
70
     * Called when data is available to be processed.
71
     *
72
     * @param mixed $data
73
     */
74
    protected function onData($data)
75
    {
76
        $this->setMarcRecord($data->first('record')->asXML());
77
    }
78
79
    /**
80
     * Generate the base URL for this resource.
81
     *
82
     * @return string
83
     */
84
    protected function urlBase()
85
    {
86
        return "/bibs/{$this->bib->mms_id}/holdings/{$this->holding_id}";
87
    }
88
89
    /**
90
     * Get the MARC record for this holding object.
91
     */
92
    public function getRecord()
93
    {
94
        return $this->init()->_marc;
95
    }
96
97
    /**
98
     * Get the items for this holding.
99
     */
100
    public function getItems()
101
    {
102
        return $this->items;
103
    }
104
}
105