Passed
Push — develop ( f1ffbe...e5fd9d )
by Nikolay
05:17
created

LanguageProvider::getLanguageForCli()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
dl 0
loc 9
rs 10
c 1
b 0
f 0
cc 3
nc 2
nop 1
1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright © 2017-2023 Alexey Portnov and Nikolay Beketov
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with this program.
17
 * If not, see <https://www.gnu.org/licenses/>.
18
 */
19
20
declare(strict_types=1);
21
22
namespace MikoPBX\Common\Providers;
23
24
use MikoPBX\Common\Models\PbxSettings;
25
use MikoPBX\Common\Models\PbxSettingsConstants;
26
use MikoPBX\Core\Workers\WorkerModelsEvents;
27
use MikoPBX\PBXCoreREST\Workers\WorkerApiCommands;
28
use Phalcon\Di\DiInterface;
29
use Phalcon\Di\ServiceProviderInterface;
30
31
class LanguageProvider implements ServiceProviderInterface
32
{
33
    public const SERVICE_NAME = 'language';
34
    public const PREFERRED_LANG_WEB = 'PREFERRED_LANG_WEB';
35
36
    /**
37
     * Registers the language service provider.
38
     *
39
     * @param DiInterface $di The DI container.
40
     */
41
    public function register(DiInterface $di): void
42
    {
43
        // Capture $this in a variable that can be used in the closure
44
        $provider = $this;
45
        $di->setShared(self::SERVICE_NAME, function () use ($di, $provider) {
46
            return $provider->determineLanguage($di);
47
        });
48
    }
49
50
    /**
51
     * Determines the language setting based on the execution environment and specific conditions.
52
     *
53
     * @param DiInterface $di Dependency Injection container.
54
     * @return string Language code.
55
     */
56
    private function determineLanguage(DiInterface $di): string
57
    {
58
        if ($this->isCliEnvironment()) {
59
            return $this->getLanguageForCli($di);
60
        }
61
62
        return $this->getLanguageForWeb($di);
63
    }
64
65
    /**
66
     * Checks if the current environment is CLI.
67
     *
68
     * @return bool True if CLI, false otherwise.
69
     */
70
    private function isCliEnvironment(): bool
71
    {
72
        return php_sapi_name() === 'cli';
73
    }
74
75
    /**
76
     * Determines the language for CLI environment.
77
     *
78
     * @param DiInterface $di Dependency Injection container.
79
     * @return string Language code.
80
     */
81
    private function getLanguageForCli(DiInterface $di): string
82
    {
83
        $processTitle = cli_get_process_title();
84
85
        if ($this->isApiOrModelEventProcess($processTitle) || $di->has(self::PREFERRED_LANG_WEB)) {
86
            return PbxSettings::getValueByKey(PbxSettingsConstants::WEB_ADMIN_LANGUAGE);
87
        }
88
89
        return PbxSettings::getValueByKey(PbxSettingsConstants::SSH_LANGUAGE);
90
    }
91
92
    /**
93
     * Checks if the process is related to API commands or model events.
94
     *
95
     * @param string $processTitle The title of the current process.
96
     * @return bool True if it's an API command or model event process, false otherwise.
97
     */
98
    private function isApiOrModelEventProcess(string $processTitle): bool
99
    {
100
        return strpos($processTitle, WorkerApiCommands::class) !== false ||
101
            strpos($processTitle, WorkerModelsEvents::class) !== false;
102
    }
103
104
    /**
105
     * Determines the language for web environment.
106
     *
107
     * @param DiInterface $di Dependency Injection container.
108
     * @return string Language code.
109
     */
110
    private function getLanguageForWeb(DiInterface $di): string
111
    {
112
        $session = $di->getShared(SessionProvider::SERVICE_NAME);
113
        $language = $session->get('WebAdminLanguage') ?? '';
114
115
        if (empty($language)) {
116
            $language = PbxSettings::getValueByKey(PbxSettingsConstants::WEB_ADMIN_LANGUAGE);
117
        }
118
119
        return $language;
120
    }
121
}
122