Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
RecordService
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 6
72
0.00% covered (danger)
0.00%
0 / 1
 getRecord
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getRecords
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 updateRecord
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 cleanup
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 deleteRecord
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 createEntity
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * Database service for Records.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2023.
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  Database
25 * @author   Sudharma Kellampalli <skellamp@villanova.edu>
26 * @author   Demian Katz <demian.katz@villanova.edu>
27 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
28 * @link     https://vufind.org/wiki/development:plugins:database_gateways Wiki
29 */
30
31namespace VuFind\Db\Service;
32
33use Exception;
34use VuFind\Db\Entity\RecordEntityInterface;
35use VuFind\Db\Table\DbTableAwareInterface;
36use VuFind\Db\Table\DbTableAwareTrait;
37
38/**
39 * Database service for Records.
40 *
41 * @category VuFind
42 * @package  Database
43 * @author   Sudharma Kellampalli <skellamp@villanova.edu>
44 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
45 * @link     https://vufind.org/wiki/development:plugins:database_gateways Wiki
46 */
47class RecordService extends AbstractDbService implements DbTableAwareInterface, RecordServiceInterface
48{
49    use DbTableAwareTrait;
50
51    /**
52     * Retrieve a record by id.
53     *
54     * @param string $id     Record ID
55     * @param string $source Record source
56     *
57     * @return ?RecordEntityInterface
58     */
59    public function getRecord(string $id, string $source): ?RecordEntityInterface
60    {
61        return $this->getDbTable('record')->findRecord($id, $source);
62    }
63
64    /**
65     * Retrieve records by ids.
66     *
67     * @param string[] $ids    Record IDs
68     * @param string   $source Record source
69     *
70     * @return RecordEntityInterface[] Array of record objects found
71     */
72    public function getRecords(array $ids, string $source): array
73    {
74        return $this->getDbTable('record')->findRecords($ids, $source);
75    }
76
77    /**
78     * Update an existing entry in the record table or create a new one.
79     *
80     * @param string $id      Record ID
81     * @param string $source  Data source
82     * @param mixed  $rawData Raw data from source (must be serializable)
83     *
84     * @return RecordEntityInterface
85     */
86    public function updateRecord(string $id, string $source, $rawData): RecordEntityInterface
87    {
88        $record = $this->getRecord($id, $source);
89        if (!$record) {
90            $record = $this->createEntity();
91        }
92        $record->setRecordId($id)
93            ->setSource($source)
94            ->setData(serialize($rawData))
95            ->setVersion(\VuFind\Config\Version::getBuildVersion())
96            ->setUpdated(new \DateTime());
97        $this->persistEntity($record);
98        return $record;
99    }
100
101    /**
102     * Clean up orphaned entries (i.e. entries that are not in favorites anymore)
103     *
104     * @return int Number of records deleted
105     */
106    public function cleanup(): int
107    {
108        return $this->getDbTable('record')->cleanup();
109    }
110
111    /**
112     * Delete a record by source and id. Return true if found and deleted, false if not found.
113     * Throws exception if something goes wrong.
114     *
115     * @param string $id     Record ID
116     * @param string $source Record source
117     *
118     * @return bool
119     * @throws Exception
120     */
121    public function deleteRecord(string $id, string $source): bool
122    {
123        $record = $this->getDbTable('record')->findRecord($id, $source);
124        if (!$record) {
125            return false;
126        }
127        $record->delete();
128        return true;
129    }
130
131    /**
132     * Create a record entity object.
133     *
134     * @return RecordEntityInterface
135     */
136    public function createEntity(): RecordEntityInterface
137    {
138        return $this->getDbTable('record')->createRow();
139    }
140}