Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
Guardian
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 1
56
0.00% covered (danger)
0.00%
0 / 1
 loadByIsbn
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 1
56
1<?php
2
3/**
4 * Guardian review 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\Reviews;
31
32use function strlen;
33
34/**
35 * Guardian review 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 Guardian extends \VuFind\Content\AbstractBase
44{
45    /**
46     * Guardian Reviews
47     *
48     * This method is responsible for connecting to the Guardian and abstracting
49     * reviews for the specific ISBN.
50     *
51     * @param string           $key     API key (unused here)
52     * @param \VuFindCode\ISBN $isbnObj ISBN object
53     *
54     * @throws \Exception
55     * @return array     Returns array with review data.
56     * @author Eoghan Ó Carragáin <eoghan.ocarragain@gmail.com>
57     *
58     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
59     */
60    public function loadByIsbn($key, \VuFindCode\ISBN $isbnObj)
61    {
62        // Base request URL:
63        $url
64            = 'http://content.guardianapis.com/search?order-by=newest&format=json' .
65                '&show-fields=all&reference=isbn%2F' . $isbnObj->get13();
66
67        // Only add api-key if one has been provided in config.ini. If no key is
68        // provided, a link to the Guardian can still be shown.
69        if (strlen($key) > 0) {
70            $url = $url . '&api-key=' . $key;
71        }
72
73        $this->debug('Guardian request: ' . $url);
74
75        // Find out if there are any reviews:
76        $result = $this->getHttpClient($url)->send();
77
78        // Was the request successful?
79        if ($result->isSuccess()) {
80            // parse json from response
81            $data = json_decode($result->getBody(), true);
82            if ($data) {
83                $result = [];
84                $i = 0;
85                foreach ($data['response']['results'] as $review) {
86                    $result[$i]['Date'] = $review['webPublicationDate'];
87                    $result[$i]['Summary'] = $review['fields']['headline'] . '. ' .
88                        preg_replace(
89                            '/<p>|<p [^>]*>|<\/p>/',
90                            '',
91                            html_entity_decode($review['fields']['trailText'])
92                        );
93                    $result[$i]['ReviewURL'] = $review['fields']['shortUrl'];
94
95                    // TODO: Make this configurable (or store it locally), so users
96                    //       running VuFind behind SSL don't get warnings due to
97                    //       inclusion of this non-SSL image URL:
98                    $poweredImage
99                        = 'http://image.guardian.co.uk/sys-images/Guardian/' .
100                        'Pix/pictures/2010/03/01/poweredbyguardianBLACK.png';
101
102                    $result[$i]['Copyright'] = '<a href="' .
103                        $review['fields']['shortUrl'] . '" target="new">' .
104                        "<img src=\"{$poweredImage}\" " .
105                        'alt="Powered by the Guardian" /></a>';
106
107                    $result[$i]['Source'] = $review['fields']['byline'];
108                    // Only return Content if the body tag contains a usable review
109                    $redist = 'Redistribution rights for this field are unavailable';
110                    if (
111                        (strlen($review['fields']['body']) > 0)
112                        && (!strstr($review['fields']['body'], $redist))
113                    ) {
114                        $result[$i]['Content'] = $review['fields']['body'];
115                    }
116                    $i++;
117                }
118                return $result;
119            } else {
120                throw new \Exception('Could not parse Guardian response.');
121            }
122        } else {
123            return [];
124        }
125    }
126}