Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
VersionAwareTrait
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 2
90
0.00% covered (danger)
0.00%
0 / 1
 getOtherVersionCount
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
30
 getVersions
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3/**
4 * Logic for record versions support. Depends on versionAwareInterface.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2022.
9 * Copyright (C) The National Library of Finland 2020.
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  RecordDrivers
26 * @author   Ere Maijala <ere.maijala@helsinki.fi>
27 * @author   Sudharma Kellampalli <skellamp@villanova.edu>
28 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
29 * @link     https://vufind.org Main Site
30 */
31
32namespace VuFind\RecordDriver\Feature;
33
34use VuFindSearch\Command\SearchCommand;
35use VuFindSearch\Query\WorkKeysQuery;
36
37/**
38 * Logic for record versions support.
39 *
40 * @category VuFind
41 * @package  RecordDrivers
42 * @author   Ere Maijala <ere.maijala@helsinki.fi>
43 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
44 * @link     https://vufind.org Main Site
45 */
46trait VersionAwareTrait
47{
48    /**
49     * Cached result of other versions (work expressions) count
50     *
51     * @var int
52     */
53    protected $otherVersionsCount = null;
54
55    /**
56     * Cached result of other versions (work expressions)
57     *
58     * @var \VuFindSearch\Response\RecordCollectionInterface
59     */
60    protected $otherVersions;
61
62    /**
63     * Return count of other versions available
64     *
65     * @return int
66     */
67    public function getOtherVersionCount()
68    {
69        if (null === $this->searchService) {
70            return false;
71        }
72
73        if (!isset($this->otherVersionsCount)) {
74            if (!($keys = $this->tryMethod('getWorkKeys'))) {
75                if (!($this instanceof VersionAwareInterface)) {
76                    throw new \Exception(
77                        'VersionAwareTrait requires VersionAwareInterface'
78                    );
79                }
80                return false;
81            }
82
83            $command = new SearchCommand(
84                $this->getSourceIdentifier(),
85                new WorkKeysQuery($this->getUniqueID(), false, $keys),
86                0,
87                0
88            );
89            $results = $this->searchService->invoke($command)->getResult();
90            $this->otherVersionsCount = $results->getTotal();
91        }
92        return $this->otherVersionsCount;
93    }
94
95    /**
96     * Retrieve versions as a search result
97     *
98     * @param bool $includeSelf Whether to include this record
99     * @param int  $count       Maximum number of records to display
100     * @param int  $offset      Start position (0-based)
101     *
102     * @return \VuFindSearch\Response\RecordCollectionInterface
103     */
104    public function getVersions($includeSelf = false, $count = 20, $offset = 0)
105    {
106        if (null === $this->searchService || !($keys = $this->getWorkKeys())) {
107            return false;
108        }
109
110        if (!isset($this->otherVersions)) {
111            $command = new SearchCommand(
112                $this->getSourceIdentifier(),
113                new WorkKeysQuery($this->getUniqueID(), false, $keys),
114                $offset,
115                $count
116            );
117            $this->otherVersions = $this->searchService->invoke($command)->getResult();
118        }
119        return $this->otherVersions;
120    }
121}