Completed
Push — cleanup-service ( 60a2d8...0f35f3 )
by Romain
02:18
created

ContextService   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 80
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getContextHash() 0 6 2
A getLanguageKey() 0 20 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
22
class ContextService implements SingletonInterface
23
{
24
    use ExtendedFacadeInstanceTrait;
25
26
    /**
27
     * @var EnvironmentService
28
     */
29
    protected $environmentService;
30
31
    /**
32
     * @var TypoScriptService
33
     */
34
    protected $typoScriptService;
35
36
    /**
37
     * Returns a unique hash for the context of the current request, depending
38
     * on whether the request comes from frontend or backend.
39
     *
40
     * @return string
41
     */
42
    public function getContextHash()
43
    {
44
        return ($this->environmentService->isEnvironmentInFrontendMode())
45
            ? 'fe-' . Core::get()->getPageController()->id
46
            : 'be-' . StringService::get()->sanitizeString(GeneralUtility::_GET('M'));
47
    }
48
49
    /**
50
     * Returns the current language key.
51
     *
52
     * @return string
53
     */
54
    public function getLanguageKey()
55
    {
56
        $languageKey = 'unknown';
57
58
        if ($this->environmentService->isEnvironmentInFrontendMode()) {
59
            $pageController = Core::get()->getPageController();
60
61
            if (isset($pageController->config['config']['language'])) {
62
                $languageKey = $pageController->config['config']['language'];
63
            }
64
        } else {
65
            $backendUser = Core::get()->getBackendUser();
66
67
            if (strlen($backendUser->uc['lang']) > 0) {
68
                $languageKey = $backendUser->uc['lang'];
69
            }
70
        }
71
72
        return $languageKey;
73
    }
74
75
    /**
76
     * Will check if the TypoScript was actually included, as it contains
77
     * required configuration to make the forms work properly.
78
     *
79
     * @return bool
80
     */
81
    public function isTypoScriptIncluded()
82
    {
83
        return null !== $this->typoScriptService->getExtensionConfigurationFromPath('settings.typoScriptIncluded');
84
    }
85
86
    /**
87
     * @param EnvironmentService $environmentService
88
     */
89
    public function injectEnvironmentService(EnvironmentService $environmentService)
90
    {
91
        $this->environmentService = $environmentService;
92
    }
93
94
    /**
95
     * @param TypoScriptService $typoScriptService
96
     */
97
    public function injectTypoScriptService(TypoScriptService $typoScriptService)
98
    {
99
        $this->typoScriptService = $typoScriptService;
100
    }
101
}
102