Passed
Push — master ( a5e8db...945884 )
by Danny
02:33
created

Theme   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 80
rs 10
c 0
b 0
f 0
wmc 11
lcom 0
cbo 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A themeDeleteDefaults() 0 11 2
A themeDelete() 0 4 1
A themeActivate() 0 4 1
C themeInstall() 0 37 7
1
<?php
2
namespace PressCLI\WPCLI;
3
4
use PressCLI\WPCLI\CLI;
5
use PressCLI\ThemeInstall\Zip;
6
use PressCLI\Option\Configuration;
7
use PressCLI\ThemeInstall\StyleCSS;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
trait Theme
11
{
12
    /**
13
     * Deletes all unused default themes.
14
     */
15
    public static function themeDeleteDefaults()
16
    {
17
        $themes = [
18
            'twentyfifteen',
19
            'twentyfourteen',
20
        ];
21
22
        foreach ($themes as $theme) {
23
            self::themeDelete($theme);
24
        }
25
    }
26
27
    /**
28
     * Deletes a theme.
29
     *
30
     * @param  string $theme Theme name to delete.
31
     */
32
    public static function themeDelete($theme)
33
    {
34
        CLI::execCommand('theme', ['delete', $theme]);
35
    }
36
37
    /**
38
     * Activates a theme.
39
     *
40
     * @param  string $theme Theme name to activate.
41
     */
42
    protected static function themeActivate($theme)
43
    {
44
        CLI::execCommand('theme', ['activate', $theme]);
45
    }
46
47
    /**
48
     * Installs a theme.
49
     *
50
     * @param  OutputInterface $output
51
     */
52
    public static function themeInstall(OutputInterface $output)
53
    {
54
        $config = Configuration::get();
55
        $url = isset($config['theme']['url']) ? $config['theme']['url'] : '';
56
        $name = isset($config['theme']['name']) ? $config['theme']['name'] : '';
57
        $directory = getcwd() . '/wp-content/themes';
58
59
        // Make sure we have the required configuration.
60
        if (!$url || !$name) {
61
            return;
62
        }
63
64
        // Make sure the theme does not already exist.
65
        if (file_exists("{$directory}/{$name}")) {
66
            $output->writeln("Warning: Theme already exists in {$directory}/{$name}.");
67
68
            // Activate theme.
69
            self::themeActivate($name);
70
71
            return;
72
        }
73
74
        // Download the theme to the themes directory.
75
        $theme = Zip::execute($url, $directory, $name);
76
        if (!$theme) {
77
            $output->writeln("<error>Error: Theme could not be downloaded from {$url}!</error>");
78
            return;
79
        }
80
81
        $output->writeln("Success: Theme successfully installed!");
82
83
        // Set style.css
84
        StyleCSS::set();
85
86
        // Activate theme.
87
        self::themeActivate($name);
88
    }
89
}
90