HideInlineRegistrations::addData()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 38
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 22
nc 4
nop 1
dl 0
loc 38
rs 9.2568
c 0
b 0
f 0
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\Form\FormDataProvider;
13
14
use TYPO3\CMS\Backend\Form\FormDataProviderInterface;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Backend\Form\FormDataProviderInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
16
use TYPO3\CMS\Core\Database\Connection;
17
use TYPO3\CMS\Core\Database\ConnectionPool;
18
use TYPO3\CMS\Core\Database\Query\Restriction\HiddenRestriction;
19
use TYPO3\CMS\Core\Localization\LanguageService;
20
use TYPO3\CMS\Core\Messaging\FlashMessage;
21
use TYPO3\CMS\Core\Messaging\FlashMessageService;
22
use TYPO3\CMS\Core\Type\ContextualFeedbackSeverity;
23
use TYPO3\CMS\Core\Utility\GeneralUtility;
24
25
class HideInlineRegistrations implements FormDataProviderInterface
26
{
27
    public function addData(array $result): array
28
    {
29
        if ($result['tableName'] !== 'tx_sfeventmgt_domain_model_event' || !is_int($result['databaseRow']['uid'])) {
30
            return $result;
31
        }
32
33
        $extConfig = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('sf_event_mgt');
34
        if ((bool)$extConfig['hideInlineRegistrations'] === false) {
35
            return $result;
36
        }
37
38
        $amountOfRegistrations = $this->getRegistrationCount($result['databaseRow']['uid']);
39
40
        if ($amountOfRegistrations > (int)$extConfig['hideInlineRegistrationsLimit']) {
41
            $message = sprintf(
42
                $this->getLanguageService()->sL('LLL:EXT:sf_event_mgt/Resources/Private/Language/locallang_be.xlf:backend.hideInlineRegistrations.description'),
43
                (string)$amountOfRegistrations
44
            );
45
46
            $flashMessage = GeneralUtility::makeInstance(
47
                FlashMessage::class,
48
                $message,
49
                $this->getLanguageService()->sL('LLL:EXT:sf_event_mgt/Resources/Private/Language/locallang_be.xlf:backend.hideInlineRegistrations.title'),
50
                ContextualFeedbackSeverity::INFO,
51
                true
52
            );
53
54
            $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
55
            $messageQueue = $flashMessageService->getMessageQueueByIdentifier();
56
            // @extensionScannerIgnoreLine
57
            $messageQueue->addMessage($flashMessage);
58
59
            // Unset the field "registration" and "registration_waitlist", so no data will be shows/loaded
60
            unset($result['processedTca']['columns']['registration']);
61
            unset($result['processedTca']['columns']['registration_waitlist']);
62
        }
63
64
        return $result;
65
    }
66
67
    protected function getRegistrationCount(int $eventId): int
68
    {
69
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
70
            ->getQueryBuilderForTable('tx_sfeventmgt_domain_model_registration');
71
        $queryBuilder->getRestrictions()->removeByType(HiddenRestriction::class);
72
73
        return (int)$queryBuilder->count('uid')
74
            ->from('tx_sfeventmgt_domain_model_registration')
75
            ->where(
76
                $queryBuilder->expr()->eq(
77
                    'event',
78
                    $queryBuilder->createNamedParameter($eventId, Connection::PARAM_INT)
79
                )
80
            )
81
            ->executeQuery()
82
            ->fetchOne();
83
    }
84
85
    protected function getLanguageService(): LanguageService
86
    {
87
        return $GLOBALS['LANG'];
88
    }
89
}
90