Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Booksite
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 2
20
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
 loadByIsbn
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3/**
4 * Booksite 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
32/**
33 * Booksite review content loader.
34 *
35 * @category VuFind
36 * @package  Content
37 * @author   Demian Katz <demian.katz@villanova.edu>
38 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
39 * @link     https://vufind.org/wiki/development Wiki
40 */
41class Booksite extends \VuFind\Content\AbstractBase
42{
43    /**
44     * Base URL for Booksite
45     *
46     * @var string
47     */
48    protected $url;
49
50    /**
51     * API key for Booksite
52     *
53     * @var string
54     */
55    protected $apiKey;
56
57    /**
58     * Constructor
59     *
60     * @param string $url    Base URL for Booksite
61     * @param string $apiKey API key for Booksite
62     */
63    public function __construct($url, $apiKey)
64    {
65        $this->url = $url;
66        $this->apiKey = $apiKey;
67    }
68
69    /**
70     * Booksite
71     *
72     * Connects to Booksite's API and retrieves reviews for the specific ISBN
73     *
74     * @param string           $key     API key (unused here)
75     * @param \VuFindCode\ISBN $isbnObj ISBN object
76     *
77     * @throws \Exception
78     * @return array     Returns array with review data.
79     * @author Joe Atzberger
80     *
81     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
82     */
83    public function loadByIsbn($key, \VuFindCode\ISBN $isbnObj)
84    {
85        $reviews = []; // Initialize return value
86
87        $isn = $this->getIsbn10($isbnObj);
88        $url = $this->url . '/poca/book/tradereviews?apikey=' . $this->apiKey
89            . '&ean=' . $isn;
90        $response = $this->getHttpClient($url)->send();
91        if (!$response->isSuccess()) {
92            $this->logWarning(
93                'Reviews: ' . $response->getStatusCode() . ' '
94                . $response->getReasonPhrase() . " $url"
95            );
96            return $reviews;    // still empty
97        }
98        $this->debug(
99            'Reviews: ' . $response->getStatusCode() . ' '
100            . $response->getReasonPhrase() . " $url"
101        );
102
103        $i = 0;
104        $json = json_decode($response->getBody());
105        foreach ($json as $source => $values) {
106            $reviews[$i]['Source'] = $source;
107            $reviews[$i]['Date'] = (string)$values->reviewDate;
108            $reviews[$i]['Content'] = (string)$values->reviewText;
109            $i++;
110        }
111
112        return $reviews;
113    }
114}