Completed
Push — master ( cee823...0abcf2 )
by Dan Michael O.
02:12
created

Item::getLoan()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
namespace Scriptotek\Alma\Bibs;
4
5
use Scriptotek\Alma\Client;
6
use Scriptotek\Alma\Conf\Library;
7
use Scriptotek\Alma\Model\LazyResource;
8
use Scriptotek\Alma\Users\Loan;
9
use Scriptotek\Alma\Users\User;
10
11
class Item extends LazyResource
12
{
13
    /** @var Bib */
14
    public $bib;
15
16
    /** @var Holding */
17
    public $holding;
18
19
    /** @var string */
20
    protected $item_id;
21
22
    /**
23
     * Item constructor.
24
     *
25
     * @param Client $client
26
     * @param Bib $bib
27
     * @param Holding $holding
28
     * @param $item_id
29
     */
30
    public function __construct(Client $client, Bib $bib, Holding $holding, $item_id)
31
    {
32
        parent::__construct($client);
33
        $this->bib = $bib;
34
        $this->holding = $holding;
35
        $this->item_id = $item_id;
36
    }
37
38
    /**
39
     * Generate the base URL for this resource.
40
     *
41
     * @return string
42
     */
43
    protected function urlBase()
44
    {
45
        return "/bibs/{$this->bib->mms_id}/holdings/{$this->holding->holding_id}/items/{$this->item_id}";
46
    }
47
48
    /**
49
     * Check if we have the full representation of our data object.
50
     *
51
     * @param \stdClass $data
52
     * @return boolean
53
     */
54
    protected function isInitialized($data)
55
    {
56
        return isset($data->item_data);
57
    }
58
59
    /**
60
     * Called when data is available to be processed.
61
     *
62
     * @param mixed $data
63
     */
64
    protected function onData($data)
65
    {
66
        if (isset($this->bib_data)) {
67
            $this->bib->init($this->bib_data);
0 ignored issues
show
Documentation introduced by
The property bib_data does not exist on object<Scriptotek\Alma\Bibs\Item>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
68
        }
69
        if (isset($this->holding_data)) {
70
            $this->holding->init($this->holding_data);
0 ignored issues
show
Bug introduced by
The property holding_data does not seem to exist. Did you mean holding?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
71
        }
72
    }
73
74
    /**
75
     * Create a new loan.
76
     *
77
     * @param User $user
78
     * @param Library $library
79
     * @param string $circ_desk
80
     * @return Loan
81
     * @throws \Scriptotek\Alma\Exception\RequestFailed
82
     */
83
    public function checkOut(User $user, Library $library, $circ_desk = 'DEFAULT_CIRC_DESK')
84
    {
85
        $postData = [
86
            'library' => ['value' => $library->code],
87
            'circ_desk' => ['value' => $circ_desk],
88
        ];
89
90
        $data = $this->client->postJSON(
91
            $this->url('/loans', ['user_id' => $user->id]),
92
            $postData
93
        );
94
95
        return Loan::make($this->client, $user, $data->loan_id)
96
            ->init($data);
0 ignored issues
show
Documentation introduced by
$data is of type boolean, but the function expects a object<stdClass>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
97
    }
98
99
    /**
100
     * Perform scan-in on item.
101
     *
102
     * @param Library $library
103
     * @param string $circ_desk
104
     * @param array $params
105
     * @return ScanInResponse
106
     * @throws \Scriptotek\Alma\Exception\RequestFailed
107
     */
108
    public function scanIn(Library $library, $circ_desk = 'DEFAULT_CIRC_DESK', $params = [])
109
    {
110
        $params['op'] = 'scan';
111
        $params['library'] = $library->code;
112
        $params['circ_desk'] = $circ_desk;
113
114
        $data = $this->client->postJSON($this->url('', $params));
115
116
        return ScanInResponse::make($this->client, $data);
117
    }
118
119
    /**
120
     * Get the current loan as a Loan object, or null if the item is not loaned out.
121
     *
122
     * @returns Loan|null
123
     */
124
    public function getLoan()
125
    {
126
        $data = $this->client->getJSON($this->url('/loans'));
127
128
        if ($data->total_record_count == 1) {
129
            return Loan::make(
130
                $this->client,
131
                User::make($this->client, $data->item_loan[0]->user_id),
132
                $data->item_loan[0]->loan_id
133
            )->init($data->item_loan[0]);
134
        }
135
136
        return null;
137
    }
138
139
    public function __get($key)
140
    {
141
        if ($key == 'loan') {
142
            return $this->getLoan();
143
        }
144
145
        $this->init();
146
147
        if (isset($this->data->item_data->{$key})) {
148
            return $this->data->item_data->{$key};
149
        }
150
        if (isset($this->data->holding_data->{$key})) {
151
            return $this->data->holding_data->{$key};
152
        }
153
        if (isset($this->data->bib_data->{$key})) {
154
            return $this->data->bib_data->{$key};
155
        }
156
157
        return parent::__get($key);
158
    }
159
}
160