Passed
Push — master ( 1fc558...6e3b51 )
by Peter
04:29
created

Themes::addThemeViewPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 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
    public function setTheme($request)
18
    {
19
        $this->findMatch($request);
20
        $this->addThemeViewPath();
21
        $this->applyThemeConfig();
22
    }
23
24
    /**
25
     * Getter for the theme that is chosen.
26
     *
27
     * @return string theme
28
     */
29
    public function getTheme()
30
    {
31
        return $this->theme;
32
    }
33
34
    /**
35
     * Iterate over the match criteria until a successful match is achieved.
36
     *
37
     * @return void
38
     */
39
    protected function findMatch($request)
40
    {
41
        foreach (app('config')->get('themes') as $match)
42
        {
43
            if ($this->testMatch($request, $match)) {
44
                break;
45
            }
46
        }
47
    }
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
    protected function testMatch($request, $match)
55
    {
56
        if (isset($match['match'])) {
57
            $rules = $this->explodeRules($match['match']);
58
59
            foreach ($rules as $rule) {
60
                if (!$this->handleRule($request, $rule)) {
61
                    return false;
62
                }
63
            }
64
        }
65
66
        // nothing to match or all matches succeeded
67
        $this->theme = $match['theme'];
68
        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
    protected function explodeRules($rules)
77
    {
78
        $rules = preg_replace('/\s+/', '', $rules);
79
        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
    protected function handleRule($request, $rule)
88
    {
89
        $rule = explode(':', $rule);
90
        $params = isset($rule[1]) ? $rule[1] : null;
91
        $class = '\\PeterColes\\Themes\\Matchers\\'.studly_case($rule[0]);
92
93
        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
    protected function addThemeViewPath()
102
    {
103
        if ($this->theme) {
104
            app('view.finder')->addLocation(themes_path($this->theme.'/views'));
105
        }
106
    }
107
108
    /**
109
     * Override configs with theme-specific settings.
110
     *
111
     * @return void
112
     */
113
    protected function applyThemeConfig()
114
    {
115
        $path = themes_path($this->theme.'/config');
116
117
        if (app('files')->exists($path)) {
118
            foreach (app('files')->allFiles($path) as $file) {
119
                $key = str_replace('.'.$file->getExtension(), '', $file->getFilename());
120
                app('config')->set($key, require $file->getPathname());
121
            }
122
        }
123
    }
124
}
125