Lines 100.00% 82 / 82
Methods 100.00% 12 / 12
Classes 100.00% 1 / 1
Covered by tests of size
Name Lines Methods CRAP
 generateSmartAnswer 100.00% 26 / 26 100.00% 1 / 1 2
 getOpenQuestions 100.00% 40 / 40 100.00% 1 / 1 6
 [phpMyFAQ\Helper\AbstractHelper] setCategory 100.00% 2 / 2 100.00% 1 / 1 1
 [phpMyFAQ\Helper\AbstractHelper] getCategory 100.00% 1 / 1 100.00% 1 / 1 1
 [phpMyFAQ\Helper\AbstractHelper] category 100.00% 1 / 1 100.00% 1 / 1 1
 [phpMyFAQ\Helper\AbstractHelper] plurals 100.00% 1 / 1 100.00% 1 / 1 1
 [phpMyFAQ\Helper\AbstractHelper] setCategoryRelation 100.00% 2 / 2 100.00% 1 / 1 1
 [phpMyFAQ\Helper\AbstractHelper] setTags 100.00% 2 / 2 100.00% 1 / 1 1
 [phpMyFAQ\Helper\AbstractHelper] setPlurals 100.00% 2 / 2 100.00% 1 / 1 1
 [phpMyFAQ\Helper\AbstractHelper] setSessionId 100.00% 2 / 2 100.00% 1 / 1 1
 [phpMyFAQ\Helper\AbstractHelper] setConfiguration 100.00% 2 / 2 100.00% 1 / 1 1
 [phpMyFAQ\Helper\AbstractHelper] getConfiguration 100.00% 1 / 1 100.00% 1 / 1 1
37class QuestionHelper extends AbstractHelper
38{
39    /**
40     * @throws CommonMarkException
41     */
42    public function generateSmartAnswer(SearchResultSet $searchResultSet): string
43    {
44        $plurals = new Plurals();
45        $smartAnswer = sprintf('<h5>%s</h5>', $plurals->get(
46            key: 'plmsgSearchAmount',
47            number: $searchResultSet->getNumberOfResults(),
48        ));
49
50        $smartAnswer .= '<ul>';
51        foreach ($searchResultSet->getResultSet() as $result) {
52            $question = (string) ($result->question ?? '');
53            $url = sprintf(
54                '%scontent/%d/%d/%s/%s.html',
55                $this->configuration->getDefaultUrl(),
56                (int) ($result->category_id ?? 0),
57                (int) ($result->id ?? 0),
58                (string) ($result->lang ?? ''),
59                TitleSlugifier::slug($question),
60            );
61            $link = new Link($url, $this->configuration);
62            $link->text = Utils::chopString($question, 15);
63            $link->setTitle($question);
64
65            $faqHelper = new FaqHelper($this->configuration);
66            $smartAnswer .= sprintf(
67                '<li>%s<br><small class="pmf-search-preview">%s...</small></li>',
68                $link->toHtmlAnchor(),
69                $faqHelper->renderAnswerPreview((string) ($result->answer ?? ''), 10),
70            );
71        }
72
73        return $smartAnswer . '</ul>';
74    }
75
76    public function getOpenQuestions(): stdClass
77    {
78        $date = new Date($this->configuration);
79        $mail = new Mail($this->configuration);
80        $db = $this->configuration->getDb();
81        $language = $db->escape($this->configuration->getLanguage()->getLanguage());
82
83        $query = sprintf(
84            "SELECT COUNT(id) AS num FROM %sfaqquestions WHERE lang = '%s' AND is_visible != 'Y'",
85            Database::getTablePrefix(),
86            $language,
87        );
88
89        $result = $db->query($query);
90        $row = $db->fetchObject($result);
91
92        $openQuestions = new stdClass();
93        $openQuestions->numberInvisibleQuestions = $row instanceof \stdClass ? (int) $row->num : 0;
94
95        $query = sprintf(
96            "SELECT * FROM %sfaqquestions WHERE lang = '%s' AND is_visible = 'Y' ORDER BY created ASC",
97            Database::getTablePrefix(),
98            $language,
99        );
100
101        $result = $db->query($query);
102
103        if ($result !== false && $this->configuration->getDb()->numRows($result) > 0) {
104            $openQuestions->numberQuestions = $this->configuration->getDb()->numRows($result);
105            $questions = [];
106            while (true) {
107                $row = $this->configuration->getDb()->fetchObject($result);
108                if (!$row instanceof \stdClass) {
109                    break;
110                }
111
112                $question = new stdClass();
113                $question->id = (int) $row->id;
114                $question->lang = (string) $row->lang;
115                $question->date = $date->format(Date::createIsoDate((string) $row->created));
116                $question->email = $mail->safeEmail((string) $row->email);
117                $question->userName = (string) $row->username;
118                $question->categoryId = (int) $row->category_id;
119                $question->categoryName = $this->getCategory()->getCategoryName((int) $row->category_id);
120                $question->question = (string) $row->question;
121                $question->answerId = (int) $row->answer_id;
122                $question->slug = TitleSlugifier::slug($question->question);
123
124                $questions[] = $question;
125            }
126
127            $openQuestions->questions = $questions;
128        }
129
130        return $openQuestions;
131    }
132}

Inherited from phpMyFAQ\Helper\AbstractHelper

47    public function setCategory(Category $Category): AbstractHelper
48    {
49        $this->Category = $Category;
50        return $this;
51    }
53    public function getCategory(): Category
54    {
55        return $this->category();
56    }
61    protected function category(): Category
62    {
63        return $this->Category ?? throw new \LogicException('setCategory() must be called before use.');
64    }
69    protected function plurals(): Plurals
70    {
71        return $this->plurals ?? throw new \LogicException('setPlurals() must be called before use.');
72    }
74    public function setCategoryRelation(Relation $categoryRelation): AbstractHelper
75    {
76        $this->categoryRelation = $categoryRelation;
77        return $this;
78    }
80    public function setTags(Tags $Tags): AbstractHelper
81    {
82        $this->Tags = $Tags;
83        return $this;
84    }
86    public function setPlurals(Plurals $plurals): AbstractHelper
87    {
88        $this->plurals = $plurals;
89        return $this;
90    }
92    public function setSessionId(int|string $sid): AbstractHelper
93    {
94        $this->sessionId = $sid;
95        return $this;
96    }
98    public function setConfiguration(Configuration $configuration): AbstractHelper
99    {
100        $this->configuration = $configuration;
101        return $this;
102    }
104    public function getConfiguration(): Configuration
105    {
106        return $this->configuration;
107    }