DisableLanguageMenuProcessor::handleMenu()   A
last analyzed

Complexity

Conditions 5
Paths 7

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 7
nop 3
dl 0
loc 15
rs 9.6111
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\DataProcessing;
13
14
use DERHANSEN\SfEventMgt\Utility\EventAvailability;
15
use Psr\Http\Message\ServerRequestInterface;
16
use TYPO3\CMS\Core\Routing\PageArguments;
17
use TYPO3\CMS\Core\Utility\GeneralUtility;
18
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Frontend\Conte...t\ContentObjectRenderer 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...
19
use TYPO3\CMS\Frontend\ContentObject\DataProcessorInterface;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Frontend\Conte...\DataProcessorInterface 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...
20
21
/**
22
 * Disable language item on detail or registration page, if the event is not translated
23
 *
24
 * 20 = DERHANSEN\SfEventMgt\DataProcessing\DisableLanguageMenuProcessor
25
 * 20.menus = languageMenu
26
 */
27
class DisableLanguageMenuProcessor implements DataProcessorInterface
28
{
29
    public function process(
30
        ContentObjectRenderer $cObj,
31
        array $contentObjectConfiguration,
32
        array $processorConfiguration,
33
        array $processedData
34
    ): array {
35
        if (!$processorConfiguration['menus']) {
36
            return $processedData;
37
        }
38
39
        $request = $cObj->getRequest();
40
        $eventId = $this->getEventId($request);
41
        if ($eventId === 0) {
42
            return $processedData;
43
        }
44
45
        $menus = GeneralUtility::trimExplode(',', $processorConfiguration['menus'], true);
46
        foreach ($menus as $menu) {
47
            if (isset($processedData[$menu])) {
48
                $this->handleMenu($request, $eventId, $processedData[$menu]);
49
            }
50
        }
51
52
        return $processedData;
53
    }
54
55
    protected function handleMenu(ServerRequestInterface $request, int $eventId, array &$menu): void
56
    {
57
        /** @var EventAvailability $eventAvailability */
58
        $eventAvailability = GeneralUtility::makeInstance(EventAvailability::class);
59
        foreach ($menu as &$item) {
60
            if (!$item['available']) {
61
                continue;
62
            }
63
            try {
64
                $availability = $eventAvailability->check((int)$item['languageId'], $eventId, $request);
65
                if (!$availability) {
66
                    $item['available'] = false;
67
                    $item['availableReason'] = 'sf_event_mgt';
68
                }
69
            } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
70
            }
71
        }
72
    }
73
74
    protected function getEventId(ServerRequestInterface $request): int
75
    {
76
        $eventId = 0;
77
        /** @var PageArguments $pageArguments */
78
        $pageArguments = $request->getAttribute('routing');
79
        if (isset($pageArguments->getRouteArguments()['tx_sfeventmgt_pieventdetail']['event'])) {
80
            $eventId = (int)$pageArguments->getRouteArguments()['tx_sfeventmgt_pieventdetail']['event'];
81
        } elseif (isset($request->getQueryParams()['tx_sfeventmgt_pieventdetail']['event'])) {
82
            $eventId = (int)$request->getQueryParams()['tx_sfeventmgt_pieventdetail']['event'];
83
        } elseif (isset($pageArguments->getRouteArguments()['tx_sfeventmgt_pieventregistration']['event'])) {
84
            $eventId = (int)$pageArguments->getRouteArguments()['tx_sfeventmgt_pieventregistration']['event'];
85
        } elseif (isset($request->getQueryParams()['tx_sfeventmgt_pieventregistration']['event'])) {
86
            $eventId = (int)$request->getQueryParams()['tx_sfeventmgt_pieventregistration']['event'];
87
        }
88
89
        return $eventId;
90
    }
91
}
92