Passed
Push — 7.x ( 46400b...fb51d4 )
by Torben
03:19
created

DisableLanguageMenuProcessor::getRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
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
        $eventId = $this->getEventId($cObj->getRequest());
40
        if ($eventId === 0) {
41
            return $processedData;
42
        }
43
44
        $menus = GeneralUtility::trimExplode(',', $processorConfiguration['menus'], true);
45
        foreach ($menus as $menu) {
46
            if (isset($processedData[$menu])) {
47
                $this->handleMenu($eventId, $processedData[$menu]);
48
            }
49
        }
50
51
        return $processedData;
52
    }
53
54
    protected function handleMenu(int $eventId, array &$menu): void
55
    {
56
        $eventAvailability = GeneralUtility::makeInstance(EventAvailability::class);
57
        foreach ($menu as &$item) {
58
            if (!$item['available']) {
59
                continue;
60
            }
61
            try {
62
                $availability = $eventAvailability->check((int)$item['languageId'], $eventId);
63
                if (!$availability) {
64
                    $item['available'] = false;
65
                    $item['availableReason'] = 'sf_event_mgt';
66
                }
67
            } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
68
            }
69
        }
70
    }
71
72
    protected function getEventId(ServerRequestInterface $request): int
73
    {
74
        $eventId = 0;
75
        /** @var PageArguments $pageArguments */
76
        $pageArguments = $request->getAttribute('routing');
77
        if (isset($pageArguments->getRouteArguments()['tx_sfeventmgt_pieventdetail']['event'])) {
78
            $eventId = (int)$pageArguments->getRouteArguments()['tx_sfeventmgt_pieventdetail']['event'];
79
        } elseif (isset($request->getQueryParams()['tx_sfeventmgt_pieventdetail']['event'])) {
80
            $eventId = (int)$request->getQueryParams()['tx_sfeventmgt_pieventdetail']['event'];
81
        } elseif (isset($pageArguments->getRouteArguments()['tx_sfeventmgt_pieventregistration']['event'])) {
82
            $eventId = (int)$pageArguments->getRouteArguments()['tx_sfeventmgt_pieventregistration']['event'];
83
        } elseif (isset($request->getQueryParams()['tx_sfeventmgt_pieventregistration']['event'])) {
84
            $eventId = (int)$request->getQueryParams()['tx_sfeventmgt_pieventregistration']['event'];
85
        }
86
87
        return $eventId;
88
    }
89
}
90