EventPlausabilityService::getLanguageService()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Extension "sf_event_mgt" for TYPO3 CMS.
7
 *
8
 * For the full copyright and license information, please read the
9
 * LICENSE.txt file that was distributed with this source code.
10
 */
11
12
namespace DERHANSEN\SfEventMgt\Service;
13
14
use TYPO3\CMS\Core\Localization\LanguageService;
15
use TYPO3\CMS\Core\Messaging\FlashMessage;
16
use TYPO3\CMS\Core\Messaging\FlashMessageService;
17
use TYPO3\CMS\Core\Type\ContextualFeedbackSeverity;
18
use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20
/**
21
 * Performs plausability checks on current event record in $result and enqueues flash messages if event contains
22
 * unplausible settings
23
 */
24
class EventPlausabilityService
25
{
26
    private const LANG_FILE = 'LLL:EXT:sf_event_mgt/Resources/Private/Language/locallang_be.xlf:';
27
28
    /**
29
     * Enqueues an error flash message, if the event startdate is not before the enddate
30
     */
31
    public function verifyEventStartAndEnddate(int $startDate, int $endDate): void
32
    {
33
        if (!$this->isStartDateBeforeEndDate($startDate, $endDate) && !($startDate === $endDate)) {
34
            $this->addMessageToFlashMessageQueue(
35
                $this->getLanguageService()->sL(self::LANG_FILE . 'event.startdateNotBeforeEnddate.message'),
36
                $this->getLanguageService()->sL(self::LANG_FILE . 'event.startdateNotBeforeEnddate.title'),
37
                ContextualFeedbackSeverity::ERROR
38
            );
39
        }
40
    }
41
42
    /**
43
     * Enqueues an warning flash message, if the event is set to notify the organisator, but no organisator
44
     * is set or organisator has no email address
45
     */
46
    public function verifyOrganisatorConfiguration(array $databaseRow): void
47
    {
48
        if ((int)$databaseRow['enable_registration'] === 0 || (int)$databaseRow['notify_organisator'] === 0) {
49
            return;
50
        }
51
52
        if (empty($databaseRow['organisator'])) {
53
            $this->addMessageToFlashMessageQueue(
54
                $this->getLanguageService()->sL(self::LANG_FILE . 'event.noOrganisator.message'),
55
                $this->getLanguageService()->sL(self::LANG_FILE . 'event.noOrganisator.title'),
56
                ContextualFeedbackSeverity::WARNING
57
            );
58
            return;
59
        }
60
61
        foreach ($databaseRow['organisator'] as $organisator) {
62
            if (!GeneralUtility::validEmail($organisator['row']['email'])) {
63
                $this->addMessageToFlashMessageQueue(
64
                    $this->getLanguageService()->sL(self::LANG_FILE . 'event.noOrganisatorEmail.message'),
65
                    $this->getLanguageService()->sL(self::LANG_FILE . 'event.noOrganisatorEmail.title'),
66
                    ContextualFeedbackSeverity::WARNING
67
                );
68
            }
69
        }
70
    }
71
72
    protected function isStartDateBeforeEndDate(int $startDate, int $endDate): bool
73
    {
74
        if ($startDate === 0 || $endDate === 0) {
75
            return true;
76
        }
77
78
        return $startDate < $endDate;
79
    }
80
81
    protected function addMessageToFlashMessageQueue(
82
        string $message,
83
        string $title = '',
84
        ContextualFeedbackSeverity $severity = ContextualFeedbackSeverity::INFO
85
    ): void {
86
        $flashMessage = GeneralUtility::makeInstance(
87
            FlashMessage::class,
88
            $message,
89
            $title,
90
            $severity,
91
            true
92
        );
93
94
        $this->addFlashMessage($flashMessage);
95
    }
96
97
    protected function addFlashMessage(FlashMessage $flashMessage): void
98
    {
99
        $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
100
        $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
101
        $defaultFlashMessageQueue->enqueue($flashMessage);
102
    }
103
104
    protected function getLanguageService(): ?LanguageService
105
    {
106
        return $GLOBALS['LANG'] ?? null;
107
    }
108
}
109