Passed
Push — master ( 3d316c...cbe4a6 )
by Sergey
07:19 queued 10s
created

Themes::getThemesCacheID()   B

Complexity

Conditions 7
Paths 2

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 7
eloc 12
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 25
rs 8.8333
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Flextype (http://flextype.org)
7
 * Founded by Sergey Romanenko and maintained by Flextype Community.
8
 */
9
10
namespace Flextype;
11
12
use Flextype\Component\Filesystem\Filesystem;
0 ignored issues
show
Bug introduced by
The type Flextype\Component\Filesystem\Filesystem 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...
13
use RuntimeException;
14
use function array_merge;
15
use function array_replace_recursive;
16
use function count;
17
use function filemtime;
18
use function is_array;
19
use function md5;
20
21
class Themes
22
{
23
    /**
24
     * Flextype Dependency Container
25
     */
26
    private $flextype;
27
28
    /**
29
     * Private construct method to enforce singleton behavior.
30
     *
31
     * @access private
32
     */
33
    public function __construct($flextype)
34
    {
35
        $this->flextype = $flextype;
36
    }
37
38
    /**
39
     * Init themes
40
     */
41
    public function init($flextype, $app) : void
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

41
    public function init($flextype, /** @scrutinizer ignore-unused */ $app) : void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $flextype is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

41
    public function init(/** @scrutinizer ignore-unused */ $flextype, $app) : void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
42
    {
43
        // Set empty themes list item
44
        $this->flextype['registry']->set('themes', []);
45
46
        // Get themes list
47
        $themes_list = $this->getThemes();
48
49
        // If Themes List isnt empty then continue
50
        if (! is_array($themes_list) || count($themes_list) <= 0) {
0 ignored issues
show
introduced by
The condition is_array($themes_list) is always true.
Loading history...
51
            return;
52
        }
53
54
        // Get themes cache ID
55
        $themes_cache_id = $this->getThemesCacheID($themes_list);
56
57
        // Get themes list from cache or scan themes folder and create new themes cache item in the registry
58
        if ($this->flextype['cache']->contains($themes_cache_id)) {
59
            $this->flextype['registry']->set('themes', $this->flextype['cache']->fetch($themes_cache_id));
60
        } else {
61
            $themes                 = [];
62
            $themes_settings        = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $themes_settings is dead and can be removed.
Loading history...
63
            $themes_manifest        = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $themes_manifest is dead and can be removed.
Loading history...
64
            $default_theme_settings = [];
65
            $site_theme_settings    = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $site_theme_settings is dead and can be removed.
Loading history...
66
            $default_theme_manifest = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $default_theme_manifest is dead and can be removed.
Loading history...
67
68
            // Go through the themes list...
69
            foreach ($themes_list as $theme) {
70
71
                // Set custom theme directory
72
                $custom_theme_settings_dir = PATH['project'] . '/config/themes/' . $theme['dirname'];
0 ignored issues
show
Bug introduced by
The constant Flextype\PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
73
74
                // Set default theme settings and manifest files
75
                $default_theme_settings_file = PATH['project'] . '/themes/' . $theme['dirname'] . '/settings.yaml';
76
                $default_theme_manifest_file = PATH['project'] . '/themes/' . $theme['dirname'] . '/theme.yaml';
77
78
                // Set custom theme settings and manifest files
79
                $custom_theme_settings_file = PATH['project'] . '/config/themes/' . $theme['dirname'] . '/settings.yaml';
80
81
                // Create custom theme settings directory
82
                ! Filesystem::has($custom_theme_settings_dir)  and Filesystem::createDir($custom_theme_settings_dir);
83
84
                // Check if default theme settings file exists
85
                if (! Filesystem::has($default_theme_settings_file)) {
86
                    throw new RuntimeException('Load ' . $theme['dirname'] . ' theme settings - failed!');
87
                }
88
89
                // Get default theme manifest content
90
                $default_theme_settings_file_content = Filesystem::read($default_theme_settings_file);
91
                if (trim($default_theme_settings_file_content) === '') {
92
                    $default_theme_settings = [];
93
                } else {
94
                    $default_theme_settings = $this->flextype['serializer']->decode($default_theme_settings_file_content, 'yaml');
95
                }
96
97
                // Create custom theme settings file
98
                ! Filesystem::has($custom_theme_settings_file) and Filesystem::write($custom_theme_settings_file, $default_theme_settings_file_content);
99
100
                // Get custom theme settings content
101
                $custom_theme_settings_file_content = Filesystem::read($custom_theme_settings_file);
102
                if (trim($custom_theme_settings_file_content) === '') {
103
                    $custom_theme_settings = [];
104
                } else {
105
                    $custom_theme_settings = $this->flextype['serializer']->decode($custom_theme_settings_file_content, 'yaml');
106
                }
107
108
                // Check if default theme manifest file exists
109
                if (! Filesystem::has($default_theme_manifest_file)) {
110
                    RuntimeException('Load ' . $theme['dirname'] . ' theme manifest - failed!');
0 ignored issues
show
Bug introduced by
The function RuntimeException was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

110
                    /** @scrutinizer ignore-call */ 
111
                    RuntimeException('Load ' . $theme['dirname'] . ' theme manifest - failed!');
Loading history...
111
                }
112
113
                // Get default theme manifest content
114
                $default_theme_manifest_file_content = Filesystem::read($default_theme_manifest_file);
115
                $default_theme_manifest              = $this->flextype['serializer']->decode($default_theme_manifest_file_content, 'yaml');
116
117
                // Merge theme settings and manifest data
118
                $themes[$theme['dirname']]['manifest'] = $default_theme_manifest;
119
                $themes[$theme['dirname']]['settings'] = array_replace_recursive($default_theme_settings, $custom_theme_settings);
120
121
            }
122
123
            // Save parsed themes list in the registry themes
124
            $this->flextype['registry']->set('themes', $themes);
125
126
            // Save parsed themes list in the cache
127
            $this->flextype['cache']->save($themes_cache_id, $themes);
128
        }
129
130
        // Emit onThemesInitialized
131
        $this->flextype['emitter']->emit('onThemesInitialized');
132
    }
133
134
    /**
135
     * Get Themes Cache ID
136
     *
137
     * @param  array $themes_list Themes list
138
     *
139
     * @access protected
140
     */
141
    private function getThemesCacheID(array $themes_list) : string
142
    {
143
        // Themes Cache ID
144
        $_themes_cache_id = '';
145
146
        // Go through...
147
        if (is_array($themes_list) && count($themes_list) > 0) {
148
            foreach ($themes_list as $theme) {
149
                $default_theme_settings_file = PATH['project'] . '/themes/' . $theme['dirname'] . '/settings.yaml';
0 ignored issues
show
Bug introduced by
The constant Flextype\PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
150
                $default_theme_manifest_file = PATH['project'] . '/themes/' . $theme['dirname'] . '/theme.yaml';
151
                $site_theme_settings_file    = PATH['project'] . '/config/themes/' . $theme['dirname'] . '/settings.yaml';
152
153
                $f1 = Filesystem::has($default_theme_settings_file) ? filemtime($default_theme_settings_file) : '';
154
                $f2 = Filesystem::has($default_theme_manifest_file) ? filemtime($default_theme_manifest_file) : '';
155
                $f3 = Filesystem::has($site_theme_settings_file) ? filemtime($site_theme_settings_file) : '';
156
157
                $_themes_cache_id .= $f1 . $f2 . $f3;
158
            }
159
        }
160
161
        // Create Unique Cache ID for Themes
162
        $themes_cache_id = md5('themes' . PATH['project'] . '/themes/' . $_themes_cache_id);
163
164
        // Return themes cache id
165
        return $themes_cache_id;
166
    }
167
168
    /**
169
     * Get list of themes
170
     *
171
     * @return array
172
     *
173
     * @access public
174
     */
175
    public function getThemes() : array
176
    {
177
        // Init themes list
178
        $themes_list = [];
179
180
        // Get themes list
181
        $_themes_list = Filesystem::listContents(PATH['project'] . '/themes');
0 ignored issues
show
Bug introduced by
The constant Flextype\PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
182
183
        // Go through founded themes
184
        foreach ($_themes_list as $theme) {
185
            if ($theme['type'] !== 'dir' || ! Filesystem::has($theme['path'] . '/' . 'theme.yaml')) {
186
                continue;
187
            }
188
189
            $themes_list[] = $theme;
190
        }
191
192
        return $themes_list;
193
    }
194
195
    /**
196
     * Get partials for theme
197
     *
198
     * @param string $theme Theme id
199
     *
200
     * @return array
201
     *
202
     * @access public
203
     */
204
    public function getPartials(string $theme) : array
205
    {
206
        // Init partials list
207
        $partials_list = [];
208
209
        // Get partials files
210
        $_partials_list = Filesystem::listContents(PATH['project'] . '/themes/' . $theme . '/templates/partials/');
0 ignored issues
show
Bug introduced by
The constant Flextype\PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
211
212
        // If there is any partials file then go...
213
        if (count($_partials_list) > 0) {
214
            foreach ($_partials_list as $partial) {
215
                if ($partial['type'] !== 'file' || $partial['extension'] !== 'html') {
216
                    continue;
217
                }
218
219
                $partials_list[] = $partial;
220
            }
221
        }
222
223
        // return partials
224
        return $partials_list;
225
    }
226
227
    /**
228
     * Get templates for theme
229
     *
230
     * @param string $theme Theme id
231
     *
232
     * @return array
233
     *
234
     * @access public
235
     */
236
    public function getTemplates(string $theme) : array
237
    {
238
        // Init templates list
239
        $templates_list = [];
240
241
        // Get templates files
242
        $_templates_list = Filesystem::listContents(PATH['project'] . '/themes/' . $theme . '/templates/');
0 ignored issues
show
Bug introduced by
The constant Flextype\PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
243
244
        // If there is any template file then go...
245
        if (count($_templates_list) > 0) {
246
            foreach ($_templates_list as $template) {
247
                if ($template['type'] !== 'file' || $template['extension'] !== 'html') {
248
                    continue;
249
                }
250
251
                $templates_list[] = $template;
252
            }
253
        }
254
255
        // return templates
256
        return $templates_list;
257
    }
258
}
259