Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
45.45% covered (danger)
45.45%
5 / 11
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
AbstractContent
45.45% covered (danger)
45.45%
5 / 11
33.33% covered (danger)
33.33%
1 / 3
18.39
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 isActive
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
3.14
 getContent
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3/**
4 * Reviews tab
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  RecordTabs
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_tabs Wiki
28 */
29
30namespace VuFind\RecordTab;
31
32use VuFind\Content\Loader;
33
34/**
35 * Reviews tab
36 *
37 * @category VuFind
38 * @package  RecordTabs
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:plugins:record_tabs Wiki
42 */
43abstract class AbstractContent extends AbstractBase
44{
45    /**
46     * Content loader
47     *
48     * @var Loader
49     */
50    protected $loader;
51
52    /**
53     * Should we hide the tab if no content is found?
54     *
55     * @var bool
56     */
57    protected $hideIfEmpty;
58
59    /**
60     * Cache for results.
61     *
62     * @var array
63     */
64    protected $results = null;
65
66    /**
67     * Constructor
68     *
69     * @param Loader $loader      Content loader (null to disable)
70     * @param bool   $hideIfEmpty Should we hide the tab if no content is found?
71     * (Note that turning this on has performance implications).
72     */
73    public function __construct(Loader $loader = null, $hideIfEmpty = false)
74    {
75        $this->loader = $loader;
76        $this->hideIfEmpty = $hideIfEmpty;
77    }
78
79    /**
80     * Is this tab active?
81     *
82     * @return bool
83     */
84    public function isActive()
85    {
86        // Depending on the "hide if empty" setting, we either want to check if
87        // the API results are empty (an expensive operation) or if we have an
88        // ISBN that can be used to retrieve API results (much less expensive,
89        // but also less effective at consistently suppressing empty tabs).
90        $check = $this->hideIfEmpty
91            ? $this->getContent()
92            : $this->getRecordDriver()->tryMethod('getCleanISBN');
93        return !(null === $this->loader || empty($check));
94    }
95
96    /**
97     * Get content for ISBN.
98     *
99     * @return array
100     */
101    public function getContent()
102    {
103        if (null === $this->results) {
104            $isbn = $this->getRecordDriver()->tryMethod('getCleanISBN');
105            $this->results = (null === $this->loader || empty($isbn))
106                ? [] : $this->loader->loadByIsbn($isbn);
107        }
108        return $this->results;
109    }
110}