Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
NewSearchItems
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
5 / 5
7
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setOptions
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getFromRecord
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getFromSearch
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 buildChannelFromParams
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * Backend-driven new items channel provider.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2024.
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  Channels
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\ChannelProvider;
31
32use VuFind\Controller\Plugin\NewItems;
33use VuFind\I18n\Translator\TranslatorAwareInterface;
34use VuFind\RecordDriver\AbstractBase as RecordDriver;
35use VuFind\Search\Base\Params;
36use VuFind\Search\Base\Results;
37use VuFindSearch\Command\SearchCommand;
38
39use function count;
40
41/**
42 * Backend-driven new items channel provider.
43 *
44 * @category VuFind
45 * @package  Channels
46 * @author   Demian Katz <demian.katz@villanova.edu>
47 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
48 * @link     https://vufind.org/wiki/development Wiki
49 */
50class NewSearchItems extends AbstractChannelProvider implements TranslatorAwareInterface
51{
52    use \VuFind\I18n\Translator\TranslatorAwareTrait;
53
54    /**
55     * Number of results to include in each channel.
56     *
57     * @var int
58     */
59    protected $channelSize;
60
61    /**
62     * Maximum age (in days) of results to retrieve.
63     *
64     * @var int
65     */
66    protected $maxAge;
67
68    /**
69     * Sort order for results.
70     *
71     * @var string
72     */
73    protected $sort;
74
75    /**
76     * Constructor
77     *
78     * @param \VuFindSearch\Service               $searchService Search service
79     * @param \VuFind\Search\Params\PluginManager $paramManager  Params manager
80     * @param NewItems                            $newItems      New items helper
81     * @param array                               $options       Settings (optional)
82     */
83    public function __construct(
84        protected \VuFindSearch\Service $searchService,
85        protected \VuFind\Search\Params\PluginManager $paramManager,
86        protected NewItems $newItems,
87        array $options = []
88    ) {
89        $this->setOptions($options);
90    }
91
92    /**
93     * Set the options for the provider.
94     *
95     * @param array $options Options
96     *
97     * @return void
98     */
99    public function setOptions(array $options)
100    {
101        $this->channelSize = $options['channelSize'] ?? 20;
102        $this->maxAge = $options['maxAge'] ?? 30;
103        $this->sort = $options['sort'] ?? 'first_indexed desc';
104    }
105
106    /**
107     * Return channel information derived from a record driver object.
108     *
109     * @param RecordDriver $driver       Record driver
110     * @param string       $channelToken Token identifying a single specific channel
111     * to load (if omitted, all channels will be loaded) -- not used in this provider
112     *
113     * @return array
114     *
115     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
116     */
117    public function getFromRecord(RecordDriver $driver, $channelToken = null)
118    {
119        $params = $this->paramManager->get($driver->getSourceIdentifier());
120        $channel = $this->buildChannelFromParams($params);
121        return (count($channel['contents']) > 0) ? [$channel] : [];
122    }
123
124    /**
125     * Return channel information derived from a search results object.
126     *
127     * @param Results $results      Search results
128     * @param string  $channelToken Token identifying a single specific channel
129     * to load (if omitted, all channels will be loaded) -- not used in this provider
130     *
131     * @return array
132     *
133     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
134     */
135    public function getFromSearch(Results $results, $channelToken = null)
136    {
137        $params = $this->paramManager->get($results->getParams()->getSearchClassId());
138        $channel = $this->buildChannelFromParams($params);
139        return (count($channel['contents']) > 0) ? [$channel] : [];
140    }
141
142    /**
143     * Add a new filter to an existing search results object to populate a
144     * channel.
145     *
146     * @param Params $params Search parameter object
147     *
148     * @return array
149     */
150    protected function buildChannelFromParams(Params $params)
151    {
152        $retVal = [
153            'title' => $this->translate('New Items'),
154            'providerId' => $this->providerId,
155        ];
156        $params->addHiddenFilter($this->newItems->getSolrFilter($this->maxAge));
157        $params->setSort($this->sort, true);
158        $query = $params->getQuery();
159        $paramBag = $params->getBackendParameters();
160        $command = new SearchCommand(
161            $params->getSearchClassId(),
162            $query,
163            limit: $this->channelSize,
164            params: $paramBag
165        );
166        $result = $this->searchService->invoke($command)->getResult()->getRecords();
167        $retVal['contents'] = $this->summarizeRecordDrivers($result);
168        return $retVal;
169    }
170}