StyleCSS::renderTemplate()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace PressCLI\ThemeInstall;
3
4
use PressCLI\Option\Configuration;
5
6
class StyleCSS
7
{
8
    /**
9
     * Sets the style.css from the template.style.css.
10
     */
11
    public static function set()
12
    {
13
        $config = Configuration::get();
14
        $name = isset($config['theme']['name']) ? $config['theme']['name'] : '';
15
        $themePath = getcwd() . "/wp-content/themes/{$name}";
16
17
        // Make sure we have the required configuration.
18
        if (!$name) {
19
            return;
20
        }
21
22
        // Render template.
23
        $template = self::renderTemplate($themePath);
24
25
        // Write rendered template to style.css
26
        self::write($template, $themePath);
27
28
        // Delete template
29
        self::deleteTemplate($themePath);
30
    }
31
32
    /**
33
     * Renders the template from the configuration.
34
     *
35
     * @param  string $themePath The path to the theme.
36
     *
37
     * @return string            The rendered template on success and empty string on failure.
38
     */
39
    protected static function renderTemplate($themePath)
40
    {
41
        $template = self::getTemplate($themePath);
42
        if (!$template) {
43
            return '';
44
        }
45
46
        $config = Configuration::get();
47
        foreach ($config['theme']['style-css'] as $key => $value) {
48
            $template = str_replace('{' . $key . '}', $value, $template);
49
        }
50
51
        return $template;
52
    }
53
54
    /**
55
     * Gets the template.
56
     *
57
     * @param  string $themePath The path to the theme.
58
     */
59
    protected static function getTemplate($themePath)
60
    {
61
        $templatePath = "{$themePath}/template.style.css";
62
        if (!file_exists($templatePath)) {
63
            return '';
64
        }
65
66
        return file_get_contents($templatePath);
67
    }
68
69
    /**
70
     * Deletes the template.
71
     *
72
     * @param  string $themePath The path to the theme.
73
     */
74
    protected static function deleteTemplate($themePath)
75
    {
76
        $templatePath = "{$themePath}/template.style.css";
77
        if (file_exists($templatePath)) {
78
            unlink($templatePath);
79
        }
80
    }
81
82
    /**
83
     * Writes the template to style.css.
84
     *
85
     * @param  string $template  Template content to write.
86
     * @param  string $themePath The path to the theme.
87
     */
88
    protected static function write($template, $themePath)
89
    {
90
        // If the template is empty then we are done here.
91
        if (!$template) {
92
            return;
93
        }
94
95
        file_put_contents("{$themePath}/style.css", $template);
96
    }
97
}
98