Test Failed
Push — master ( cc821f...fdc4d2 )
by Fran
09:36 queued 04:02
created

TemplateFunctions::extractPathname()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 10
Ratio 62.5 %

Code Coverage

Tests 4
CRAP Score 9.2876

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 2
nop 2
dl 10
loc 16
ccs 4
cts 9
cp 0.4444
crap 9.2876
rs 8.8571
c 0
b 0
f 0
1
<?php
2
namespace PSFS\base\extension;
3
4
use PSFS\base\config\Config;
5
use PSFS\base\exception\ConfigException;
6
use PSFS\base\Request;
7
use PSFS\base\Router;
8
use PSFS\base\Security;
9
use PSFS\base\Template;
10
use PSFS\base\types\Form;
11
use PSFS\base\types\helpers\GeneratorHelper;
12
use Symfony\Component\Translation\Tests\StringClass;
13
14
class TemplateFunctions
15
{
16
17
    const ASSETS_FUNCTION = "\\PSFS\\base\\extension\\TemplateFunctions::asset";
18
    const ROUTE_FUNCTION = "\\PSFS\\base\\extension\\TemplateFunctions::route";
19
    const CONFIG_FUNCTION = "\\PSFS\\base\\extension\\TemplateFunctions::config";
20
    const BUTTON_FUNCTION = "\\PSFS\\base\\extension\\TemplateFunctions::button";
21
    const WIDGET_FUNCTION = "\\PSFS\\base\\extension\\TemplateFunctions::widget";
22
    const FORM_FUNCTION = "\\PSFS\\base\\extension\\TemplateFunctions::form";
23
    const RESOURCE_FUNCTION = "\\PSFS\\base\\extension\\TemplateFunctions::resource";
24
    const SESSION_FUNCTION = "\\PSFS\\base\\extension\\TemplateFunctions::session";
25
    const EXISTS_FLASH_FUNCTION = "\\PSFS\\base\\extension\\TemplateFunctions::existsFlash";
26
    const GET_FLASH_FUNCTION = "\\PSFS\\base\\extension\\TemplateFunctions::getFlash";
27
    const GET_QUERY_FUNCTION = "\\PSFS\\base\\extension\\TemplateFunctions::query";
28
29
    /**
30
     * Función que copia los recursos de las carpetas Public al DocumentRoot
31
     * @param $string
32
     * @param null $name
33
     * @param bool|TRUE $return
34
     *
35
     * @return string
36
     */
37 1
    public static function asset($string, $name = null, $return = true)
38
    {
39
40 1
        $file_path = "";
41 1
        if (!file_exists($file_path)) {
42 1
            $file_path = BASE_DIR . $string;
43
        }
44 1
        $filename_path = AssetsParser::findDomainPath($string, $file_path);
45
46 1
        $file_path = self::processAsset($string, $name, $return, $filename_path);
47 1
        $return_path = (empty($name)) ? Request::getInstance()->getRootUrl() . '/' . $file_path : $name;
48 1
        return ($return) ? $return_path : '';
49
    }
50
51
    /**
52
     * Función que devuelve una url correspondiente a una ruta
53
     * @param string $path
54
     * @param bool|FALSE $absolute
55
     * @param array $params
56
     *
57
     * @return string|null
58
     */
59 1
    public static function route($path = '', $absolute = false, array $params = [])
60
    {
61 1
        $router = Router::getInstance();
62
        try {
63 1
            return $router->getRoute($path, $absolute, $params);
64 1
        } catch (\Exception $e) {
65 1
            return $router->getRoute('', $absolute, $params);
66
        }
67
    }
68
69
    /**
70
     * Función que devuelve un parámetro de la configuración
71
     * @param $param
72
     * @param string $default
73
     *
74
     * @return string
75
     */
76 1
    public static function config($param, $default = '')
77
    {
78 1
        return Config::getInstance()->get($param) ?: $default;
79
    }
80
81
    /**
82
     * Función que devuelve un query string
83
     * @param string $query
84
     *
85
     * @return string
86
     */
87
    public static function query($query)
88
    {
89
        return Request::getInstance()->getQuery($query);
90
    }
91
92
    /**
93
     * Método que devuelve un botón en html para la plantilla de formularios
94
     * @param array $button
95
     */
96
    public static function button(array $button)
97
    {
98
        Template::getInstance()->getTemplateEngine()->display('forms/button.html.twig', array(
99
            'button' => $button,
100
        ));
101
    }
102
103
    /**
104
     * Función que pinta parte de un formulario
105
     * @param array $field
106
     * @param StringClass $label
107
     */
108
    public static function widget(array $field, StringClass $label = null)
109
    {
110
        if (!empty($label)) {
111
            $field["label"] = $label;
112
        }
113
        //Limpiamos los campos obligatorios
114
        if (!isset($field["required"])) {
115
            $field["required"] = true;
116
        } elseif (isset($field["required"]) && (bool)$field["required"] === false) {
117
            unset($field["required"]);
118
        }
119
        Template::getInstance()->getTemplateEngine()->display('forms/field.html.twig', array(
120
            'field' => $field,
121
        ));
122
    }
123
124
    /**
125
     * Función que deveulve un formulario en html
126
     * @param Form $form
127
     */
128
    public static function form(Form $form)
129
    {
130
        Template::getInstance()->getTemplateEngine()->display('forms/base.html.twig', array(
131
            'form' => $form,
132
        ));
133
    }
134
135
    /**
136
     * Función que copia un recurso directamente en el DocumentRoot
137
     * @param string $path
138
     * @param string $dest
139
     * @param bool|FALSE $force
140
     *
141
     * @return string
142
     * @throws ConfigException
143
     */
144 1
    public static function resource($path, $dest, $force = false)
145
    {
146 1
        $debug = Config::getInstance()->getDebugMode();
147 1
        $domains = Template::getDomains(true);
148 1
        $filename_path = self::extractPathname($path, $domains);
149 1
        \PSFS\Services\GeneratorService::copyResources($dest, $force, $filename_path, $debug);
150 1
        return '';
151
    }
152
153
    /**
154
     * Método que extrae el pathname para un dominio
155
     * @param string $path
156
     * @param $domains
157
     *
158
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
159
     */
160 1
    private static function extractPathname($path, $domains)
161
    {
162 1
        $filename_path = $path;
163 1 View Code Duplication
        if (!file_exists($path) && !empty($domains)) {
164
            foreach ($domains as $domain => $paths) {
165
                $domain_filename = str_replace($domain, $paths["public"], $path);
166
                if (file_exists($domain_filename)) {
167
                    $filename_path = $domain_filename;
168
                    continue;
169
                }
170
            }
171
172
        }
173
174 1
        return $filename_path;
175
    }
176
177
    /**
178
     * @param string $filename_path
179
     */
180
    private static function processCssLines($filename_path)
181
    {
182
        $handle = @fopen($filename_path, 'r');
183
        if ($handle) {
184
            while (!feof($handle)) {
185
                AssetsParser::extractCssLineResource($handle, $filename_path);
186
            }
187
            fclose($handle);
188
        }
189
    }
190
191
    /**
192
     * Método que copia el contenido de un recurso en su destino correspondiente
193
     * @param string $name
194
     * @param string $filename_path
195
     * @param string $base
196
     * @param string $file_path
197
     */
198
    private static function putResourceContent($name, $filename_path, $base, $file_path)
199
    {
200
        $data = file_get_contents($filename_path);
201
        if (!empty($name)) file_put_contents(WEB_DIR . DIRECTORY_SEPARATOR . $name, $data);
202
        else file_put_contents($base . $file_path, $data);
203
    }
204
205
    /**
206
     * Método que procesa un recurso para su copia en el DocumentRoot
207
     * @param string $string
208
     * @param string $name
209
     * @param boolean $return
210
     * @param string $filename_path
211
     *
212
     * @return string
213
     */
214 1
    private static function processAsset($string, $name, $return, $filename_path)
215
    {
216 1
        $file_path = $filename_path;
217 1
        if (file_exists($filename_path)) {
218
            list($base, $html_base, $file_path) = AssetsParser::calculateAssetPath($string, $name, $return, $filename_path);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 124 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
219
            //Creamos el directorio si no existe
220
            GeneratorHelper::createDir($base . $html_base);
221
            //Si se ha modificado
222
            if (!file_exists($base . $file_path) || filemtime($base . $file_path) < filemtime($filename_path)) {
223
                if ($html_base == 'css') {
224
                    self::processCssLines($filename_path);
225
                }
226
                self::putResourceContent($name, $filename_path, $base, $file_path);
227
            }
228
        }
229
230 1
        return $file_path;
231
    }
232
233
    /**
234
     * Template function for get a session var
235
     * @param string $key
236
     * @return mixed
237
     */
238 1
    public static function session($key)
239
    {
240 1
        return Security::getInstance()->getSessionKey($key);
241
    }
242
243
    /**
244
     * Template function that check if exists any flash session var
245
     * @param string $key
246
     * @return bool
247
     */
248 1
    public static function existsFlash($key = '')
249
    {
250 1
        return null !== Security::getInstance()->getFlash($key);
251
    }
252
253
    /**
254
     * Template function that get a flash session var
255
     * @param string $key
256
     * @return mixed
257
     */
258
    public static function getFlash($key)
259
    {
260
        $var = Security::getInstance()->getFlash($key);
261
        Security::getInstance()->setFlash($key, null);
262
        return $var;
263
    }
264
}