Themes::applyThemeConfig()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 0
dl 0
loc 11
ccs 9
cts 9
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace PeterColes\Themes;
4
5
class Themes
6
{
7
    protected $theme = null;
8
9
    /**
10
     * Get the name of the theme for this context.
11
     * Make its path available for view selection
12
     * and override config settings where needed.
13
     *
14
     * @param  \Illuminate\Http\Request  $request
15
     * @return void
16
     */
17 26
    public function setTheme($request)
18
    {
19 26
        $this->findMatch($request);
20 24
        $this->addThemeViewPath();
21 24
        $this->applyThemeConfig();
22 24
    }
23
24
    /**
25
     * Getter for the theme that is chosen.
26
     *
27
     * @return string theme
28
     */
29 22
    public function getTheme()
30
    {
31 22
        return $this->theme;
32
    }
33
34
    /**
35
     * Iterate over the match criteria until a successful match is achieved.
36
     *
37
     * @return void
38
     */
39 26
    protected function findMatch($request)
40
    {
41 26
        foreach (app('config')->get('themes') as $match)
42
        {
43 26
            if ($this->testMatch($request, $match)) {
44 14
                break;
45
            }
46 24
        }
47 24
    }
48
49
    /**
50
     * If there's no match critera or all rules are matched, set the theme and exit.
51
     *
52
     * @return boolean
53
     */
54 26
    protected function testMatch($request, $match)
55
    {
56 26
        if (isset($match['match'])) {
57 21
            $rules = $this->explodeRules($match['match']);
58
59 21
            foreach ($rules as $rule) {
60 21
                if (!$this->handleRule($request, $rule)) {
61 10
                    return false;
62
                }
63 11
            }
64 9
        }
65
66
        // nothing to match or all matches succeeded
67 14
        $this->theme = $match['theme'];
68 14
        return true;
69
    }
70
71
    /**
72
     * Remove whtespace from rules and separate the rules in a match group.
73
     *
74
     * @return array rules
75
     */
76 21
    protected function explodeRules($rules)
77
    {
78 21
        $rules = preg_replace('/\s+/', '', $rules);
79 21
        return explode('|', $rules);
80
    }
81
82
    /**
83
     * Separate rule name from parameters and pass to matcher class to evaluate.
84
     *
85
     * @return boolean
86
     */
87 21
    protected function handleRule($request, $rule)
88
    {
89 21
        $rule = explode(':', $rule);
90 21
        $params = isset($rule[1]) ? $rule[1] : null;
91 21
        $class = '\\PeterColes\\Themes\\Matchers\\'.studly_case($rule[0]);
92
93 21
        return (new $class)->handle($request, $params);
94
    }
95
96
    /**
97
     * Add path for current theme's views to the list that Laravel searches.
98
     *
99
     * @return void
100
     */
101 24
    protected function addThemeViewPath()
102
    {
103 24
        if ($this->theme) {
104 12
            app('view.finder')->addLocation(themes_path($this->theme.'/resources/views'));
105 12
        }
106 24
    }
107
108
    /**
109
     * Override configs with theme-specific settings.
110
     *
111
     * @return void
112
     */
113 24
    protected function applyThemeConfig()
114
    {
115 24
        $path = themes_path($this->theme.'/config');
116
117 24
        if (app('files')->exists($path)) {
118 1
            foreach (app('files')->allFiles($path) as $file) {
119 1
                $key = str_replace('.'.$file->getExtension(), '', $file->getFilename());
120 1
                app('config')->set($key, require $file->getPathname());
121 1
            }
122 1
        }
123 24
    }
124
}
125