Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Loader
0.00% covered (danger)
0.00%
0 / 21
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 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getIsbnObject
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 loadByIsbn
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2
3/**
4 * Third-party content loader
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2010.
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   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 Wiki
28 */
29
30namespace VuFind\Content;
31
32use VuFind\ServiceManager\AbstractPluginManager;
33
34/**
35 * Third-party content loader
36 *
37 * @category VuFind
38 * @package  Content
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/wiki/development Wiki
42 */
43class Loader
44{
45    /**
46     * Plug-in loader
47     *
48     * @var AbstractPluginManager
49     */
50    protected $loader;
51
52    /**
53     * Provider information
54     *
55     * @var string
56     */
57    protected $providers;
58
59    /**
60     * Constructor
61     *
62     * @param AbstractPluginManager $loader    Plugin loader for content
63     * @param string                $providers Provider information
64     */
65    public function __construct(AbstractPluginManager $loader, $providers = '')
66    {
67        $this->loader = $loader;
68        $this->providers = $providers;
69    }
70
71    /**
72     * Build an ISBN object; return false if value is invalid.
73     *
74     * @param string $isbn ISBN
75     *
76     * @return \VuFindCode\ISBN|bool
77     */
78    protected function getIsbnObject($isbn)
79    {
80        // We can't proceed without an ISBN:
81        return (empty($isbn))
82            ? false : new \VuFindCode\ISBN($isbn);
83    }
84
85    /**
86     * Retrieve results for the providers specified.
87     *
88     * @param string $isbn ISBN to use for lookup
89     *
90     * @return array
91     */
92    public function loadByIsbn($isbn)
93    {
94        $results = [];
95        if (!($isbnObj = $this->getIsbnObject($isbn))) {
96            return $results;
97        }
98
99        // Fetch from provider
100        $providers = explode(',', $this->providers);
101        foreach ($providers as $provider) {
102            $parts = explode(':', trim($provider));
103            $provider = $parts[0];
104            if (!empty($provider)) {
105                $key = $parts[1] ?? '';
106                try {
107                    $plugin = $this->loader->get($provider);
108                    $results[$provider] = $plugin->loadByIsbn($key, $isbnObj);
109
110                    // If the current provider had no valid data, store nothing:
111                    if (empty($results[$provider])) {
112                        unset($results[$provider]);
113                    }
114                } catch (\Exception $e) {
115                    // Ignore exceptions:
116                    error_log($e->getMessage());
117                    unset($results[$provider]);
118                }
119            }
120        }
121
122        return $results;
123    }
124}