Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
Relais
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 7
132
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getDefaultData
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 getOclcRequestData
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 request
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
 authenticatePatron
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 placeRequest
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 search
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3/**
4 * Relais connection class.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2018.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  Relais
25 * @author   Demian Katz <demian.katz@villanova.edu>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org/wiki/development Wiki
28 */
29
30namespace VuFind\Connection;
31
32use Laminas\Config\Config;
33use Laminas\Http\Client;
34
35/**
36 * Relais connection class.
37 *
38 * @category VuFind
39 * @package  Relais
40 * @author   Demian Katz <demian.katz@villanova.edu>
41 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
42 * @link     https://vufind.org/wiki/development Wiki
43 */
44class Relais implements \Laminas\Log\LoggerAwareInterface
45{
46    use \VuFind\Log\LoggerAwareTrait;
47
48    /**
49     * HTTP client
50     *
51     * @var Client
52     */
53    protected $client;
54
55    /**
56     * Relais configuration
57     *
58     * @var Config
59     */
60    protected $config;
61
62    /**
63     * Constructor
64     *
65     * @param Client $client HTTP client
66     * @param Config $config Relais configuration
67     */
68    public function __construct(Client $client, Config $config)
69    {
70        $this->client = $client;
71        $this->config = $config;
72    }
73
74    /**
75     * Get default data to send to API.
76     *
77     * @return array
78     */
79    protected function getDefaultData()
80    {
81        return [
82            'ApiKey' => $this->config->apikey ?? null,
83            'UserGroup' => 'PATRON',
84            'PartnershipId' => $this->config->group ?? null,
85            'LibrarySymbol' => $this->config->symbol ?? null,
86        ];
87    }
88
89    /**
90     * Format the parameters needed to look up an OCLC number in the API.
91     *
92     * @param string  $oclc   OCLC number to look up
93     * @param ?string $patron Patron ID (null to use default from config)
94     *
95     * @return array
96     */
97    protected function getOclcRequestData($oclc, $patron)
98    {
99        return [
100            'PickupLocation' => $this->config->pickupLocation ?? null,
101            'Notes' => 'This request was made through the VuFind Catalog interface',
102            'PatronId' => $patron ?? $this->config->patronForLookup ?? null,
103            'ExactSearch' => [
104                [
105                    'Type' => 'OCLC',
106                    'Value' => $oclc,
107                ],
108            ],
109        ];
110    }
111
112    /**
113     * Make an API request
114     *
115     * @param string $uri  Endpoint to request from
116     * @param array  $data Data to send with request
117     *
118     * @return string
119     */
120    protected function request($uri, $data)
121    {
122        $this->client->resetParameters()
123            ->setUri($uri)
124            ->setMethod('POST');
125        $requestBody = json_encode($data + $this->getDefaultData());
126        $this->debug('Posting ' . $requestBody . ' to ' . $uri);
127        $this->client->setRawBody($requestBody);
128        $this->client->getRequest()->getHeaders()
129            ->addHeaderLine('Content-Type: application/json');
130        $response = $this->client->send();
131        $body = $response->getBody();
132        $this->debug('Status: ' . $response->getStatusCode() . ', body: ' . $body);
133        return $body;
134    }
135
136    /**
137     * Authenticate a patron
138     *
139     * @param string $patron           Patron ID (null to use default from config)
140     * @param bool   $returnFullObject True to return the full API response object;
141     * false to return only the authorization ID.
142     *
143     * @return mixed
144     * @throws \Exception
145     */
146    public function authenticatePatron($patron = null, $returnFullObject = false)
147    {
148        $uri = $this->config->authenticateurl ?? null;
149        if (empty($uri)) {
150            throw new \Exception('authenticateurl not configured!');
151        }
152        $data = ['PatronId' => $patron ?? $this->config->patronForLookup ?? null];
153        $result = json_decode($this->request($uri, $data));
154        return $returnFullObject ? $result : ($result->AuthorizationId ?? null);
155    }
156
157    /**
158     * Place a request
159     *
160     * @param string $oclc   OCLC number to look up
161     * @param string $auth   Authentication ID from authenticatePatron()
162     * @param string $patron Patron ID (null to use default from config)
163     *
164     * @return string
165     * @throws \Exception
166     */
167    public function placeRequest($oclc, $auth, $patron = null)
168    {
169        $uri = $this->config->addurl ?? null;
170        if (empty($uri)) {
171            throw new \Exception('addurl not configured!');
172        }
173        $data = $this->getOclcRequestData($oclc, $patron);
174        return $this->request($uri . '?aid=' . urlencode($auth), $data);
175    }
176
177    /**
178     * Perform a search
179     *
180     * @param string $oclc   OCLC number to look up
181     * @param string $auth   Authentication ID from authenticatePatron()
182     * @param string $patron Patron ID (null to use default from config)
183     *
184     * @return string
185     * @throws \Exception
186     */
187    public function search($oclc, $auth, $patron = null)
188    {
189        $uri = $this->config->availableurl ?? null;
190        if (empty($uri)) {
191            throw new \Exception('availableurl not configured!');
192        }
193        $data = $this->getOclcRequestData($oclc, $patron);
194        return $this->request($uri . '?aid=' . urlencode($auth), $data);
195    }
196}