Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Orb
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
2 / 2
8
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 getUrl
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
7
1<?php
2
3/**
4 * Orb cover content loader.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2021.
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  Content
25 * @author   Frédéric Demians <f.demians@tamil.fr>
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\Content\Covers;
31
32/**
33 * Orb cover content loader.
34 *
35 * @category VuFind
36 * @package  Content
37 * @author   Frédéric Demians <f.demians@tamil.fr>
38 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
39 * @link     https://vufind.org/wiki/development:plugins:content_provider_components
40 */
41class Orb extends \VuFind\Content\AbstractCover implements \VuFind\Http\CachingDownloaderAwareInterface
42{
43    use \VuFind\Http\CachingDownloaderAwareTrait;
44
45    /**
46     * Base URL for Orb API
47     *
48     * @var string
49     */
50    protected $url;
51
52    /**
53     * API user for Orb
54     *
55     * @var string
56     */
57    protected $apiUser;
58
59    /**
60     * API key for Orb
61     *
62     * @var string
63     */
64    protected $apiKey;
65
66    /**
67     * Constructor
68     *
69     * @param string $url     Base URL for Orb
70     * @param string $apiUser API key for Orb
71     * @param string $apiKey  API key for Orb
72     */
73    public function __construct($url, $apiUser, $apiKey)
74    {
75        $this->url = $url;
76        $this->apiUser = $apiUser;
77        $this->apiKey = $apiKey;
78        $this->supportsIsbn = $this->cacheAllowed = true;
79        $this->cacheOptionsSection = 'OrbCover';
80    }
81
82    /**
83     * Get image URL for a particular API key and set of IDs (or false if invalid).
84     *
85     * @param string $key  API key
86     * @param string $size Size of image to load (small/medium/large)
87     * @param array  $ids  Associative array of identifiers (keys may include 'isbn'
88     * pointing to an ISBN object and 'issn' pointing to a string)
89     *
90     * @return string|bool
91     *
92     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
93     */
94    public function getUrl($key, $size, $ids)
95    {
96        if (!isset($ids['isbn'])) {
97            return false;
98        }
99        $ean = $ids['isbn']->get13();
100
101        $url = 'https://' . $this->apiUser . ':' . $this->apiKey . '@' .
102               $this->url . '/products?eans=' . $ean . '&sort=ean_asc';
103
104        if (!isset($this->cachingDownloader)) {
105            throw new \Exception('CachingDownloader initialization failed.');
106        }
107        $json = $this->cachingDownloader->downloadJson($url);
108        $imageVersion = $size == 'small' ? 'thumbnail' : 'original';
109        foreach ($json->data as $title) {
110            if (
111                $title->ean13 == $ean
112                && isset($title->images->front->$imageVersion->src)
113            ) {
114                return $title->images->front->$imageVersion->src;
115            }
116        }
117        return false;
118    }
119}