Completed
Push — master ( 4cc4cf...1e6e7e )
by Tim
13:02 queued 10s
created

FaqController::getTargetEmailAddress()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 0
1
<?php
2
3
declare(strict_types = 1);
4
/**
5
 * FAQ.
6
 */
7
8
namespace HDNET\Faq\Controller;
9
10
use HDNET\Faq\Domain\Model\Question;
11
use HDNET\Faq\Domain\Model\Request\Faq;
12
use HDNET\Faq\Domain\Model\Request\QuestionRequest;
13
use HDNET\Faq\Domain\Repository\QuestionCategoryRepository;
14
use HDNET\Faq\Domain\Repository\QuestionRepository;
15
use TYPO3\CMS\Core\Utility\GeneralUtility;
16
use TYPO3\CMS\Extbase\Annotation\IgnoreValidation;
17
use TYPO3\CMS\Extbase\Mvc\Exception\StopActionException;
18
use TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException;
19
20
/**
21
 * FAQ.
22
 */
23
class FaqController extends AbstractController
24
{
25
    const TEASER_MODE_VOTING = 0;
26
27
    const TEASER_MODE_CUSTOM = 1;
28
29
    /**
30
     * Question repository.
31
     *
32
     * @var QuestionRepository
33
     */
34
    protected $questionRepository;
35
36
    /**
37
     * Question category repository.
38
     *
39
     * @var QuestionCategoryRepository
40
     */
41
    protected $questionCategoryRepository;
42
43
    public function __construct(QuestionRepository $questionRepository, QuestionCategoryRepository $questionCategoryRepository)
44
    {
45
        $this->questionRepository = $questionRepository;
46
        $this->questionCategoryRepository = $questionCategoryRepository;
47
    }
48
49
    /**
50
     * Index action.
51
     *
52
     * @param Faq|null $faq
53
     * @param bool $showAll
54
     * @throws InvalidQueryException
55
     */
56
    public function indexAction(Faq $faq = null, $showAll = false): void
57
    {
58
        $topCategory = (int)$this->settings['faq']['topCategory'];
59
60
        if (true === (bool)$this->settings['overrideShowAll']) {
61
            $showAll = true;
62
        }
63
        if (0 !== (int)$this->settings['overrideTopCategory']) {
64
            $topCategory = (int)$this->settings['overrideTopCategory'];
65
        }
66
67
        if (\is_object($faq)) {
68
            $questions = $this->questionRepository->findByFaq($faq, $topCategory);
69
            $showResults = true;
70
        } elseif ($showAll) {
71
            $showResults = true;
72
            $questions = $this->questionRepository->findAll($topCategory);
73
        } else {
74
            $questions = [];
75
            $showResults = false;
76
        }
77
78
        if (self::TEASER_MODE_VOTING === (int)$this->settings['topMode']) {
79
            $topQuestions = $this->questionRepository->findTop(
80
                (int)$this->settings['faq']['limitTop'],
81
                $topCategory,
82
                GeneralUtility::intExplode(',', $this->settings['faq']['topQuestions'], true)
83
            );
84
        } else {
85
            $topQuestions = $this->questionRepository->findByUidsSorted(GeneralUtility::intExplode(
86
                ',',
87
                $this->settings['custom'],
88
                true
89
            ));
90
        }
91
92
        if (null === $faq) {
93
            $faq = $this->objectManager->get(Faq::class);
0 ignored issues
show
Deprecated Code introduced by
The method TYPO3\CMS\Extbase\Object...ManagerInterface::get() has been deprecated with message: since TYPO3 10.4, will be removed in version 12.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
94
        }
95
96
        $this->view->assignMultiple([
97
            'showResults' => $showResults,
98
            'faq' => $faq,
99
            'questions' => $questions,
100
            'newQuestions' => $this->questionRepository->findNewest(
101
                (int)$this->settings['faq']['limitNewest'],
102
                $topCategory
103
            ),
104
            'topQuestions' => $topQuestions,
105
            'categories' => $this->questionCategoryRepository->findByParent(
106
                $topCategory,
107
                (bool)$this->settings['faq']['categorySort'] ?: false
108
            ),
109
        ]);
110
    }
111
112
    /**
113
     * Render the teaser action.
114
     */
115
    public function teaserAction(): void
116
    {
117
        $topQuestions = GeneralUtility::intExplode(',', $this->settings['faq']['topQuestions'], true);
118
        $teaserCategories = GeneralUtility::intExplode(',', $this->settings['faq']['teaserCategories'], true);
119
        $teaserLimit = (int)$this->settings['faq']['teaserLimit'];
120
        $questions = $this->questionRepository->findByTeaserConfiguration(
121
            $topQuestions,
122
            $teaserCategories,
123
            $teaserLimit
124
        );
125
        $this->view->assign('questions', $questions);
126
    }
127
128
    /**
129
     * Render the detail action.
130
     *
131
     * @param Question $question
132
     */
133
    public function detailAction(Question $question): void
134
    {
135
        $this->view->assign('question', $question);
136
    }
137
138
    /**
139
     * Enter form.
140
     *
141
     * @param QuestionRequest|null $question
142
     * @IgnoreValidation(argumentName="question")
143
     */
144
    public function formAction(QuestionRequest $question = null): void
145
    {
146
        if (null === $question) {
147
            $question = new QuestionRequest();
148
        }
149
150
        $this->view->assign('question', $question);
151
    }
152
153
    /**
154
     * Send action.
155
     *
156
     * @param QuestionRequest $question
157
     * @param null $captcha
158
     *
159
     * @throws StopActionException
160
     */
161
    public function sendAction(QuestionRequest $question, $captcha = null): void
162
    {
163
        // @todo integrate captcha based on $this->settings['enableCaptcha']
164
        // * @validate $captcha \SJBR\SrFreecap\Validation\Validator\CaptchaValidator && Not Empty
165
        $this->disableIndexing();
166
167
        $targetEmailAddress = $this->getTargetEmailAddress();
168
        if (GeneralUtility::validEmail($targetEmailAddress)) {
169
            $this->view->assign('to', [$targetEmailAddress => $targetEmailAddress]);
170
            $this->view->assign('subject', 'Neue Frage eingestellt');
171
            $this->view->assign('question', $question);
172
            $this->view->assign('captcha', $captcha);
173
            $this->view->render();
174
        }
175
        $this->forward('user');
176
    }
177
178
    /**
179
     * user action.
180
     *
181
     * @param QuestionRequest $question
182
     *
183
     * @throws StopActionException
184
     */
185
    public function userAction(QuestionRequest $question): void
186
    {
187
        if (GeneralUtility::validEmail($question->getEmail())) {
188
            $this->view->assignMultiple([
189
                'subject' => 'FAQ eingereicht',
190
                'to' => [$question->getEmail() => $question->getEmail()],
191
                'question' => $question,
192
            ]);
193
            $this->view->render();
194
        }
195
        $this->forward('thanks');
196
    }
197
198
    /**
199
     * Send action.
200
     *
201
     * @param QuestionRequest $question
202
     */
203
    public function thanksAction(QuestionRequest $question): void
204
    {
205
        $this->disableIndexing();
206
        $this->view->assign('question', $question);
207
    }
208
209
    /**
210
     * Get the target Email address.
211
     *
212
     * @throws \Exception
213
     *
214
     * @return string
215
     */
216
    protected function getTargetEmailAddress(): string
217
    {
218
        if (isset($this->settings['faq']['targetEmail']) && GeneralUtility::validEmail(\trim((string)$this->settings['faq']['targetEmail']))) {
219
            return \trim((string)$this->settings['faq']['targetEmail']);
220
        }
221
        throw new \Exception('No target e-mail address found', 123718231823);
222
    }
223
}
224