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

VoteController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types = 1);
4
/**
5
 * VoteController.php.
6
 */
7
8
namespace HDNET\Faq\ViewHelpers\Widget\Controller;
9
10
use HDNET\Autoloader\Utility\TranslateUtility;
11
use HDNET\Faq\Domain\Model\Question;
12
use HDNET\Faq\Domain\Model\Request\Vote;
13
use HDNET\Faq\Domain\Repository\QuestionRepository;
14
use HDNET\Faq\Exception\AlreadyVotedException;
15
use HDNET\Faq\Exception\VoteException;
16
use HDNET\Faq\Service\SessionService;
17
use TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException;
18
use TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException;
19
20
/**
21
 * VoteController.
22
 */
23
class VoteController extends AbstractWidgetController
24
{
25
    /**
26
     * Session service.
27
     *
28
     * @var SessionService
29
     */
30
    protected $sessionService;
31
32
    /**
33
     * Question repository.
34
     *
35
     * @var QuestionRepository
36
     *
37
     */
38
    protected $questionRepository;
39
40
    public function __construct(SessionService $sessionService, QuestionRepository $questionRepository)
41
    {
42
        $this->sessionService = $sessionService;
43
        $this->questionRepository = $questionRepository;
44
45
    }
46
47
    /**
48
     * Index action.
49
     */
50
    public function indexAction(): void
51
    {
52
        $this->view->assignMultiple([
53
            'top' => $this->widgetConfiguration['counters']['top'],
54
            'flop' => $this->widgetConfiguration['counters']['flop'],
55
            'question' => $this->widgetConfiguration['question'],
56
        ]);
57
    }
58
59
    /**
60
     * Vote action.
61
     *
62
     * @param Question $question
63
     * @param int $mode
64
     *
65
     * @return string
66
     * @throws IllegalObjectTypeException
67
     * @throws UnknownObjectException
68
     */
69
    public function voteAction(Question $question, int $mode)
70
    {
71
        $vote = $this->objectManager->get(Vote::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...
72
        $vote->setMode($mode);
73
        $vote->setQuestion($question);
74
75
        $result = [
76
            'state' => 'ERROR',
77
            'description' => 'Unknown',
78
            'currentCounter' => 0,
79
        ];
80
81
        $sessionIdentifier = 'topflop';
82
83
        try {
84
            $ids = $this->sessionService->setAndGet($sessionIdentifier, []);
85
            $vote->checkAgainst($ids);
86
            \array_push($ids, $vote->getQuestion()
87
                ->getUid());
88
            $this->sessionService->set($sessionIdentifier, $ids);
89
            $vote->updateQuestion();
90
            $this->questionRepository->update($vote->getQuestion());
91
            $result['state'] = 'OK';
92
            $result['description'] = TranslateUtility::assureLabel(
93
                'eid.ok',
94
                'faq',
95
                'Vielen Dank f&uuml;r Ihre Wertung.'
96
            );
97
            $result['currentCounter'] = $vote->getQuestionVotes();
98
        } catch (AlreadyVotedException $e) {
99
            $result['description'] = TranslateUtility::assureLabel(
100
                'eid.error.multivote',
101
                'faq',
102
                'Sie haben f&uuml;r diese Frage bereits abgestimmt!'
103
            );
104
        } catch (VoteException $e) {
105
            $result['description'] = $e->getMessage();
106
        }
107
108
        return \json_encode($result);
109
    }
110
}
111