Passed
Push — master ( 97b83c...a9605e )
by Alexey
05:18
created

Statics::file()   C

Complexity

Conditions 8
Paths 12

Size

Total Lines 25
Code Lines 20

Duplication

Lines 7
Ratio 28 %

Importance

Changes 0
Metric Value
cc 8
eloc 20
nc 12
nop 4
dl 7
loc 25
rs 5.3846
c 0
b 0
f 0
1
<?php
2
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 = '') {
23 View Code Duplication
        if (!$path) {
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...
24
            return !empty(\App::$primary->config['site']['noimage']) ? \App::$primary->config['site']['noimage'] : '/static/system/images/no-image.png';
25
        }
26
        $absolutePath = App::$cur->staticLoader->parsePath($path);
27
        $convert = false;
28 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...
29
            $absolutePath = mb_convert_encoding($absolutePath, 'Windows-1251', 'UTF-8');
30
            $convert = true;
31
        }
32
        if (!file_exists($absolutePath)) {
33
            return '';
34
        } else {
35
            $options = [];
36
            if ($resize) {
37
                $resize = explode('x', $resize);
38
                $options = ['resize' => ['x' => $resize[0], 'y' => $resize[1]]];
39
            }
40
            $options['crop'] = $resizeCrop;
41
            $options['pos'] = $resizePos;
42
            $path = Cache::file($absolutePath, $options);
43
            $path = $convert ? mb_convert_encoding($path, 'UTF-8', 'Windows-1251') : $path;
44
            return '/' . $path;
45
        }
46
    }
47
48
}
49