Passed
Push — master ( 9bd6cb...5aafeb )
by Karel
07:55
created

Template::getSetting()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
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 4
rs 10
1
<?php
2
3
namespace Chuckbe\Chuckcms\Chuck\Accessors;
4
5
use App\Http\Requests;
0 ignored issues
show
Bug introduced by
The type App\Http\Requests was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Chuckbe\Chuckcms\Models\Template as TemplateModel;
7
8
class Template
9
{
10
    private $template;
11
    private $templateSettings;
12
13
    public function __construct($template = null) 
14
    {
15
        $this->template = $this->getCurrentTemplate($template);
16
        $this->templateSettings = $this->getCurrentSettings($this->template);
17
    }
18
19
    public static function forTemplate(string $template)
20
    {
21
        return new static($template);
22
    }
23
24
    private function getCurrentSettings(TemplateModel $template_model)
25
    {
26
        return $template_model->json;
27
    }
28
29
    private function getCurrentTemplate($template_slug)
30
    {
31
        if (is_null($template_slug)) {
32
            $template = TemplateModel::where('active', true)->where('type', 'default')->first();
33
        } else {
34
            $template = TemplateModel::where('slug', $template_slug)->first();
35
        }
36
37
        if (is_null($template)) {
38
            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...
39
        }
40
41
        return $template;
42
    }
43
44
    public function getSetting($var)
45
    {
46
        $setting = $this->resolveSetting($var, $this->templateSettings);
47
        return $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
}
62