Test Failed
Branch v5 (12d602)
by Alexey
04:51
created

Statics::file()   D

Complexity

Conditions 9
Paths 60

Size

Total Lines 28
Code Lines 21

Duplication

Lines 4
Ratio 14.29 %

Importance

Changes 0
Metric Value
cc 9
eloc 21
nc 60
nop 5
dl 4
loc 28
rs 4.909
c 0
b 0
f 0
1
<?php
2
namespace Inji;
3
/**
4
 * Statics
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2015 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
class Statics {
12
13
    /**
14
     * Cached static file and return absolute url for client side use
15
     *
16
     * @param string $path
17
     * @param string $resize
18
     * @param string $resizeCrop
19
     * @param string $resizePos
20
     * @return string
21
     */
22
    public static function file($path, $resize = '', $resizeCrop = '', $resizePos = '', $pathAbsolute = false) {
23
        if (!$path) {
24
            $pathAbsolute = false;
25
            $path = !empty(App::$primary->config['site']['noimage']) ? App::$primary->config['site']['noimage'] : '/static/system/images/no-image.png';
26
        }
27
28
        $absolutePath = $pathAbsolute ? $path : App::$cur->staticLoader->parsePath($path);
29
30
        $convert = false;
31 View Code Duplication
        if (!file_exists($absolutePath) && file_exists(mb_convert_encoding($absolutePath, 'Windows-1251', 'UTF-8'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
32
            $absolutePath = mb_convert_encoding($absolutePath, 'Windows-1251', 'UTF-8');
33
            $convert = true;
34
        }
35
        if (!file_exists($absolutePath)) {
36
            return '';
37
        } else {
38
            $options = [];
39
            if ($resize) {
40
                $resize = explode('x', $resize);
41
                $options = ['resize' => ['x' => $resize[0], 'y' => $resize[1]]];
42
            }
43
            $options['crop'] = $resizeCrop;
44
            $options['pos'] = $resizePos;
45
            $path = Cache::file($absolutePath, $options);
46
            $path = $convert ? mb_convert_encoding($path, 'UTF-8', 'Windows-1251') : $path;
47
            return '/' . $path;
48
        }
49
    }
50
}