Passed
Push — master ( da5c3a...138f6a )
by Torben
132:09 queued 128:49
created

isStartDateBeforeEndDate()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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