Completed
Push — typo3-v8-compatibility ( ae4db2...381377 )
by Romain
02:16
created

ContextService   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 9

Importance

Changes 0
Metric Value
wmc 13
lcom 2
cbo 9
dl 0
loc 102
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getContextHash() 0 6 2
A getLanguageKey() 0 20 4
A translate() 0 11 4
A isTypoScriptIncluded() 0 4 1
A injectEnvironmentService() 0 4 1
A injectTypoScriptService() 0 4 1
1
<?php
2
/*
3
 * 2017 Romain CANON <[email protected]>
4
 *
5
 * This file is part of the TYPO3 Formz project.
6
 * It is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU General Public License, either
8
 * version 3 of the License, or any later version.
9
 *
10
 * For the full copyright and license information, see:
11
 * http://www.gnu.org/licenses/gpl-3.0.html
12
 */
13
14
namespace Romm\Formz\Service;
15
16
use Romm\Formz\Core\Core;
17
use Romm\Formz\Service\Traits\ExtendedFacadeInstanceTrait;
18
use TYPO3\CMS\Core\SingletonInterface;
19
use TYPO3\CMS\Core\Utility\GeneralUtility;
20
use TYPO3\CMS\Extbase\Service\EnvironmentService;
21
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
22
23
class ContextService implements SingletonInterface
24
{
25
    use ExtendedFacadeInstanceTrait;
26
27
    /**
28
     * @var EnvironmentService
29
     */
30
    protected $environmentService;
31
32
    /**
33
     * @var TypoScriptService
34
     */
35
    protected $typoScriptService;
36
37
    /**
38
     * Returns a unique hash for the context of the current request, depending
39
     * on whether the request comes from frontend or backend.
40
     *
41
     * @return string
42
     */
43
    public function getContextHash()
44
    {
45
        return ($this->environmentService->isEnvironmentInFrontendMode())
46
            ? 'fe-' . Core::get()->getPageController()->id
47
            : 'be-' . StringService::get()->sanitizeString(GeneralUtility::_GET('M'));
0 ignored issues
show
Bug introduced by
It seems like \TYPO3\CMS\Core\Utility\GeneralUtility::_GET('M') targeting TYPO3\CMS\Core\Utility\GeneralUtility::_GET() can also be of type array or null; however, Romm\Formz\Service\StringService::sanitizeString() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
48
    }
49
50
    /**
51
     * Returns the current language key.
52
     *
53
     * @return string
54
     */
55
    public function getLanguageKey()
56
    {
57
        $languageKey = 'unknown';
58
59
        if ($this->environmentService->isEnvironmentInFrontendMode()) {
60
            $pageController = Core::get()->getPageController();
61
62
            if (isset($pageController->config['config']['language'])) {
63
                $languageKey = $pageController->config['config']['language'];
64
            }
65
        } else {
66
            $backendUser = Core::get()->getBackendUser();
67
68
            if (strlen($backendUser->uc['lang']) > 0) {
69
                $languageKey = $backendUser->uc['lang'];
70
            }
71
        }
72
73
        return $languageKey;
74
    }
75
76
    /**
77
     * Translation handler. Does the same job as Extbase translation tools,
78
     * expect that if the index to the LLL reference is not found, the index is
79
     * returned (Extbase would have returned an empty string).
80
     *
81
     * @param    string $index        The index to the LLL reference.
82
     * @param    string $extensionKey Key of the extension containing the LLL reference.
83
     * @param    array  $arguments    Arguments passed over to vsprintf.
84
     * @return   string               The translated string.
85
     */
86
    public function translate($index, $extensionKey = null, $arguments = null)
87
    {
88
        $extensionKey = ($extensionKey) ?: ExtensionService::get()->getExtensionKey();
89
        $result = LocalizationUtility::translate($index, $extensionKey, $arguments);
90
91
        if ($result === '' && $index !== '') {
92
            $result = $index;
93
        }
94
95
        return $result;
96
    }
97
98
    /**
99
     * Will check if the TypoScript was actually included, as it contains
100
     * required configuration to make the forms work properly.
101
     *
102
     * @return bool
103
     */
104
    public function isTypoScriptIncluded()
105
    {
106
        return null !== $this->typoScriptService->getExtensionConfigurationFromPath('settings.typoScriptIncluded');
107
    }
108
109
    /**
110
     * @param EnvironmentService $environmentService
111
     */
112
    public function injectEnvironmentService(EnvironmentService $environmentService)
113
    {
114
        $this->environmentService = $environmentService;
115
    }
116
117
    /**
118
     * @param TypoScriptService $typoScriptService
119
     */
120
    public function injectTypoScriptService(TypoScriptService $typoScriptService)
121
    {
122
        $this->typoScriptService = $typoScriptService;
123
    }
124
}
125