Lines 96.77% 30 / 31
Methods 87.50% 7 / 8
Classes 0.00% 0 / 1
Covered by tests of size
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 add 100.00% 1 / 1 100.00% 1 / 1 1
 delete 100.00% 1 / 1 100.00% 1 / 1 1
 get 100.00% 1 / 1 100.00% 1 / 1 1
 getAll 100.00% 17 / 17 100.00% 1 / 1 3
 getVisibility 100.00% 4 / 4 100.00% 1 / 1 1
 setVisibility 100.00% 5 / 5 100.00% 1 / 1 1
 updateQuestionAnswer 0.00% 0 / 1 0.00% 0 / 1 2
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}