Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.77% covered (success)
96.77%
30 / 31
87.50% covered (success)
87.50%
7 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
Question
96.77% covered (success)
96.77%
30 / 31
87.50% covered (success)
87.50%
7 / 8
10
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
 add
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 delete
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getAll
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
3
 getVisibility
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 setVisibility
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 updateQuestionAnswer
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * The main Question class.
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public License,
7 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
8 * obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * @package   phpMyFAQ
11 * @author    Thorsten Rinne <thorsten@phpmyfaq.de>
12 * @copyright 2019-2026 phpMyFAQ Team
13 * @license   https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14 * @link      https://www.phpmyfaq.de
15 * @since     2019-11-22
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ;
21
22use phpMyFAQ\Entity\QuestionEntity;
23use phpMyFAQ\Question\QuestionRepository;
24
25/**
26 * Class Question
27 *
28 * @package phpMyFAQ
29 */
30readonly class Question
31{
32    private QuestionRepository $questionRepository;
33
34    /**
35     * Question constructor.
36     */
37    public function __construct(
38        private Configuration $configuration,
39    ) {
40        $this->questionRepository = new QuestionRepository($configuration);
41    }
42
43    /**
44     * Adds a new question.
45     */
46    public function add(QuestionEntity $questionEntity): bool
47    {
48        return $this->questionRepository->add($questionEntity);
49    }
50
51    /**
52     * Deletes a question for the table "faqquestions".
53     */
54    public function delete(int $questionId): bool
55    {
56        return $this->questionRepository->delete($questionId, $this->configuration->getLanguage()->getLanguage());
57    }
58
59    /**
60     * Returns a new question.
61     *
62     * @return array<string, int|string>
63     */
64    public function get(int $questionId): array
65    {
66        return $this->questionRepository->getById($questionId, $this->configuration->getLanguage()->getLanguage());
67    }
68
69    /**
70     * Returns all open questions.
71     *
72     * @return QuestionEntity[]
73     */
74    public function getAll(bool $showAll = true, ?string $language = null): array
75    {
76        $questions = [];
77        $targetLang = $language === null ? $this->configuration->getLanguage()->getLanguage() : $language;
78        $rows = $this->questionRepository->getAll($targetLang, $showAll);
79
80        foreach ($rows as $row) {
81            $question = new QuestionEntity();
82            $question
83                ->setId((int) $row['id'])
84                ->setLanguage((string) $row['lang'])
85                ->setUsername((string) $row['username'])
86                ->setEmail((string) $row['email'])
87                ->setCategoryId((int) $row['category_id'])
88                ->setQuestion((string) $row['question'])
89                ->setCreated(Date::createIsoDate((string) $row['created']))
90                ->setAnswerId((int) $row['answer_id'])
91                ->setIsVisible($row['is_visible'] === 'Y');
92
93            $questions[] = $question;
94        }
95
96        return $questions;
97    }
98
99    /**
100     * Returns the visibility of a question.
101     */
102    public function getVisibility(int $questionId): string
103    {
104        return $this->questionRepository->getVisibility(
105            $questionId,
106            $this->configuration->getLanguage()->getLanguage(),
107        );
108    }
109
110    /**
111     * Sets the visibility of a question.
112     */
113    public function setVisibility(int $questionId, string $isVisible): bool
114    {
115        return $this->questionRepository->setVisibility(
116            $questionId,
117            $isVisible,
118            $this->configuration->getLanguage()->getLanguage(),
119        );
120    }
121
122    /**
123     * Updates field answer_id in the table "faqquestion".
124     */
125    public function updateQuestionAnswer(int $openQuestionId, int $faqId, int $categoryId): bool
126    {
127        return $this->questionRepository->updateQuestionAnswer($openQuestionId, $faqId, $categoryId);
128    }
129}