Completed
Pull Request — develop (#716)
by Serg
11:55
created

ManagerTheme::loadChunks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php namespace EvolutionCMS;
2
3
use EvolutionCMS\Interfaces\ManagerThemeInterface;
4
use EvolutionCMS\Interfaces\CoreInterface;
5
use Exception;
6
7
class ManagerTheme implements ManagerThemeInterface
8
{
9
    /**
10
     * @var CoreInterface
11
     */
12
    protected $core;
13
14
    /**
15
     * @var $theme
16
     */
17
    protected $theme;
18
19
    public function __construct(CoreInterface $core, $theme = '')
20
    {
21
        $this->core = $core;
22
23
        if (empty($theme)) {
24
            $theme = $this->core->getConfig('manager_theme');
25
        }
26
27
        $this->theme = $theme;
28
29
        $this->loadSnippets();
30
31
        $this->loadChunks();
32
    }
33
34
    public function loadSnippets()
35
    {
36
        $this->addSnippet('recentInfoList', $this->getSnippet('welcome/RecentInfo'));
37
    }
38
39
    public function loadChunks()
40
    {
41
        $this->addChunk('welcome/RecentInfo', $this->getChunk('welcome/RecentInfo'));
42
        $this->addChunk('welcome/StartUpScript', $this->getChunk('welcome/StartUpScript'));
43
        $this->addChunk('welcome/Widget', $this->getChunk('welcome/Widget'));
44
        $this->addChunk('welcome/WrapIcon', $this->getChunk('welcome/WrapIcon'));
45
    }
46
47
    protected function pathElement($type, $name, $ext)
48
    {
49
        return MODX_MANAGER_PATH . sprintf('media/style/%s/%s/%s.%s', $this->theme, $type, $name, $ext);
50
    }
51
52
    public function getElement($type, $name)
53
    {
54
        switch ($type) {
55
            case 'chunk':
56
                return file_get_contents($this->pathElement($type, $name, 'tpl'));
57
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
58
            case 'snippet':
59
                return file_get_contents($this->pathElement($type, $name, 'php'));
60
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
61
            default:
62
                throw new Exception;
63
        }
64
    }
65
66
    public function getSnippet($name)
67
    {
68
        return $this->getElement('snippets', $name);
69
    }
70
71
    public function getChunk($name)
72
    {
73
        return $this->getElement('chunks', $name);
74
    }
75
76
    public function addElement($name, $code, $type)
77
    {
78
        switch ($type) {
79
            case 'chunk':
80
                $this->addChunk($name, $code);
81
                break;
82
            case 'snippet':
83
                $this->addSnippet($name, $code);
84
                break;
85
            default:
86
                throw new Exception;
87
        }
88
    }
89
90
    public function addSnippet($name, $code)
91
    {
92
        $this->core->addSnippet(
93
            $name,
94
            $code,
95
            'manager',
96
            array(
97
                'managerTheme' => $this
98
            )
99
        );
100
    }
101
102
    public function addChunk($name, $code)
103
    {
104
        $this->core->addChunk($name, $code, 'manager');
105
    }
106
}
107