Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
RetrieveBatchCommand
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
4 / 4
8
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 getArguments
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
5
 getRecordIdentifiers
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * Retrieve a batch of documents command.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2010.
9 * Copyright (C) The National Library of Finland 2021.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2,
13 * as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
23 *
24 * @category VuFind
25 * @package  Search
26 * @author   David Maus <maus@hab.de>
27 * @author   Aleksi Peebles <aleksi.peebles@helsinki.fi>
28 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
29 * @link     https://vufind.org
30 */
31
32namespace VuFindSearch\Command;
33
34use VuFindSearch\Backend\BackendInterface;
35use VuFindSearch\Feature\RetrieveBatchInterface;
36use VuFindSearch\ParamBag;
37
38/**
39 * Retrieve a batch of documents command.
40 *
41 * @category VuFind
42 * @package  Search
43 * @author   David Maus <maus@hab.de>
44 * @author   Aleksi Peebles <aleksi.peebles@helsinki.fi>
45 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
46 * @link     https://vufind.org
47 */
48class RetrieveBatchCommand extends CallMethodCommand
49{
50    /**
51     * Record identifiers.
52     *
53     * @var array
54     */
55    protected $ids;
56
57    /**
58     * RetrieveBatchCommand constructor.
59     *
60     * @param string    $backendId Search backend identifier
61     * @param array     $ids       Record identifiers
62     * @param ?ParamBag $params    Search backend parameters
63     */
64    public function __construct(
65        string $backendId,
66        array $ids,
67        ?ParamBag $params = null
68    ) {
69        $this->ids = $ids;
70        parent::__construct(
71            $backendId,
72            RetrieveBatchInterface::class,
73            'retrieveBatch',
74            $params
75        );
76    }
77
78    /**
79     * Return search backend interface method arguments.
80     *
81     * @return array
82     */
83    public function getArguments(): array
84    {
85        return [
86            $this->getRecordIdentifiers(),
87            $this->getSearchParameters(),
88        ];
89    }
90
91    /**
92     * Execute command on backend.
93     *
94     * @param BackendInterface $backend Backend
95     *
96     * @return CommandInterface Command instance for method chaining
97     */
98    public function execute(BackendInterface $backend): CommandInterface
99    {
100        // If the backend implements the RetrieveBatchInterface, we can load
101        // all the records at once.
102        if ($backend instanceof RetrieveBatchInterface) {
103            return parent::execute($backend);
104        }
105
106        // Otherwise, we need to load them one at a time and aggregate them.
107
108        $response = false;
109        foreach ($this->getRecordIdentifiers() as $id) {
110            $next = $backend->retrieve($id, $this->params);
111            if (!$response) {
112                $response = $next;
113            } elseif ($record = $next->first()) {
114                $response->add($record);
115            }
116        }
117
118        return $this->finalizeExecution($response);
119    }
120
121    /**
122     * Return record identifiers.
123     *
124     * @return array
125     */
126    public function getRecordIdentifiers(): array
127    {
128        return $this->ids;
129    }
130}