AssetSource   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 85.71%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 21
c 1
b 0
f 1
dl 0
loc 65
ccs 18
cts 21
cp 0.8571
rs 10
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A isFullPath() 0 13 4
A transformSourceWithAsset() 0 3 1
A buildSource() 0 16 5
A check() 0 4 1
1
<?php
2
3
namespace ByTIC\Assets\Assets\Asset;
4
5
use ByTIC\Assets\Assets\Asset;
6
use Nip\Utility\Str;
7
use Nip\Utility\Traits\SingletonTrait;
8
9
/**
10
 * Class AssetSource
11
 * @package ByTIC\Assets\Assets\Asset
12
 */
13
class AssetSource
14
{
15
    use SingletonTrait;
16
17
    /**
18
     * @param $source
19
     * @param $type
20
     * @return string
21 9
     */
22
    public static function check($source, $type)
23
    {
24 4
        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...
25 9
            return static::instance()->buildSource($source, $type);
26
        };
27
    }
28
29
    /**
30
     * @param $source
31
     * @param $type
32
     * @return string
33 7
     */
34
    protected function buildSource($source, $type)
35 7
    {
36 1
        if (empty($source)) {
37
            return $source;
38 6
        }
39 4
        if ($this->isFullPath($source)) {
40
            return $source;
41 2
        }
42 1
        if ($type === Asset::TYPE_STYLES) {
43
            return $this->transformSourceWithAsset('/stylesheets/'.$source.'.css');
44 1
        }
45 1
        if ($type === Asset::TYPE_SCRIPTS) {
46
            return $this->transformSourceWithAsset('/scripts/'.$source.'.js');
47
        }
48
49
        return $source;
50
    }
51
52
    /**
53
     * @param $source
54
     * @return bool
55 6
     */
56
    protected function isFullPath($source)
57 6
    {
58 3
        if (Str::startsWith($source, 'http')) {
59
            return true;
60 3
        }
61 1
        if (strpos($source, '.css') !== false) {
62
            return true;
63
        }
64 2
        if (strpos($source, '.js') !== false) {
65
            return true;
66
        }
67
68
        return false;
69
    }
70
71
    /**
72
     * @param $source
73
     * @return string
74
     */
75
    protected function transformSourceWithAsset($source)
76
    {
77
        return \asset($source);
78
    }
79
}
80