Test Failed
Push — master ( 965da0...97ed9f )
by Vítězslav
04:38
created

Shared::linkify()   B

Complexity

Conditions 10
Paths 12

Size

Total Lines 57
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 43
c 1
b 0
f 0
nc 12
nop 3
dl 0
loc 57
ccs 0
cts 37
cp 0
crap 110
rs 7.6666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Všeobecně sdílený objekt frameworku.
4
 * Tento objekt je automaticky přez svůj singleton instancován do každého Ease*
5
 * objektu.
6
 * Poskytuje kdykoliv přístup k často volaným objektům framworku jako například
7
 * uživatel, databáze, webstránka nebo logy.
8
 * Také obsahuje pole obecnych nastavení a funkce pro jeho obluhu.
9
 *
10
 * @author    Vitex <[email protected]>
11
 * @copyright 2009-2018 [email protected] (G)
12
 */
13
14
namespace Ease;
15
16
/**
17
 * Všeobecně sdílený objekt frameworku.
18
 * Tento objekt je automaticky přez svůj singleton instancován do každého Ease*
19
 * objektu.
20
 * Poskytuje kdykoliv přístup k často volaným objektům framworku jako například
21
 * uživatel, databáze, webstránka nebo logy.
22
 * Také obsahuje pole obecnych nastavení a funkce pro jeho obluhu.
23
 *
24
 * @copyright 2009-2016 [email protected] (G)
25
 * @author    Vitex <[email protected]>
26
 */
