Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
78.43% covered (warning)
78.43%
40 / 51
25.00% covered (danger)
25.00%
1 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
BrowZine
78.43% covered (warning)
78.43%
40 / 51
25.00% covered (danger)
25.00%
1 / 4
17.26
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
 arrayKeyAvailable
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
5.05
 getLinks
94.44% covered (success)
94.44%
17 / 18
0.00% covered (danger)
0.00%
0 / 1
6.01
 getDoiServices
59.09% covered (warning)
59.09%
13 / 22
0.00% covered (danger)
0.00%
0 / 1
3.62
1<?php
2
3/**
4 * BrowZine DOI linker
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2018.
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  DOI
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/wiki/development:plugins:record_drivers Wiki
28 */
29
30namespace VuFind\DoiLinker;
31
32use VuFind\I18n\Translator\TranslatorAwareInterface;
33use VuFindSearch\Backend\BrowZine\Command\LookupDoiCommand;
34use VuFindSearch\Service;
35
36use function in_array;
37
38/**
39 * BrowZine DOI linker
40 *
41 * @category VuFind
42 * @package  DOI
43 * @author   Demian Katz <demian.katz@villanova.edu>
44 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
45 * @link     https://vufind.org/wiki/development:plugins:record_drivers Wiki
46 */
47class BrowZine implements DoiLinkerInterface, TranslatorAwareInterface
48{
49    use \VuFind\I18n\Translator\TranslatorAwareTrait;
50
51    /**
52     * Search service
53     *
54     * @var Service
55     */
56    protected $searchService;
57
58    /**
59     * Configuration options
60     *
61     * @var array
62     */
63    protected $config;
64
65    /**
66     * Configured DOI services
67     *
68     * @var array
69     */
70    protected $doiServices;
71
72    /**
73     * Constructor
74     *
75     * @param Service $searchService Search service
76     * @param array   $config        Configuration settings
77     * @param array   $doiServices   Configured DOI services
78     */
79    public function __construct(
80        Service $searchService,
81        array $config = [],
82        array $doiServices = []
83    ) {
84        $this->searchService = $searchService;
85        $this->config = $config;
86        $this->doiServices = $doiServices;
87    }
88
89    /**
90     * Check if an array key is available in the data and allowed by filter settings.
91     *
92     * @param string $key  Key to check
93     * @param array  $data Available data
94     *
95     * @return bool
96     */
97    protected function arrayKeyAvailable(string $key, ?array $data): bool
98    {
99        if (empty($data[$key])) {
100            return false;
101        }
102        switch (strtolower(trim($this->config['filterType'] ?? 'none'))) {
103            case 'include':
104                return in_array($key, (array)($this->config['filter'] ?? []));
105            case 'exclude':
106                return !in_array($key, (array)($this->config['filter'] ?? []));
107            default:
108        }
109        // If we got this far, no filter setting is applied, so the option is legal:
110        return true;
111    }
112
113    /**
114     * Given an array of DOIs, perform a lookup and return an associative array
115     * of arrays, keyed by DOI. Each array contains one or more associative arrays
116     * with required 'link' (URL to related resource) and 'label' (display text)
117     * keys and an optional 'icon' (URL to icon graphic) or localIcon (name of
118     * configured icon in theme) key.
119     *
120     * @param array $doiArray DOIs to look up
121     *
122     * @return array
123     */
124    public function getLinks(array $doiArray)
125    {
126        $response = [];
127        $localIcons = !empty($this->config['local_icons']);
128        foreach ($doiArray as $doi) {
129            $command = new LookupDoiCommand('BrowZine', $doi);
130            $result = $this->searchService->invoke($command)->getResult();
131            $data = $result['data'] ?? null;
132            foreach ($this->getDoiServices() as $key => $config) {
133                if ($this->arrayKeyAvailable($key, $data)) {
134                    $result = [
135                        'link' => $data[$key],
136                        'label' => $this->translate($config['linkText']),
137                        'data' => $data,
138                    ];
139                    if (!$localIcons && !empty($config['icon'])) {
140                        $result['icon'] = $config['icon'];
141                    } else {
142                        $result['localIcon'] = $config['localIcon'];
143                    }
144                    $response[$doi][] = $result;
145                }
146            }
147        }
148        return $response;
149    }
150
151    /**
152     * Get an array of DOI services and their configuration
153     *
154     * @return array
155     */
156    protected function getDoiServices(): array
157    {
158        if (empty($this->doiServices)) {
159            $baseIconUrl = 'https://assets.thirdiron.com/images/integrations/';
160            return [
161                'browzineWebLink' => [
162                    'linkText' => 'View Complete Issue',
163                    'localIcon' => 'browzine-issue',
164                    'icon' => $baseIconUrl . 'browzine-open-book-icon.svg',
165                ],
166                'fullTextFile' => [
167                    'linkText' => 'PDF Full Text',
168                    'localIcon' => 'browzine-pdf',
169                    'icon' => $baseIconUrl . 'browzine-pdf-download-icon.svg',
170                ],
171            ];
172        }
173        $result = [];
174        foreach ($this->doiServices as $key => $config) {
175            $parts = explode('|', $config);
176            $result[$key] = [
177                'linkText' => $parts[0],
178                'localIcon' => $parts[1],
179                'icon' => $parts[2] ?? null,
180            ];
181        }
182        return $result;
183    }
184}