Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
58.06% covered (warning)
58.06%
36 / 62
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Connector
58.06% covered (warning)
58.06%
36 / 62
60.00% covered (warning)
60.00%
3 / 5
28.45
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 search
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
2
 checkForHttpError
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 call
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
42
 getRecord
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3/**
4 * Central class for connecting to EIT resources used by VuFind.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Julia Bauder 2013.
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  Connection
25 * @author   Julia Bauder <bauderj@grinnell.edu>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org/wiki/development:architecture Wiki
28 */
29
30namespace VuFindSearch\Backend\EIT;
31
32use Laminas\Http\Client;
33use VuFindSearch\Backend\Exception\HttpErrorException;
34use VuFindSearch\ParamBag;
35
36use function is_array;
37
38/**
39 * Central class for connecting to EIT resources used by VuFind.
40 *
41 * @category VuFind
42 * @package  Connection
43 * @author   Julia Bauder <bauderj@grinnell.edu>
44 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
45 * @link     https://vufind.org/wiki/development:architecture Wiki
46 */
47class Connector implements \Laminas\Log\LoggerAwareInterface
48{
49    use \VuFind\Log\LoggerAwareTrait;
50
51    /**
52     * Base url for searches
53     *
54     * @var string
55     */
56    protected $base;
57
58    /**
59     * The HTTP_Request object used for REST transactions
60     *
61     * @var Client
62     */
63    protected $client;
64
65    /**
66     * EBSCO EIT Profile used for authentication
67     *
68     * @var string
69     */
70    protected $prof;
71
72    /**
73     * Password associated with the EBSCO EIT Profile
74     *
75     * @var string
76     */
77    protected $pwd;
78
79    /**
80     * Array of 3-character EBSCO database abbreviations to include in search
81     *
82     * @var array
83     */
84    protected $dbs = [];
85
86    /**
87     * Constructor
88     *
89     * @param string $base   Base URL
90     * @param Client $client HTTP client
91     * @param string $prof   Profile
92     * @param string $pwd    Password
93     * @param string $dbs    Database list (comma-separated abbrevs.)
94     */
95    public function __construct($base, Client $client, $prof, $pwd, $dbs)
96    {
97        $this->base = $base;
98        $this->client = $client;
99        $this->prof = $prof;
100        $this->pwd = $pwd;
101        $this->dbs = $dbs;
102    }
103
104    /**
105     * Execute a search.
106     *
107     * @param ParamBag $params Parameters
108     * @param int      $offset Search offset
109     * @param int      $limit  Search limit
110     *
111     * @return array
112     */
113    public function search(ParamBag $params, $offset, $limit)
114    {
115        $startrec = $offset + 1;
116        $params->set('startrec', $startrec);
117        $params->set('numrec', $limit);
118        $params->set('prof', $this->prof);
119        $params->set('pwd', $this->pwd);
120        $response = $this->call('GET', $params->getArrayCopy());
121        $xml = simplexml_load_string($response);
122        $finalDocs = [];
123        foreach ($xml->SearchResults->records->rec ?? [] as $doc) {
124            $finalDocs[] = simplexml_load_string($doc->asXML());
125        }
126        return [
127            'docs' => $finalDocs,
128            'offset' => $offset,
129            'total' => (int)$xml->Hits,
130        ];
131    }
132
133    /**
134     * Check for HTTP errors in a response.
135     *
136     * @param \Laminas\Http\Response $result The response to check.
137     *
138     * @throws \VuFindSearch\Backend\Exception\BackendException
139     * @return void
140     */
141    public function checkForHttpError($result)
142    {
143        if (!$result->isSuccess()) {
144            throw HttpErrorException::createFromResponse($result);
145        }
146    }
147
148    /**
149     * Make an API call
150     *
151     * @param string $method GET or POST
152     * @param array  $params Parameters to send
153     *
154     * @return \SimpleXMLElement
155     */
156    protected function call($method = 'GET', $params = null)
157    {
158        $queryString = '';
159        if ($params) {
160            $query = [];
161            foreach ($params as $function => $value) {
162                if (is_array($value)) {
163                    foreach ($value as $additional) {
164                        $additional = urlencode($additional);
165                        $query[] = "$function=$additional";
166                    }
167                } else {
168                    $value = urlencode($value);
169                    $query[] = "$function=$value";
170                }
171            }
172            $queryString = implode('&', $query);
173        }
174
175        $dbs = explode(',', $this->dbs);
176        $dblist = '';
177        foreach ($dbs as $db) {
178            $dblist .= '&db=' . $db;
179        }
180
181        $url = $this->base . '?' . $queryString . $dblist;
182        $this->debug('Connect: ' . $url);
183
184        // Send Request
185        $this->client->resetParameters();
186        $this->client->setUri($url);
187        $result = $this->client->setMethod($method)->send();
188        $body = $result->getBody();
189        $xml = simplexml_load_string($body);
190        $this->debug($this->varDump($xml));
191        return $body;
192    }
193
194    /**
195     * Retrieve a specific record.
196     *
197     * @param string   $id     Record ID to retrieve
198     * @param ParamBag $params Parameters
199     *
200     * @throws \Exception
201     * @return array
202     */
203    public function getRecord($id, ParamBag $params = null)
204    {
205        $query = 'AN ' . $id;
206        $params = $params ?: new ParamBag();
207        $params->set('prof', $this->prof);
208        $params->set('pwd', $this->pwd);
209        $params->set('query', $query);
210        $this->client->resetParameters();
211        $response = $this->call('GET', $params->getArrayCopy());
212        $xml = simplexml_load_string($response);
213        $finalDocs = [];
214        foreach ($xml->SearchResults->records->rec ?? [] as $doc) {
215            $finalDocs[] = simplexml_load_string($doc->asXML());
216        }
217        return [
218            'docs' => $finalDocs,
219            'offset' => 0,
220            'total' => (int)$xml->Hits,
221        ];
222    }
223}