Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
15.79% covered (danger)
15.79%
3 / 19
12.50% covered (danger)
12.50%
1 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
CommentsService
15.79% covered (danger)
15.79%
3 / 19
12.50% covered (danger)
12.50%
1 / 8
113.92
0.00% covered (danger)
0.00%
0 / 1
 createEntity
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 addComment
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 getRecordComments
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 deleteIfOwnedByUser
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 deleteByUser
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getStatistics
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getCommentById
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 changeResourceId
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 Comments.
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 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org/wiki/development:plugins:database_gateways Wiki
28 */
29
30namespace VuFind\Db\Service;
31
32use VuFind\Db\Entity\CommentsEntityInterface;
33use VuFind\Db\Entity\ResourceEntityInterface;
34use VuFind\Db\Entity\UserEntityInterface;
35use VuFind\Db\Table\DbTableAwareInterface;
36use VuFind\Db\Table\DbTableAwareTrait;
37
38use function is_array;
39use function is_int;
40
41/**
42 * Database service for Comments.
43 *
44 * @category VuFind
45 * @package  Database
46 * @author   Demian Katz <demian.katz@villanova.edu>
47 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
48 * @link     https://vufind.org/wiki/development:plugins:database_gateways Wiki
49 */
50class CommentsService extends AbstractDbService implements
51    CommentsServiceInterface,
52    DbServiceAwareInterface,
53    DbTableAwareInterface
54{
55    use DbServiceAwareTrait;
56    use DbTableAwareTrait;
57
58    /**
59     * Create a comments entity object.
60     *
61     * @return CommentsEntityInterface
62     */
63    public function createEntity(): CommentsEntityInterface
64    {
65        return $this->getDbTable('comments')->createRow();
66    }
67
68    /**
69     * Add a comment to the current resource. Returns comment ID on success, null on failure.
70     *
71     * @param string                      $comment      The comment to save.
72     * @param UserEntityInterface|int     $userOrId     User object or identifier
73     * @param ResourceEntityInterface|int $resourceOrId Resource object or identifier
74     *
75     * @return ?int
76     */
77    public function addComment(
78        string $comment,
79        UserEntityInterface|int $userOrId,
80        ResourceEntityInterface|int $resourceOrId
81    ): ?int {
82        $user = is_int($userOrId)
83            ? $this->getDbService(UserServiceInterface::class)->getUserById($userOrId)
84            : $userOrId;
85        $resource = is_int($resourceOrId)
86            ? $this->getDbService(ResourceServiceInterface::class)->getResourceById($resourceOrId)
87            : $resourceOrId;
88        return $resource->addComment($comment, $user);
89    }
90
91    /**
92     * Get comments associated with the specified record.
93     *
94     * @param string $id     Record ID to look up
95     * @param string $source Source of record to look up
96     *
97     * @return CommentsEntityInterface[]
98     */
99    public function getRecordComments(string $id, string $source = DEFAULT_SEARCH_BACKEND): array
100    {
101        $comments = $this->getDbTable('comments')->getForResource($id, $source);
102        return is_array($comments) ? $comments : iterator_to_array($comments);
103    }
104
105    /**
106     * Delete a comment if the owner is logged in.  Returns true on success.
107     *
108     * @param int                     $id       ID of row to delete
109     * @param UserEntityInterface|int $userOrId User object or identifier
110     *
111     * @return bool
112     */
113    public function deleteIfOwnedByUser(int $id, UserEntityInterface|int $userOrId): bool
114    {
115        $user = is_int($userOrId)
116            ? $this->getDbService(UserServiceInterface::class)->getUserById($userOrId) : $userOrId;
117        return $this->getDbTable('comments')->deleteIfOwnedByUser($id, $user);
118    }
119
120    /**
121     * Deletes all comments by a user.
122     *
123     * @param UserEntityInterface|int $userOrId User object or identifier
124     *
125     * @return void
126     */
127    public function deleteByUser(UserEntityInterface|int $userOrId): void
128    {
129        $user = is_int($userOrId)
130            ? $this->getDbService(UserServiceInterface::class)->getUserById($userOrId) : $userOrId;
131        $this->getDbTable('comments')->deleteByUser($user);
132    }
133
134    /**
135     * Get statistics on use of comments.
136     *
137     * @return array
138     */
139    public function getStatistics(): array
140    {
141        return $this->getDbTable('comments')->getStatistics();
142    }
143
144    /**
145     * Get a comment row by ID (or return null for no match).
146     *
147     * @param int $id ID of comment to retrieve.
148     *
149     * @return ?CommentsEntityInterface
150     */
151    public function getCommentById(int $id): ?CommentsEntityInterface
152    {
153        return $this->getDbTable('comments')->select(['id' => $id])->current();
154    }
155
156    /**
157     * Change all matching comments to use the new resource ID instead of the old one (called when an ID changes).
158     *
159     * @param int $old Original resource ID
160     * @param int $new New resource ID
161     *
162     * @return void
163     */
164    public function changeResourceId(int $old, int $new): void
165    {
166        $this->getDbTable('comments')->update(['resource_id' => $new], ['resource_id' => $old]);
167    }
168}