Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.00% covered (success)
92.00%
23 / 25
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Connector
92.00% covered (success)
92.00%
23 / 25
50.00% covered (danger)
50.00%
1 / 2
12.07
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 httpRequest
91.30% covered (success)
91.30%
21 / 23
0.00% covered (danger)
0.00%
0 / 1
11.08
1<?php
2
3/**
4 * EBSCO EDS API Connector
5 *
6 * PHP version 8
7 *
8 * Copyright (C) EBSCO Industries 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 EBSCOIndustries
24 * @package  EBSCO
25 * @author   Michelle Milton <mmilton@epnet.com>
26 * @author   Cornelius Amzar <cornelius.amzar@bsz-bw.de>
27 * @author   Ere Maijala <ere.maijala@helsinki.fi>
28 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
29 * @link     https://vufind.org
30 */
31
32namespace VuFindSearch\Backend\EDS;
33
34use Laminas\Http\Client as HttpClient;
35use Laminas\Log\LoggerAwareInterface;
36
37/**
38 * EBSCO EDS API Connector
39 *
40 * @category EBSCOIndustries
41 * @package  EBSCO
42 * @author   Michelle Milton <mmilton@epnet.com>
43 * @author   Cornelius Amzar <cornelius.amzar@bsz-bw.de>
44 * @author   Ere Maijala <ere.maijala@helsinki.fi>
45 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
46 * @link     https://vufind.org
47 */
48class Connector extends Base implements LoggerAwareInterface
49{
50    use \VuFind\Log\LoggerAwareTrait;
51    use \VuFindSearch\Backend\Feature\ConnectorCacheTrait;
52
53    /**
54     * The HTTP Request object to execute EDS API transactions
55     *
56     * @var HttpClient
57     */
58    protected $client;
59
60    /**
61     * Constructor
62     *
63     * Sets up the EDS API Client
64     *
65     * @param array      $settings Associative array of setting to use in
66     * conjunction with the EDS API
67     *    <ul>
68     *      <li>debug - boolean to control debug mode</li>
69     *      <li>orgid - Organization making calls to the EDS API</li>
70     *      <li>timeout - HTTP timeout value (default = 120)</li>
71     *    </ul>
72     * @param HttpClient $client   HTTP client object
73     */
74    public function __construct($settings, $client)
75    {
76        parent::__construct($settings);
77        $this->client = $client;
78    }
79
80    /**
81     * Perform an HTTP request.
82     *
83     * @param string $baseUrl       Base URL for request
84     * @param string $method        HTTP method for request (GET,POST, etc.)
85     * @param string $queryString   Query string to append to URL
86     * @param array  $headers       HTTP headers to send
87     * @param string $messageBody   Message body to for HTTP Request
88     * @param string $messageFormat Format of request $messageBody and responses
89     * @param bool   $cacheable     Whether the request is cacheable
90     *
91     * @throws ApiException
92     * @return string               HTTP response body
93     */
94    protected function httpRequest(
95        $baseUrl,
96        $method,
97        $queryString,
98        $headers,
99        $messageBody = null,
100        $messageFormat = 'application/json; charset=utf-8',
101        $cacheable = true
102    ) {
103        $this->debug("{$method}{$baseUrl}?{$queryString}");
104
105        $this->client->resetParameters();
106
107        $this->client->setHeaders($headers);
108        $this->client->setMethod($method);
109
110        if ($method == 'GET' && !empty($queryString)) {
111            $baseUrl .= '?' . $queryString;
112        } elseif ($method == 'POST' && isset($messageBody)) {
113            $this->client->setRawBody($messageBody);
114        }
115        $this->client->setUri($baseUrl);
116        $this->client->setEncType($messageFormat);
117
118        // Check cache:
119        $cacheKey = null;
120        if ($cacheable && $this->cache) {
121            $cacheKey = $this->getCacheKey($this->client);
122            if ($result = $this->getCachedData($cacheKey)) {
123                return $result;
124            }
125        }
126
127        // Send request:
128        $result = $this->client->send();
129        $resultBody = $result->getBody();
130        if (!$result->isSuccess()) {
131            $decodedError = json_decode($resultBody, true);
132            throw new ApiException($decodedError ?: $resultBody);
133        }
134        if ($cacheKey) {
135            $this->putCachedData($cacheKey, $resultBody);
136        }
137        return $resultBody;
138    }
139}