Completed
Pull Request — master (#55)
by Bart
04:15
created

Plugins::getMigrationsService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
rs 10
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Services;
4
5
use Craft\Craft;
6
use Craft\Exception;
7
use Craft\BasePlugin;
8
9
/**
10
 * Schematic Plugins Service.
11
 *
12
 * Sync Craft Setups.
13
 *
14
 * @author    Nerds & Company
15
 * @copyright Copyright (c) 2015-2016, Nerds & Company
16
 * @license   MIT
17
 *
18
 * @link      http://www.nerds.company
19
 */
20
class Plugins extends Base
21
{
22
    /**
23
     * @return PluginsService
24
     */
25 6
    protected function getPluginService()
26
    {
27 6
        return Craft::app()->plugins;
28
    }
29
30
    /**
31
     * @return UpdatesService
32
     */
33 5
    protected function getUpdatesService()
34
    {
35 5
        return Craft::app()->updates;
36
    }
37
38
    /**
39
     * Installs plugin by handle.
40
     *
41
     * @param string $handle
42
     */
43 3
    protected function installPluginByHandle($handle)
44
    {
45 3
        Craft::log(Craft::t('Installing plugin {handle}', ['handle' => $handle]));
46
47
        try {
48 3
            $this->getPluginService()->installPlugin($handle);
49 3
        } catch (\Exception $e) {
50 1
            $this->addError($e->getMessage());
51
        }
52 3
    }
53
54
    /**
55
     * Uninstalls plugin by handle.
56
     *
57
     * @param $handle
58
     */
59 1
    protected function uninstallPluginByHandle($handle)
60
    {
61 1
        $this->getPluginService()->uninstallPlugin($handle);
62 1
    }
63
64
    /**
65
     * Returns plugin by handle.
66
     *
67
     * @param string $handle
68
     *
69
     * @return BasePlugin|null
70
     */
71 5
    protected function getPlugin($handle)
72
    {
73 5
        $plugin = $this->getPluginService()->getPlugin($handle, false);
74 5
        if (!$plugin) {
75 1
            $this->addError(Craft::t("Plugin {handle} could not be found, make sure it's files are located in the plugins folder", ['handle' => $handle]));
76 1
        }
77
78 5
        return $plugin;
79
    }
80
81
    /**
82
     * Toggles plugin based on enabled flag.
83
     *
84
     * @param string $handle
85
     * @param bool   $isEnabled
86
     */
87 3
    protected function togglePluginByHandle($handle, $isEnabled)
88
    {
89 3
        if ($isEnabled) {
90 2
            $this->getPluginService()->enablePlugin($handle);
91 2
        } else {
92 1
            $this->getPluginService()->disablePlugin($handle);
93
        }
94 3
    }
95
96
    /**
97
     * Run plugin migrations automatically.
98
     *
99
     * @param string $handle
100
     *
101
     * @throws Exception
102
     */
103
    protected function runMigrations($handle)
104
    {
105
        $result = $this->getUpdatesService()->updateDatabase($handle);
106
        if (!$result['success']) {
107
            throw new Exception($result['message']);
108
        }
109
    }
110
111
    /**
112
     * @param BasePlugin $plugin
113
     *
114
     * @return array
115
     */
116 1
    private function getPluginDefinition(BasePlugin $plugin)
117
    {
118
        return [
119 1
            'isInstalled'       => $plugin->isInstalled,
120 1
            'isEnabled'         => $plugin->isEnabled,
121 1
            'settings'          => $plugin->getSettings()->attributes,
122 1
        ];
123
    }
124
125
    /**
126
     * @param array $pluginDefinitions
127
     * @param bool  $force
128
     *
129
     * @return Result
130
     */
131 5
    public function import(array $pluginDefinitions, $force = false)
132
    {
133 5
        Craft::log(Craft::t('Updating Craft'));
134 5
        $result = $this->getUpdatesService()->updateDatabase('craft');
135 5
        if (!$result['success']) {
136
            throw new Exception($result['message']);
137
        }
138
139 5
        Craft::log(Craft::t('Importing Plugins'));
140 5
        foreach ($pluginDefinitions as $handle => $pluginDefinition) {
141 5
            Craft::log(Craft::t('Applying definitions for {handle}', ['handle' => $handle]));
142
143 5
            if ($plugin = $this->getPlugin($handle)) {
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $plugin is correct as $this->getPlugin($handle) (which targets NerdsAndCompany\Schemati...es\Plugins::getPlugin()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
144 4
                if ($pluginDefinition['isInstalled']) {
145 3
                    $isNewPlugin = !$plugin->isInstalled;
146 3
                    if ($isNewPlugin) {
147 3
                        $this->installPluginByHandle($handle);
148 3
                    }
149
150 3
                    $this->togglePluginByHandle($handle, $pluginDefinition['isEnabled']);
151
152 3
                    if (!$isNewPlugin && $plugin->isEnabled) {
153
                        $this->runMigrations($handle);
154
                    }
155
156 3
                    if (array_key_exists('settings', $pluginDefinition)) {
157 3
                        Craft::log(Craft::t('Saving plugin settings for {handle}', ['handle' => $handle]));
158
159 3
                        $this->getPluginService()->savePluginSettings($plugin, $pluginDefinition['settings']);
160 3
                    }
161 3
                } else {
162 1
                    $this->uninstallPluginByHandle($handle);
163
                }
164 4
            }
165 5
        }
166
167 5
        return $this->getResultModel();
168
    }
169
170
    /**
171
     * @param array $data
172
     *
173
     * @return array
174
     */
175 1
    public function export(array $data = [])
176
    {
177 1
        Craft::log(Craft::t('Exporting Plugins'));
178
179 1
        $plugins = $this->getPluginService()->getPlugins(false);
180 1
        $pluginDefinitions = [];
181
182 1
        foreach ($plugins as $plugin) {
183 1
            $handle = preg_replace('/^Craft\\\\(.*?)Plugin$/', '$1', get_class($plugin));
184 1
            $pluginDefinitions[$handle] = $this->getPluginDefinition($plugin);
185 1
        }
186 1
        ksort($pluginDefinitions);
187
188 1
        return $pluginDefinitions;
189
    }
190
}
191