Template::getSetting()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 2
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Chuckbe\Chuckcms\Chuck\Accessors;
4
5
use Chuckbe\Chuckcms\Models\Template as TemplateModel;
6
7
class Template
8
{
9
    private $template;
10
    private $templateSettings;
11
12
    public function __construct($template = null)
13
    {
14
        $this->template = $this->getCurrentTemplate($template);
15
        $this->templateSettings = $this->getCurrentSettings($this->template);
16
    }
17
18
    public static function forTemplate(string $template)
19
    {
20
        return new static($template);
21
    }
22
23
    private function getCurrentSettings(TemplateModel $template_model)
24
    {
25
        return $template_model->json;
26
    }
27
28
    private function getCurrentTemplate($template_slug)
29
    {
30
        if (is_null($template_slug)) {
31
            $template = TemplateModel::where('active', true)->where('type', 'default')->first();
32
        } else {
33
            $template = TemplateModel::where('slug', $template_slug)->first();
34
        }
35
36
        if (is_null($template)) {
37
            throw new Exception('Whoops! No Template Defined...');
0 ignored issues
show
Bug introduced by
The type Chuckbe\Chuckcms\Chuck\Accessors\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
38
        }
39
40
        return $template;
41
    }
42
43
    public function getSetting($var)
44
    {
45
        $setting = $this->resolveSetting($var, $this->templateSettings);
46
47
        return !is_null($setting) ? $setting : null;
48
    }
49
50
    private function resolveSetting($var, $settings)
51
    {
52
        if (array_key_exists($var, $settings)) {
53
            $setting = $settings[$var]['value'];
54
        } else {
55
            return null;
56
        }
57
58
        return $setting;
59
    }
60
}
61