Passed
Push — master ( e4f13f...46daff )
by Takashi
02:18
created

AssetResolver::getAssetPaths()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 9
nop 2
dl 0
loc 22
rs 8.6737
c 0
b 0
f 0
1
<?php
2
3
namespace Ttskch;
4
5
class AssetResolver
6
{
7
    /**
8
     * @var array
9
     */
10
    private $categoryBasedConfig;
11
12
    /**
13
     * @var array
14
     */
15
    private $tagBasedConfig;
16
17
    const DEFAULT_CSS_PATH = 'css/post/default.css';
18
    const DEFAULT_JS_PATH = 'js/post/default.js';
19
20
    public function __construct(array $config)
21
    {
22
        $this->categoryBasedConfig = [];
23
        $this->tagBasedConfig = [];
24
25
        foreach ($config as $key => $value) {
26
            if (strpos($key, '#') === 0) {
27
                $this->tagBasedConfig[$key] = $value;
28
            } else {
29
                $this->categoryBasedConfig[$key] = $value;
30
            }
31
        }
32
33
        // deeper category should win.
34
        krsort($this->categoryBasedConfig, SORT_NATURAL);
35
    }
36
37
    /**
38
     * @param string $category
39
     * @param array $tags
40
     * @return array
41
     */
42
    public function getAssetPaths($category, array $tags)
43
    {
44
        $assetPaths = [
45
            'css' => self::DEFAULT_CSS_PATH,
46
            'js' => self::DEFAULT_JS_PATH,
47
        ];
48
49
        foreach ($this->categoryBasedConfig as $matcher => $paths) {
50
            if (preg_match(sprintf('#^%s#', $matcher), $category)) {
51
                $assetPaths = array_merge($assetPaths, $paths);
52
                break;  // deeper category should match early.
53
            }
54
        }
55
56
        foreach ($this->tagBasedConfig as $matcher => $paths) {
57
            if (in_array(substr($matcher, 1), $tags)) {
58
                $assetPaths = array_merge($assetPaths, $paths);
59
                break;
60
            }
61
        }
62
63
        return $assetPaths;
64
    }
65
}
66