Theme   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 129
rs 10
c 0
b 0
f 0
wmc 19

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getAvailableLocales() 0 3 1
A getName() 0 3 1
A __construct() 0 9 4
A getAvailableTemplateStacks() 0 19 5
A getData() 0 24 6
A getPath() 0 3 1
A findLocale() 0 3 1
1
<?php
2
/**
3
 * Theme class file
4
 *
5
 * @package EBloodBank
6
 * @since   1.3
7
 */
8
namespace EBloodBank;
9
10
use DirectoryIterator;
11
use InvalidArgumentException;
12
use Symfony\Component\Yaml\Yaml;
13
14
/**
15
 * Theme class
16
 *
17
 * @since 1.3
18
 */
19
class Theme
20
{
21
    /**
22
     * @var string
23
     * @since 1.3
24
     */
25
    protected $name;
26
27
    /**
28
     * @var string
29
     * @since 1.3
30
     */
31
    protected $path;
32
33
    /**
34
     * @return void
35
     * @since 1.3
36
     */
37
    public function __construct($name, $path)
38
    {
39
        $this->name = $name;
40
        if (empty($this->name)) {
41
            throw new InvalidArgumentException(__('Invalid theme name.'));
42
        }
43
        $this->path = trimTrailingSlash($path);
44
        if (empty($this->path) || ! is_dir($this->path)) {
45
            throw new InvalidArgumentException(__('Invalid theme directory path.'));
46
        }
47
    }
48
49
    /**
50
     * Get the theme name.
51
     *
52
     * @return string
53
     * @since 1.3
54
     */
55
    public function getName()
56
    {
57
        return $this->name;
58
    }
59
60
    /**
61
     * Get the theme path.
62
     *
63
     * @return string
64
     * @since 1.3
65
     */
66
    public function getPath()
67
    {
68
        return $this->path;
69
    }
70
71
    /**
72
     * Find a locale in the theme.
73
     *
74
     * @return Locale|null
75
     * @since 1.3
76
     */
77
    public function findLocale($code)
78
    {
79
        return Locales::findLocale($code, $this->getPath() . '/locales');
80
    }
81
82
    /**
83
     * Get the available locales in the theme.
84
     *
85
     * @return Locale[]
86
     * @since 1.3
87
     */
88
    public function getAvailableLocales()
89
    {
90
        return Locales::getAvailableLocales($this->getPath() . '/locales');
91
    }
92
93
    /**
94
     * @return mixed
95
     * @since 1.3
96
     */
97
    public function getData($key = '', $default = '')
98
    {
99
        static $data;
100
101
        if (is_null($data)) {
102
            $data = [];
103
            $dataFile = $this->getPath() . '/theme.yml';
104
            if (file_exists($dataFile)) {
105
                $contents = file_get_contents($dataFile);
106
                if ($contents !== false) {
107
                    $data = Yaml::parse($contents, true);
108
                }
109
            }
110
        }
111
112
        if (! empty($key)) {
113
            if (isset($data[$key])) {
114
                return $data[$key];
115
            } else {
116
                return $default;
117
            }
118
        }
119
120
        return $data;
121
    }
122
123
    /**
124
     * Get the available templates stacks in the theme.
125
     *
126
     * @return array
127
     * @since 1.3
128
     */
129
    public function getAvailableTemplateStacks()
130
    {
131
        static $stacks;
132
133
        if (is_null($stacks)) {
134
            $stacks = [];
135
            $stacksItr = new DirectoryIterator($this->getPath() . '/templates');
136
137
            foreach ($stacksItr as $stackDir) {
138
                if ($stackDir->isDot() || ! $stackDir->isDir()) {
139
                    continue;
140
                }
141
                $stacks[] = trimTrailingSlash($stackDir->getRealPath());
142
            }
143
144
            arsort($stacks); // Sort the stacks, high priority stack at first.
145
        }
146
147
        return $stacks;
148
    }
149
}
150