Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
84.21% covered (warning)
84.21%
16 / 19
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Database
84.21% covered (warning)
84.21%
16 / 19
50.00% covered (danger)
50.00%
1 / 2
3.04
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 handle
83.33% covered (warning)
83.33%
15 / 18
0.00% covered (danger)
0.00%
0 / 1
2.02
1<?php
2
3/**
4 * Class Database
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Moravian Library 2022.
9 * Copyright (C) Villanova University 2023.
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  Form
26 * @author   Josef Moravec <moravec@mzk.cz>
27 * @license  https://opensource.org/licenses/gpl-2.0.php GNU General Public License
28 * @link     https://vufind.org/wiki/development Wiki
29 */
30
31declare(strict_types=1);
32
33namespace VuFind\Form\Handler;
34
35use Laminas\Log\LoggerAwareInterface;
36use VuFind\Db\Entity\UserEntityInterface;
37use VuFind\Db\Service\FeedbackServiceInterface;
38use VuFind\Log\LoggerAwareTrait;
39
40/**
41 * Class Database
42 *
43 * @category VuFind
44 * @package  Form
45 * @author   Josef Moravec <moravec@mzk.cz>
46 * @license  https://opensource.org/licenses/gpl-2.0.php GNU General Public License
47 * @link     https://vufind.org/wiki/development Wiki
48 */
49class Database implements HandlerInterface, LoggerAwareInterface
50{
51    use LoggerAwareTrait;
52
53    /**
54     * Constructor
55     *
56     * @param FeedbackServiceInterface $feedbackService Feedback database service
57     * @param string                   $baseUrl         Site base url
58     */
59    public function __construct(
60        protected FeedbackServiceInterface $feedbackService,
61        protected string $baseUrl
62    ) {
63    }
64
65    /**
66     * Get data from submitted form and process them.
67     *
68     * @param \VuFind\Form\Form                     $form   Submitted form
69     * @param \Laminas\Mvc\Controller\Plugin\Params $params Request params
70     * @param ?UserEntityInterface                  $user   Authenticated user
71     *
72     * @return bool
73     */
74    public function handle(
75        \VuFind\Form\Form $form,
76        \Laminas\Mvc\Controller\Plugin\Params $params,
77        ?UserEntityInterface $user = null
78    ): bool {
79        $fields = $form->mapRequestParamsToFieldValues($params->fromPost());
80        $fields = array_column($fields, 'value', 'name');
81        $formData = $fields;
82        unset($formData['message']);
83        $now = new \DateTime();
84        $data = $this->feedbackService->createEntity()
85            ->setUser($user)
86            ->setMessage($fields['message'] ?? '')
87            ->setFormData($formData)
88            ->setFormName($form->getFormId())
89            ->setSiteUrl($this->baseUrl)
90            ->setCreated($now)
91            ->setUpdated($now);
92        try {
93            $this->feedbackService->persistEntity($data);
94        } catch (\Exception $e) {
95            $this->logError('Could not save feedback data: ' . $e->getMessage());
96            return false;
97        }
98        // If we got this far, we succeeded; otherwise, persistEntity would have
99        // thrown an exception above.
100        return true;
101    }
102}