Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ObalkyKnih
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 3
90
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
 getUrl
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
42
 getMetadata
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3/**
4 * Class ObalkyKnih
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Moravian Library 2019.
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   Josef Moravec <moravec@mzk.cz>
26 * @license  https://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org/wiki/development Wiki
28 */
29
30declare(strict_types=1);
31
32namespace VuFind\Content\Covers;
33
34use VuFind\Content\ObalkyKnihService;
35
36/**
37 * Class ObalkyKnih
38 *
39 * @category VuFind
40 * @package  Content
41 * @author   Josef Moravec <moravec@mzk.cz>
42 * @license  https://opensource.org/licenses/gpl-2.0.php GNU General Public License
43 * @link     https://vufind.org/wiki/development Wiki
44 */
45class ObalkyKnih extends \VuFind\Content\AbstractCover
46{
47    /**
48     * Obalky knih service
49     *
50     * @var ObalkyKnihService
51     */
52    protected $service;
53
54    /**
55     * Constructor
56     *
57     * @param ObalkyKnihService $service Service for getting metadata from
58     * obalkyknih.cz
59     */
60    public function __construct(ObalkyKnihService $service)
61    {
62        $this->supportsIsbn = true;
63        $this->supportsIssn = true;
64        $this->supportsIsmn = true;
65        $this->supportsOclc = true;
66        $this->supportsUpc = true;
67        $this->supportsNbn = true;
68        $this->supportsRecordid = true;
69        $this->supportsUuid = true;
70        $this->cacheAllowed = false;
71        $this->directUrls = true;
72        $this->mandatoryBacklinkLocations = ['core'];
73
74        $this->service = $service;
75    }
76
77    /**
78     * Get image URL for a particular API key and set of IDs (or false if invalid).
79     *
80     * @param string $key  API key
81     * @param string $size Size of image to load (small/medium/large)
82     * @param array  $ids  Associative array of identifiers (keys may include 'isbn'
83     * pointing to an ISBN object and 'issn' pointing to a string)
84     *
85     * @return string|bool
86     *
87     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
88     */
89    public function getUrl($key, $size, $ids)
90    {
91        $data = $this->service->getData($ids);
92        if (!isset($data)) {
93            return false;
94        }
95        switch ($size) {
96            case 'small':
97                $imageUrl = $data->cover_icon_url ?? false;
98                break;
99            case 'medium':
100                $imageUrl = $data->cover_medium_url ?? false;
101                break;
102            case 'large':
103                $imageUrl = $data->cover_preview510_url ?? false;
104                break;
105            default:
106                $imageUrl = $data->cover_medium_url ?? false;
107                break;
108        }
109        return $imageUrl;
110    }
111
112    /**
113     * Get cover metadata for a particular API key and set of IDs (or empty array).
114     *
115     * @param string $key  API key
116     * @param string $size Size of image to load (small/medium/large)
117     * @param array  $ids  Associative array of identifiers (keys may include 'isbn'
118     * pointing to an ISBN object, 'issn' pointing to a string and 'oclc' pointing
119     * to an OCLC number string)
120     *
121     * @return array Array with keys: url, backlink_url, backlink_text
122     */
123    public function getMetadata(?string $key, string $size, array $ids)
124    {
125        $url = $this->getUrl($key, $size, $ids);
126        if ($url) {
127            $data = $this->service->getData($ids);
128            return [
129                'url' => $url,
130                'backlink_url' => $data->backlink_url ?? '',
131                'backlink_text' => 'ObálkyKnih.cz',
132            ];
133        }
134        return [];
135    }
136}