Completed
Push — master ( 07eadb...49cf5c )
by Bart
04:28
created

Plugins::getUpdatesService()   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 3
Bugs 0 Features 0
Metric Value
c 3
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
        if ($this->getUpdatesService()->isCraftDbMigrationNeeded()) {
135
            $result = $this->getUpdatesService()->updateDatabase('craft');
136
            if (!$result['success']) {
137
                throw new Exception($result['message']);
138
            }
139
        }
140
141 5
        Craft::log(Craft::t('Importing Plugins'));
142 5
        foreach ($pluginDefinitions as $handle => $pluginDefinition) {
143 5
            Craft::log(Craft::t('Applying definitions for {handle}', ['handle' => $handle]));
144
145 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...
146 4
                if ($pluginDefinition['isInstalled']) {
147 3
                    $isNewPlugin = !$plugin->isInstalled;
148 3
                    if ($isNewPlugin) {
149 3
                        $this->installPluginByHandle($handle);
150 3
                    }
151
152 3
                    $this->togglePluginByHandle($handle, $pluginDefinition['isEnabled']);
153
154 3
                    if (!$isNewPlugin && $plugin->isEnabled) {
155
                        $this->runMigrations($handle);
156
                    }
157
158 3
                    if (array_key_exists('settings', $pluginDefinition)) {
159 3
                        Craft::log(Craft::t('Saving plugin settings for {handle}', ['handle' => $handle]));
160
161 3
                        $this->getPluginService()->savePluginSettings($plugin, $pluginDefinition['settings']);
162 3
                    }
163 3
                } else {
164 1
                    $this->uninstallPluginByHandle($handle);
165
                }
166 4
            }
167 5
        }
168
169 5
        return $this->getResultModel();
170
    }
171
172
    /**
173
     * @param array $data
174
     *
175
     * @return array
176
     */
177 1
    public function export(array $data = [])
178
    {
179 1
        Craft::log(Craft::t('Exporting Plugins'));
180
181 1
        $plugins = $this->getPluginService()->getPlugins(false);
182 1
        $pluginDefinitions = [];
183
184 1
        foreach ($plugins as $plugin) {
185 1
            $handle = preg_replace('/^Craft\\\\(.*?)Plugin$/', '$1', get_class($plugin));
186 1
            $pluginDefinitions[$handle] = $this->getPluginDefinition($plugin);
187 1
        }
188 1
        ksort($pluginDefinitions);
189
190 1
        return $pluginDefinitions;
191
    }
192
}
193