Passed
Push — master ( 736032...f19203 )
by Gabriel
04:19 queued 30s
created

AssetSource::buildSource()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5.025

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 5
eloc 9
c 1
b 0
f 1
nc 5
nop 2
dl 0
loc 16
ccs 9
cts 10
cp 0.9
crap 5.025
rs 9.6111
1
<?php
2
3
namespace ByTIC\Assets\Assets\Asset;
4
5
use ByTIC\Assets\Assets\Asset;
6
use Nip\Utility\Traits\SingletonTrait;
7
8
/**
9
 * Class AssetSource
10
 * @package ByTIC\Assets\Assets\Asset
11
 */
12
class AssetSource
13
{
14
    use SingletonTrait;
15
16
    /**
17
     * @param $source
18
     * @param $type
19
     * @return string
20
     */
21 9
    public static function check($source, $type)
22
    {
23
        return function () use ($source, $type) {
0 ignored issues
show
Bug Best Practice introduced by
The expression return function(...) { /* ... */ } returns the type callable which is incompatible with the documented return type string.
Loading history...
24 4
            return static::instance()->buildSource($source, $type);
25 9
        };
26
    }
27
28
    /**
29
     * @param $source
30
     * @param $type
31
     * @return string
32
     */
33 7
    protected function buildSource($source, $type)
34
    {
35 7
        if (empty($source)) {
36 1
            return $source;
37
        }
38 6
        if ($this->isFullPath($source)) {
39 4
            return $source;
40
        }
41 2
        if ($type === Asset::TYPE_STYLES) {
42 1
            return $this->transformSourceWithAsset('/stylesheets/'.$source.'.css');
43
        }
44 1
        if ($type === Asset::TYPE_SCRIPTS) {
45 1
            return $this->transformSourceWithAsset('/scripts/'.$source.'.js');
46
        }
47
48
        return $source;
49
    }
50
51
    /**
52
     * @param $source
53
     * @return bool
54
     */
55 6
    protected function isFullPath($source)
56
    {
57 6
        if (strpos($source, '.css') !== false) {
58 3
            return true;
59
        }
60 3
        if (strpos($source, '.js') !== false) {
61 1
            return true;
62
        }
63
64 2
        return false;
65
    }
66
67
    /**
68
     * @param $source
69
     * @return string
70
     */
71
    protected function transformSourceWithAsset($source)
72
    {
73
        return \asset($source);
74
    }
75
}
76