Completed
Push — master ( 8757d5...111d7a )
by Mihail
03:00
created

Alias::__construct()   D

Complexity

Conditions 10
Paths 64

Size

Total Lines 45
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 18
Bugs 6 Features 0
Metric Value
c 18
b 6
f 0
dl 0
loc 45
rs 4.8196
cc 10
eloc 26
nc 64
nop 0

How to fix   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
namespace Ffcms\Core;
4
5
use Ffcms\Core\Helper\Type\Arr;
6
use Ffcms\Core\Helper\Type\Obj;
7
use Ffcms\Core\Helper\Type\Str;
8
9
10
/**
11
 * Class Alias - fast alias for core property's
12
 * @package Ffcms\Core
13
 */
14
class Alias
15
{
16
17
    /**
18
     * Absolute path to current view folder
19
     * @var string
20
     */
21
    public $currentViewPath;
22
23
    /**
24
     * Return full URL of current view folder
25
     * @var string
26
     */
27
    public $currentViewUrl;
28
29
    /**
30
     * Current app basic domain address, obtained from request
31
     * @var string
32
     */
33
    public $baseDomain;
34
35
    /**
36
     * Current app basic URL without language path
37
     * @var string
38
     */
39
    public $baseUrlNoLang;
40
41
    /**
42
     * Current app basic URL address, obtained from request
43
     * @var string
44
     */
45
    public $baseUrl;
46
47
    /**
48
     * Current app basic URL without any changes in pathway(lang-defined, etc)
49
     * @var string
50
     */
51
    public $scriptUrl;
52
53
54
    /**
55
     * Default vendor library's
56
     * @var array
57
     */
58
    protected $vendorNamedLibrary = [
59
        'js' => null,
60
        'css' => null
61
    ];
62
63
    /**
64
     * Custom code library's
65
     * @var array
66
     */
67
    protected $codeCustomLibrary = [
68
        'js' => null,
69
        'css' => null
70
    ];
71
72
    /**
73
     * Custom code storage for templates
74
     * @var array
75
     */
76
    protected $plainCode = [
77
        'js' => null,
78
        'css' => null
79
    ];
80
81
    /**
82
     * Alias constructor. Build alias properties for system data to provide fast-access from apps and other places.
83
     */
84
    public function __construct()
85
    {
86
        // make alias for view pathway
87
        $this->currentViewPath = App::$View->themePath;
88
89
        // make alias for baseUrl, script url and domain
90
        $this->baseDomain = App::$Request->getHttpHost();
91
        if (Str::likeEmpty($this->baseDomain)) {
92
            $this->baseDomain = App::$Properties->get('baseDomain');
93
        }
94
        // build script url
95
        $this->scriptUrl = App::$Request->getScheme() . '://' . $this->baseDomain;
96
        if (App::$Properties->get('basePath') !== '/') {
97
            $this->scriptUrl .= rtrim(App::$Properties->get('basePath'), '/');
98
        }
99
        // build base url (with current used interface path slug)
100
        $this->baseUrl = $this->scriptUrl;
101
        if (App::$Request->getInterfaceSlug() !== null) {
102
            $this->baseUrl .= App::$Request->getInterfaceSlug();
103
        }
104
105
        $this->baseUrlNoLang = $this->baseUrl;
106
        if (App::$Request->languageInPath() && App::$Request->getLanguage() !== null) {
107
            $this->baseUrl .= '/' . App::$Request->getLanguage();
108
        }
109
110
        // add cron initiation from user if enabled
111
        if ((bool)App::$Properties->get('userCron') && env_name === 'Front') {
112
            $this->addPlainCode('js', 'if(Math.random() > 0.8) { $.get("' . $this->scriptUrl . '/cron.php?rand=" + Math.random()); }');
113
        }
114
115
        // build vendor libs alias
116
        $this->vendorNamedLibrary['js']['jquery'] = $this->scriptUrl . '/vendor/bower/jquery/dist/jquery.min.js';
117
        $this->vendorNamedLibrary['css']['bootstrap'] = $this->scriptUrl . '/vendor/bower/bootstrap/dist/css/bootstrap.min.css';
118
        $this->vendorNamedLibrary['js']['bootstrap'] = $this->scriptUrl . '/vendor/bower/bootstrap/dist/js/bootstrap.min.js';
119
        $this->vendorNamedLibrary['css']['fa'] = $this->scriptUrl . '/vendor/bower/components-font-awesome/css/font-awesome.min.css';
120
        $this->vendorNamedLibrary['js']['jquery-ui'] = $this->scriptUrl . '/vendor/bower/jquery-ui/jquery-ui.min.js';
121
        $this->vendorNamedLibrary['css']['jquery-ui'] = $this->scriptUrl . '/vendor/bower/jquery-ui/themes/base/jquery-ui.min.css';
122
123
        $themeAll = App::$Properties->get('theme');
124
        if (!isset($themeAll[env_name]) || Str::length($themeAll[env_name]) < 1) {
125
            $themeAll[env_name] = 'default';
126
        }
127
        $this->currentViewUrl = $this->scriptUrl . '/Apps/View/' . env_name . '/' . $themeAll[env_name];
128
    }
129
130
    /**
131
     * Get vendor library url if defined by type and name. Example: getVendor('js', 'jquery')
132
     * @param string $type
133
     * @param string $name
134
     * @return string|null
135
     */
136
    public function getVendor($type, $name)
137
    {
138
        return $this->vendorNamedLibrary[$type][$name];
139
    }
140
141
    /**
142
     * Set custom library $link by $type
143
     * @param string $type
144
     * @param string $link
145
     */
146
    public function setCustomLibrary($type, $link)
147
    {
148
        $this->codeCustomLibrary[$type][] = $link;
149
    }
150
151
    /**
152
     * Get custom library array by type
153
     * @param string $type
154
     * @return array|null
155
     */
156
    public function getCustomLibraryArray($type)
157
    {
158
        return $this->codeCustomLibrary[$type];
159
    }
160
161
    /**
162
     * Unset custom library's by type
163
     * @param $type
164
     */
165
    public function unsetCustomLibrary($type)
166
    {
167
        unset($this->codeCustomLibrary[$type]);
168
    }
169
170
    /**
171
     * @param string $type
172
     * @param string $code
173
     * @return bool
174
     */
175
    public function addPlainCode($type, $code)
176
    {
177
        $allowed = ['css', 'js'];
178
        if (!Arr::in($type, $allowed)) {
179
            return false;
180
        }
181
182
        $this->plainCode[$type][] = $code;
183
        return true;
184
    }
185
186
    /**
187
     * Get plain code build container as string
188
     * @param string $type
189
     * @return null|string
190
     */
191
    public function getPlainCode($type)
192
    {
193
        return $this->plainCode[$type];
194
    }
195
196
197
}