Test Setup Failed
Pull Request — master (#15)
by
unknown
07:32
created

Item::urlBase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
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\Requests;
10
use Scriptotek\Alma\Users\User;
11
12
class Item extends LazyResource
13
{
14
    /** @var Bib */
15
    public $bib;
16
17
    /** @var Holding */
18
    public $holding;
19
20
    /** @var Requests */
21
    public $requests;
22
23
    /** @var string */
24
    public $item_id;
25
26
    /**
27
     * Item constructor.
28
     *
29
     * @param Client  $client
30
     * @param Bib     $bib
31
     * @param Holding $holding
32
     * @param $item_id
33
     */
34
    public function __construct(Client $client, Bib $bib, Holding $holding, $item_id)
35
    {
36
        parent::__construct($client);
37
        $this->bib = $bib;
38
        $this->holding = $holding;
39
        $this->item_id = $item_id;
40
        $this->requests = Requests::make($this->client, $this->url('/requests'));
41
    }
42
43
    /**
44
     * Generate the base URL for this resource.
45
     *
46
     * @return string
47
     */
48
    protected function urlBase()
49
    {
50
        return "/bibs/{$this->bib->mms_id}/holdings/{$this->holding->holding_id}/items/{$this->item_id}";
51
    }
52
53
    /**
54
     * Save the item.
55
     */
56
    public function save()
57
    {
58
        return $this->client->putJSON($this->url(), $this->getData());
59
    }
60
61
62
    /**
63
     * Check if we have the full representation of our data object.
64
     *
65
     * @param \stdClass $data
66
     *
67
     * @return bool
68
     */
69
    protected function isInitialized($data)
70
    {
71
        return isset($data->item_data);
72
    }
73
74
    /**
75
     * Called when data is available to be processed.
76
     *
77
     * @param mixed $data
78
     */
79
    protected function onData($data)
80
    {
81
        if (isset($this->bib_data)) {
82
            $this->bib->init($this->bib_data);
83
        }
84
        if (isset($this->holding_data)) {
85
            $this->holding->init($this->holding_data);
86
        }
87
    }
88
89
    /**
90
     * Create a new loan.
91
     *
92
     * @param User    $user
93
     * @param Library $library
94
     * @param string  $circ_desk
95
     *
96
     * @throws \Scriptotek\Alma\Exception\RequestFailed
97
     *
98
     * @return Loan
99
     */
100
    public function checkOut(User $user, Library $library, $circ_desk = 'DEFAULT_CIRC_DESK')
101
    {
102
        $postData = [
103
            'library'   => ['value' => $library->code],
104
            'circ_desk' => ['value' => $circ_desk],
105
        ];
106
107
        $data = $this->client->postJSON(
108
            $this->url('/loans', ['user_id' => $user->id]),
109
            $postData
110
        );
111
112
        return Loan::make($this->client, $user, $data->loan_id)
113
            ->init($data);
114
    }
115
116
    /**
117
     * Perform scan-in on item.
118
     *
119
     * @param Library $library
120
     * @param string  $circ_desk
121
     * @param array   $params
122
     *
123
     * @throws \Scriptotek\Alma\Exception\RequestFailed
124
     *
125
     * @return ScanInResponse
126
     */
127
    public function scanIn(Library $library, $circ_desk = 'DEFAULT_CIRC_DESK', $params = [])
128
    {
129
        $params['op'] = 'scan';
130
        $params['library'] = $library->code;
131
        $params['circ_desk'] = $circ_desk;
132
133
        $data = $this->client->postJSON($this->url('', $params));
134
135
        return ScanInResponse::make($this->client, $data);
136
    }
137
138
    /**
139
     * Get the current loan as a Loan object, or null if the item is not loaned out.
140
     *
141
     * @returns Loan|null
142
     */
143
    public function getLoan()
144
    {
145
        $data = $this->client->getJSON($this->url('/loans'));
146
147
        if ($data->total_record_count == 1) {
148
            return Loan::make(
149
                $this->client,
150
                User::make($this->client, $data->item_loan[0]->user_id),
151
                $data->item_loan[0]->loan_id
152
            )->init($data->item_loan[0]);
153
        }
154
    }
155
156
    public function __get($key)
157
    {
158
        if ($key == 'loan') {
159
            return $this->getLoan();
160
        }
161
162
        $this->init();
163
164
        if (isset($this->data->item_data->{$key})) {
165
            return $this->data->item_data->{$key};
166
        }
167
        if (isset($this->data->holding_data->{$key})) {
168
            return $this->data->holding_data->{$key};
169
        }
170
        if (isset($this->data->bib_data->{$key})) {
171
            return $this->data->bib_data->{$key};
172
        }
173
174
        return parent::__get($key);
175
    }
176
}
177