Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
CallMethodCommand
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
3 / 3
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
2
 getArguments
n/a
0 / 0
n/a
0 / 0
0
 getExtraRequestDetails
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2
3/**
4 * Call method command.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) The National Library of Finland 2021.
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  Search
25 * @author   Aleksi Peebles <aleksi.peebles@helsinki.fi>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org
28 */
29
30namespace VuFindSearch\Command;
31
32use VuFindSearch\Backend\BackendInterface;
33use VuFindSearch\Backend\Exception\BackendException;
34use VuFindSearch\Feature\ExtraRequestDetailsInterface;
35use VuFindSearch\ParamBag;
36
37use function call_user_func;
38
39/**
40 * Call method command.
41 *
42 * @category VuFind
43 * @package  Search
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 */
48abstract class CallMethodCommand extends AbstractBase
49{
50    /**
51     * Search backend interface
52     *
53     * @var string
54     */
55    protected $interface;
56
57    /**
58     * Search backend interface method
59     *
60     * @var string
61     */
62    protected $method;
63
64    /**
65     * Optional search details.
66     *
67     * @var ?array
68     */
69    protected $extraRequestDetails = null;
70
71    /**
72     * CallMethodCommand constructor.
73     *
74     * @param string    $backendId Search backend identifier
75     * @param string    $interface Search backend interface
76     * @param string    $method    Search backend interface method
77     * @param ?ParamBag $params    Search backend parameters
78     * @param mixed     $context   Command context. Optional, if left out the search
79     * interface method is used as the context.
80     */
81    public function __construct(
82        string $backendId,
83        string $interface,
84        string $method,
85        ?ParamBag $params = null,
86        $context = null
87    ) {
88        parent::__construct(
89            $backendId,
90            $context ?: $method,
91            $params
92        );
93        $this->interface = $interface;
94        $this->method = $method;
95    }
96
97    /**
98     * Return search backend interface method arguments.
99     *
100     * @return array
101     */
102    abstract public function getArguments(): array;
103
104    /**
105     * Get extra request details.
106     *
107     * @return ?array
108     */
109    public function getExtraRequestDetails(): ?array
110    {
111        return $this->extraRequestDetails;
112    }
113
114    /**
115     * Execute command on backend.
116     *
117     * @param BackendInterface $backend Backend
118     *
119     * @return CommandInterface Command instance for method chaining
120     */
121    public function execute(BackendInterface $backend): CommandInterface
122    {
123        $this->validateBackend($backend);
124        if (
125            !($backend instanceof $this->interface)
126            || !method_exists($this->interface, $this->method)
127        ) {
128            throw new BackendException(
129                "$this->backendId does not support $this->method()"
130            );
131        }
132        $args = $this->getArguments();
133        if ($backend instanceof ExtraRequestDetailsInterface) {
134            $backend->resetExtraRequestDetails();
135        }
136        $this->finalizeExecution(
137            call_user_func([$backend, $this->method], ...$args)
138        );
139        if ($backend instanceof ExtraRequestDetailsInterface) {
140            $this->extraRequestDetails = $backend->getExtraRequestDetails();
141        }
142        return $this;
143    }
144}