Passed
Push — develop ( 58d21c...a0b112 )
by Nikolay
06:04
created

MessagesProvider   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 19
eloc 58
dl 0
loc 96
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
D register() 0 90 19
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * Copyright (C) MIKO LLC - All Rights Reserved
6
 * Unauthorized copying of this file, via any medium is strictly prohibited
7
 * Proprietary and confidential
8
 * Written by Nikolay Beketov, 4 2020
9
 *
10
 */
11
12
namespace MikoPBX\Common\Providers;
13
14
use MikoPBX\Core\System\MikoPBXConfig;
15
use Phalcon\Di\DiInterface;
16
use Phalcon\Di\ServiceProviderInterface;
17
18
use function MikoPBX\Common\Config\appPath;
19
20
class MessagesProvider implements ServiceProviderInterface
21
{
22
23
    /**
24
     * @inheritDoc
25
     */
26
    public function register(DiInterface $di): void
27
    {
28
        $coreConfig = $di->getShared('config')->get('core');
29
        $di->setShared(
30
            'messages',
31
            function () use ($di, $coreConfig) {
32
                $cacheKey = false;
33
                if (php_sapi_name() === 'cli'){
34
                    if (!empty($_ENV['SSH_CLIENT'])) {
35
                        $language = 'en';
36
                    } else {
37
                        $conf     = new MikoPBXConfig();
38
                        $language = $conf->getGeneralSettings('SSHLanguage');
39
                    }
40
                } else {
41
                    $language = $di->get('language');
42
                    $session  = $di->get('session');
43
                    if ($session !== null && $session->has('versionHash')) {
44
                        $cacheKey = 'LocalisationArray' . $session->get('versionHash') . $language . '.php';
45
                    }
46
                }
47
48
                $messages = [];
49
                // Заглянем сначала в кеш переводов
50
51
                if ($cacheKey) {
52
                    $translates = $di->get('managedCache')->get($cacheKey, 3600);
53
                    if (is_array($translates)) {
54
                        return $translates;
55
                    }
56
                }
57
58
                $translates = [];
59
                // Возьмем английский интерфейс
60
                $enFilePath = appPath('/src/Common/Messages/en.php');
61
                if (file_exists($enFilePath)) {
62
                    $translates = require $enFilePath;
63
                }
64
65
                if ($language !== 'en') {
66
                    $additionalTranslates = [];
67
                    // Check if we have a translation file for that lang
68
                    $langFile = appPath("/src/Common/Messages/{$language}.php");
69
                    if (file_exists($langFile)) {
70
                        $additionalTranslates = require $langFile;
71
                    }
72
                    // Заменим английские переводы на выбранный админом язык
73
                    if ($additionalTranslates !== [[]]) {
74
                        $translates = array_merge($translates, $additionalTranslates);
75
                    }
76
                }
77
78
                // Возьмем английский перевод расширений
79
                $extensionsTranslates = [[]];
80
                $results              = glob($coreConfig->modulesDir . '/*/{Messages}/en.php', GLOB_BRACE);
81
                foreach ($results as $path) {
82
                    $langArr = require $path;
83
                    if (is_array($langArr)) {
84
                        $extensionsTranslates[] = $langArr;
85
                    } else {
86
                        $extensionsTranslates[] = $messages; // Поддержка старых модулей
87
                    }
88
                }
89
                if ($extensionsTranslates !== [[]]) {
90
                    $translates = array_merge($translates, ...$extensionsTranslates);
91
                }
92
                if ($language !== 'en') {
93
                    $additionalTranslates = [[]];
94
                    $results              = glob(
95
                        $coreConfig->modulesDir . "/*/{Messages}/{$language}.php",
96
                        GLOB_BRACE
97
                    );
98
                    foreach ($results as $path) {
99
                        $langArr = require $path;
100
                        if (is_array($langArr)) {
101
                            $additionalTranslates[] = $langArr;
102
                        } else {
103
                            $additionalTranslates[] = $messages; // Поддержка старых модулей
104
                        }
105
                    }
106
                    if ($additionalTranslates !== [[]]) {
107
                        $translates = array_merge($translates, ...$additionalTranslates);
108
                    }
109
                }
110
                if ($cacheKey) {
111
                    $this->get('managedCache')->set($cacheKey, $translates);
0 ignored issues
show
Bug introduced by
The method get() does not exist on MikoPBX\Common\Providers\MessagesProvider. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

111
                    $this->/** @scrutinizer ignore-call */ 
112
                           get('managedCache')->set($cacheKey, $translates);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
112
                }
113
114
                // Return a translation object
115
                return $translates;
116
            }
117
        );
118
    }
119
}