Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
65.00% covered (warning)
65.00%
13 / 20
50.00% covered (danger)
50.00%
3 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
Connector
65.00% covered (warning)
65.00%
13 / 20
50.00% covered (danger)
50.00%
3 / 6
10.74
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 lookupDoi
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 lookupIssns
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 search
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getUri
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 request
80.00% covered (warning)
80.00%
8 / 10
0.00% covered (danger)
0.00%
0 / 1
2.03
1<?php
2
3/**
4 * BrowZine connector.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2017.
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  Search
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
28 */
29
30namespace VuFindSearch\Backend\BrowZine;
31
32use Laminas\Http\Client as HttpClient;
33
34/**
35 * BrowZine connector.
36 *
37 * @category VuFind
38 * @package  Search
39 * @author   Demian Katz <demian.katz@villanova.edu>
40 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
41 * @link     https://vufind.org
42 */
43class Connector implements \Laminas\Log\LoggerAwareInterface
44{
45    use \VuFind\Log\LoggerAwareTrait;
46
47    /**
48     * The base URI for API requests
49     *
50     * @var string
51     */
52    protected $base = 'https://api.thirdiron.com/public/v1/';
53
54    /**
55     * The HTTP Request client used for API transactions
56     *
57     * @var HttpClient
58     */
59    protected $client;
60
61    /**
62     * The API access token
63     *
64     * @var string
65     */
66    protected $token;
67
68    /**
69     * The library ID number to use
70     *
71     * @var string
72     */
73    protected $libraryId;
74
75    /**
76     * Constructor
77     *
78     * Sets up the BrowZine Client
79     *
80     * @param HttpClient $client HTTP client
81     * @param string     $token  API access token
82     * @param string     $id     Library ID number
83     */
84    public function __construct(HttpClient $client, $token, $id)
85    {
86        $this->client = $client;
87        $this->token = $token;
88        $this->libraryId = $id;
89    }
90
91    /**
92     * Perform a DOI lookup
93     *
94     * @param string $doi            DOI
95     * @param bool   $includeJournal Include journal data in response?
96     *
97     * @return mixed
98     */
99    public function lookupDoi($doi, $includeJournal = false)
100    {
101        // Documentation says URL encoding of DOI is not necessary.
102        return $this->request(
103            'articles/doi/' . $doi,
104            $includeJournal ? ['include' => 'journal'] : []
105        );
106    }
107
108    /**
109     * Perform an ISSN lookup.
110     *
111     * @param string|array $issns ISSN(s) to look up.
112     *
113     * @return mixed
114     */
115    public function lookupIssns($issns)
116    {
117        return $this->request('search', ['issns' => implode(',', (array)$issns)]);
118    }
119
120    /**
121     * Perform a search
122     *
123     * @param string $query Search query
124     *
125     * @return mixed
126     */
127    public function search($query)
128    {
129        return $this->request('search', compact('query'));
130    }
131
132    /**
133     * Get a full request URL for a relative path
134     *
135     * @param string $path URL path for service
136     *
137     * @return string
138     */
139    protected function getUri($path)
140    {
141        return $this->base . 'libraries/' . $this->libraryId . '/' . $path;
142    }
143
144    /**
145     * Perform an API request and return the response body
146     *
147     * @param string $path   URL path for service
148     * @param array  $params GET parameters
149     *
150     * @return mixed
151     */
152    protected function request($path, $params = [])
153    {
154        $params['access_token'] = $this->token;
155        $uri = $this->getUri($path);
156        $this->debug('BrowZine request: ' . $uri);
157        $this->client->setUri($uri);
158        $this->client->setParameterGet($params);
159        $result = $this->client->send();
160        if ($result->isSuccess()) {
161            return json_decode($result->getBody(), true);
162        } else {
163            $this->debug('API failure; status: ' . $result->getStatusCode());
164        }
165        return null;
166    }
167}