Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
FeedbackService
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 5
42
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
 getFeedbackById
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getFeedbackPaginator
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
6
 deleteByIdArray
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getUniqueColumn
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * Database service for feedback.
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 Laminas\Paginator\Paginator;
34use VuFind\Db\Entity\FeedbackEntityInterface;
35use VuFind\Db\Table\DbTableAwareInterface;
36use VuFind\Db\Table\DbTableAwareTrait;
37
38use function count;
39
40/**
41 * Database service for feedback.
42 *
43 * @category VuFind
44 * @package  Database
45 * @author   Sudharma Kellampalli <skellamp@villanova.edu>
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 FeedbackService extends AbstractDbService implements DbTableAwareInterface, FeedbackServiceInterface
51{
52    use DbTableAwareTrait;
53
54    /**
55     * Create a feedback entity object.
56     *
57     * @return FeedbackEntityInterface
58     */
59    public function createEntity(): FeedbackEntityInterface
60    {
61        return $this->getDbTable('feedback')->createRow();
62    }
63
64    /**
65     * Fetch a feedback entity by ID.
66     *
67     * @param int $id ID of feedback entity
68     *
69     * @return ?FeedbackEntityInterface
70     */
71    public function getFeedbackById(int $id): ?FeedbackEntityInterface
72    {
73        return $this->getDbTable('feedback')->select(['id' => $id])->current();
74    }
75
76    /**
77     * Get feedback by filter
78     *
79     * @param ?string $formName Form name (optional filter)
80     * @param ?string $siteUrl  Site URL (optional filter)
81     * @param ?string $status   Current status (optional filter)
82     * @param ?int    $page     Current page (optional)
83     * @param int     $limit    Limit per page
84     *
85     * @return Paginator
86     */
87    public function getFeedbackPaginator(
88        ?string $formName = null,
89        ?string $siteUrl = null,
90        ?string $status = null,
91        ?int $page = null,
92        int $limit = 20
93    ): Paginator {
94        // The template expects a different format than what is returned by Laminas\Db; we need to do
95        // some data conversion and then populate a new paginator with the remapped results. We'll use
96        // a padded array and the array adapter to make this work. Probably not the most robust solution,
97        // but good enough for the current needs of the software; this will go away in a future database
98        // layer migration.
99        $feedbackTable = $this->getDbTable('feedback');
100        $paginator = $feedbackTable->getFeedbackByFilter($formName, $siteUrl, $status, $page, $limit);
101        $results = array_fill(0, count($paginator->getAdapter()), []);
102        $index = (($page ?? 1) - 1) * $limit;
103        foreach ($paginator as $current) {
104            $row = (array)$current;
105            $row['feedback_entity'] = $feedbackTable->createRow()->populate($row);
106            $results[$index] = $row;
107            $index++;
108        }
109        $newPaginator = new Paginator(new \Laminas\Paginator\Adapter\ArrayAdapter($results));
110        $newPaginator->setCurrentPageNumber($page ?? 1);
111        $newPaginator->setItemCountPerPage($limit);
112        return $newPaginator;
113    }
114
115    /**
116     * Delete feedback by ids
117     *
118     * @param array $ids IDs
119     *
120     * @return int Count of deleted rows
121     */
122    public function deleteByIdArray(array $ids): int
123    {
124        return $this->getDbTable('feedback')->deleteByIdArray($ids);
125    }
126
127    /**
128     * Get unique values for a column of the feedback table
129     *
130     * @param string $column Column name
131     *
132     * @return array
133     */
134    public function getUniqueColumn(string $column): array
135    {
136        $feedbackTable = $this->getDbTable('feedback');
137        $feedback = $feedbackTable->select(
138            function ($select) use ($column) {
139                $select->columns(['id', $column]);
140                $select->order($column);
141            }
142        );
143        $feedbackArray = $feedback->toArray();
144        return array_unique(array_column($feedbackArray, $column));
145    }
146}