Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
11 / 11
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractBase
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
11 / 11
14
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getTargetIdentifier
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setTargetIdentifier
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 finalizeExecution
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 validateBackend
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 isExecuted
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getResult
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getSearchParameters
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setSearchParameters
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getContext
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setContext
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * Abstract base 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\Exception\LogicException;
34use VuFindSearch\Exception\RuntimeException;
35use VuFindSearch\ParamBag;
36
37/**
38 * Abstract base command.
39 *
40 * @category VuFind
41 * @package  Search
42 * @author   Aleksi Peebles <aleksi.peebles@helsinki.fi>
43 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
44 * @link     https://vufind.org
45 */
46abstract class AbstractBase implements CommandInterface
47{
48    /**
49     * Search backend identifier
50     *
51     * @var string
52     */
53    protected $backendId;
54
55    /**
56     * Command context
57     *
58     * @var mixed
59     */
60    protected $context;
61
62    /**
63     * Search backend parameters
64     *
65     * @var ParamBag
66     */
67    protected $params;
68
69    /**
70     * Was the command executed?
71     *
72     * @var bool
73     */
74    protected $executed = false;
75
76    /**
77     * Result of executed operation
78     *
79     * @var mixed
80     */
81    protected $result;
82
83    /**
84     * Constructor.
85     *
86     * @param string    $backendId Search backend identifier
87     * @param mixed     $context   Command context
88     * @param ?ParamBag $params    Search backend parameters
89     */
90    public function __construct(
91        string $backendId,
92        $context,
93        ?ParamBag $params = null
94    ) {
95        $this->backendId = $backendId;
96        $this->context = $context;
97        $this->params = $params ?: new ParamBag();
98    }
99
100    /**
101     * Return target backend identifier.
102     *
103     * @return string
104     */
105    public function getTargetIdentifier(): string
106    {
107        return $this->backendId;
108    }
109
110    /**
111     * Set target backend identifier.
112     *
113     * @param string $identifier New identifier
114     *
115     * @return void
116     */
117    public function setTargetIdentifier(string $identifier): void
118    {
119        $this->backendId = $identifier;
120    }
121
122    /**
123     * Save a result, flag the command as executed, and return the command object;
124     * useful as the final step in execute() implementations.
125     *
126     * @param mixed $result Result of execution.
127     *
128     * @return CommandInterface
129     */
130    protected function finalizeExecution($result): CommandInterface
131    {
132        $this->result = $result;
133        $this->executed = true;
134        return $this;
135    }
136
137    /**
138     * Validate that the provided backend matches the expected target identifier.
139     *
140     * @param BackendInterface $backend Backend instance
141     *
142     * @return void
143     * @throws RuntimeException
144     */
145    protected function validateBackend(BackendInterface $backend): void
146    {
147        if (($backendId = $backend->getIdentifier()) !== $this->backendId) {
148            throw new RuntimeException(
149                "Expected backend instance $this->backendId instead of $backendId"
150            );
151        }
152    }
153
154    /**
155     * Was the command executed?
156     *
157     * @return bool
158     */
159    public function isExecuted(): bool
160    {
161        return $this->executed;
162    }
163
164    /**
165     * Return result of executed operation.
166     *
167     * @throws LogicException Command was not yet executed
168     *
169     * @return mixed
170     */
171    public function getResult()
172    {
173        if (!$this->isExecuted()) {
174            throw new LogicException('Command was not yet executed');
175        }
176        return $this->result ?? null;
177    }
178
179    /**
180     * Return search parameters.
181     *
182     * @return ParamBag
183     */
184    public function getSearchParameters(): ParamBag
185    {
186        return $this->params;
187    }
188
189    /**
190     * Set search parameters.
191     *
192     * @param ParamBag $params Params
193     *
194     * @return void
195     */
196    public function setSearchParameters(ParamBag $params): void
197    {
198        $this->params = $params;
199    }
200
201    /**
202     * Return command context.
203     *
204     * @return mixed
205     */
206    public function getContext()
207    {
208        return $this->context;
209    }
210
211    /**
212     * Set command context.
213     *
214     * @param mixed $context Context
215     *
216     * @return void
217     */
218    public function setContext($context)
219    {
220        $this->context = $context;
221    }
222}