27
class Shared extends Atom
28
{
29
    /**
30
     * Odkaz na objekt stránky.
31
     *
32
     * @var WebPage
0 ignored issues
show
Bug introduced by
The type Ease\WebPage 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...
33
     */
34
    public $webPage = null;
35
36
    /**
37
     * JavaScripts.
38
     *
39
     * @var array
40
     */
41
    public $javaScripts = null;
42
43
    /**
44
     * Pole kaskádových stylů
45
     * $var array.
46
     */
47
    public $cascadeStyles = null;
48
49
    /**
50
     * Pole konfigurací.
51
     *
52
     * @var array
53
     */
54
    public $configuration = [];
55
56
    /**
57
     * Informuje zdali je objekt spuštěn v prostředí webové stránky nebo jako script.
58
     *
59
     * @var string web|cli
60
     */
61
    public $runType = null;
62
63
    /**
64
     * Odkaz na instanci objektu uživatele.
65
     *
66
     * @var User|Anonym
0 ignored issues
show
Bug introduced by
The type Ease\User 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...
67
     */
68
    public $user = null;
69
70
    /**
71
     * Odkaz na objekt databáze.
72
     *
73
     * @var SQL\PDO
0 ignored issues
show
Bug introduced by
The type Ease\SQL\PDO 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...
74
     */
75
    public $dbLink = null;
76
77
    /**
78
     *
79
     * @var Locale 
80
     */
81
    public $locale = null;
82
83
    /**
84
     * Saves obejct instace (singleton...).
85
     *
86
     * @var Shared
87
     */
88
    private static $instance = null;
89
90
    /**
91
     * Název položky session s objektem uživatele.
92
     *
93
     * @var string
94
     */
95
    public static $userSessionName = 'User';
96
97
    /**
98
     * Logger live here
99
     * @var Logger\ToFile|Logger\ToMemory|Logger\ToSyslog
100
     */
101
    public static $log = null;
102
103
    /**
104
     * Array of Status Messages
105
     * @var array of Logger\Message
106
     */
107
    public $messages = [];
108
109
    /**
110
     * Inicializace sdílené třídy.
111
     */
112
    public function __construct()
113
    {
114
        $cgiMessages = [];
115
        $webMessages = [];
116
        $prefix      = defined('EASE_APPNAME') ? constant('EASE_APPNAME') : '';
117
        $msgFile     = sys_get_temp_dir().'/'.$prefix.'EaseStatusMessages'.posix_getuid().'.ser';
118
        if (file_exists($msgFile) && is_readable($msgFile) && filesize($msgFile)
119
            && is_writable($msgFile)) {
120
            $cgiMessages = unserialize(file_get_contents($msgFile));
121
            file_put_contents($msgFile, '');
122
        }
123
124
        if (defined('EASE_APPNAME')) {
125
            if (isset($_SESSION[constant('EASE_APPNAME')]['EaseMessages'])) {
126
                $webMessages = $_SESSION[constant('EASE_APPNAME')]['EaseMessages'];
127
                unset($_SESSION[constant('EASE_APPNAME')]['EaseMessages']);
128
            }
129
        } else {
130
            if (isset($_SESSION['EaseMessages'])) {
131
                $webMessages = $_SESSION['EaseMessages'];
132
                unset($_SESSION['EaseMessages']);
133
            }
134
        }
135
        $this->statusMessages = is_array($cgiMessages) ? array_merge($cgiMessages,
136
                $webMessages) : $webMessages;
137
    }
138
139
    /**
140
     * Pri vytvareni objektu pomoci funkce singleton (ma stejne parametry, jako konstruktor)
141
     * se bude v ramci behu programu pouzivat pouze jedna jeho Instance (ta prvni).
142
     *
143
     * @param string $class název třídy jenž má být zinstancována
144
     *
145
     * @link   http://docs.php.net/en/language.oop5.patterns.html Dokumentace a priklad
146
     *
147
     * @return \Ease\Shared
148
     */
149
    public static function singleton()
150
    {
151
        if (!isset(self::$instance)) {
152
            self::$instance = new self();
153
        }
154
        return self::$instance;
155
    }
156
157
    /**
158
     * Vrací se.
159
     *
160
     * @return Shared
161
     */
162
    public static function &instanced()
163
    {
164
        $easeShared = self::singleton();
165
166
        return $easeShared;
167
    }
168
169
    /**
170
     * Nastavuje hodnotu konfiguračního klíče.
171
     *
172
     * @param string $configName  klíč
173
     * @param mixed  $configValue hodnota klíče
174
     */
175
    public function setConfigValue($configName, $configValue)
176
    {
177
        $this->configuration[$configName] = $configValue;
178
    }
179
180
    /**
181
     * Vrací konfigurační hodnotu pod klíčem.
182
     *
183
     * @param string $configName klíč
184
     *
185
     * @return mixed
186
     */
187
    public function getConfigValue($configName)
188
    {
189
        return array_key_exists($configName, $this->configuration) ? $this->configuration[$configName]
190
                : null;
191
    }
192
193
    /**
194
     * Vrací instanci objektu logování.
195
     *
196
     * @return Logger
197
     */
198
    public static function logger()
199
    {
200
        return Logger\Regent::singleton();
201
    }
202
203
    /**
204
     * Locale Class handler
205
     * 
206
     * @param Locale $locale overriding object
207
     * 
208
     * @return Locale
209
     */
210
    public static function &locale($locale = null)
211
    {
212
        $shared = self::instanced();
213
        if (is_object($locale)) {
214
            $shared->locale = &$locale;
215
        }
216
        if (!is_object($shared->locale)) {
217
            $shared->locale = Locale::singleton();
218
        }
219
        return $shared->locale;
220
    }
221
222
    /**
223
     * Take message to print / log
224
     * @param Logger\Message $message
225
     */
226
    public function takeMessage($message)
227
    {
228
        $this->messages[] = $message;
229
        $this->addStatusMessage($message->body, $message->type);
230
        $this->logger()->addToLog($message->caller, $message->body,
231
            $message->type);
232
    }
233
234
    /**
235
     * Write remaining messages to temporary file.
236
     */
237
    public function __destruct()
238
    {
239
        if (php_sapi_name() == 'cli') {
240
            $prefix       = defined('EASE_APPNAME') ? constant('EASE_APPNAME') : '';
241
            $messagesFile = sys_get_temp_dir().'/'.$prefix.'EaseStatusMessages'.posix_getuid().'.ser';
242
            file_put_contents($messagesFile, serialize($this->statusMessages));
243
        } else {
244
            if (defined('EASE_APPNAME')) {
245
                $_SESSION[constant('EASE_APPNAME')]['EaseMessages'] = $this->statusMessages;
246
            } else {
247
                $_SESSION['EaseMessages'] = $this->statusMessages;
248
            }
249
        }
250
    }
251
252
    /**
253
     * Load Configuration values from json file $this->configFile and define UPPERCASE keys
254
     *
255
     * @param string  $configFile      Path to file with configuration
256
     * @param boolean $defineConstants false to do not define constants
257
     *
258
     * @return array full configuration array
259
     */
260
    public function loadConfig($configFile, $defineConstants = false)
261
    {
262
        if (!file_exists($configFile)) {
263
            throw new Exception('Config file '.(realpath($configFile) ? realpath($configFile)
264
                        : $configFile).' does not exist');
265
        }
266
        $configuration = json_decode(file_get_contents($configFile), true);
267
        foreach ($configuration as $configKey => $configValue) {
268
            if ($defineConstants && (strtoupper($configKey) == $configKey) && (!defined($configKey))) {
269
                define($configKey, $configValue);
270
            } else {
271
                $this->setConfigValue($configKey, $configValue);
272
            }
273
            $this->configuration[$configKey] = $configValue;
274
        }
275
276
        if (array_key_exists('debug', $this->configuration)) {
277
            $this->debug = boolval($this->configuration['debug']);
278
        }
279
280
        return $this->configuration;
281
    }
282
}
283