doSetup()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 17
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 17
loc 17
rs 9.7
c 0
b 0
f 0
1
<?php
2
/**
3
 * (c) shopware AG <[email protected]>
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
use Doctrine\Common\Collections\ArrayCollection;
8
use ShopwarePlugins\Connect\Bootstrap\SubscriberRegistration;
9
use ShopwarePlugins\Connect\Bootstrap\Uninstall;
10
use ShopwarePlugins\Connect\Bootstrap\Update;
11
use ShopwarePlugins\Connect\Bootstrap\Setup;
12
use ShopwarePlugins\Connect\Commands\ApiEndpointCommand;
13
use ShopwarePlugins\Connect\Commands\ImageImportCommand;
14
use ShopwarePlugins\Connect\Components\BasketHelper;
15
use ShopwarePlugins\Connect\Components\ConnectFactory;
16
use ShopwarePlugins\Connect\Components\Logger;
17
18
require_once __DIR__ . '/vendor/autoload.php';
19
20
/**
21
 * @category  Shopware
22
 * @package   Shopware\Plugins\SwagConnect
23
 */
24
final class Shopware_Plugins_Backend_SwagConnect_Bootstrap extends Shopware_Components_Plugin_Bootstrap
25
{
26
    /**
27
     * @var SubscriberRegistration
28
     */
29
    private $subscriberRegistration;
30
31
    /**
32
     * @var ConnectFactory
33
     */
34
    private $connectFactory;
35
36
    /**
37
     * Returns the current version of the plugin.
38
     *
39
     * @throws Exception
40
     * @return string|void
41
     */
42
    public function getVersion()
43
    {
44
        $info = json_decode(file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'plugin.json'), true);
45
46
        if ($info) {
47
            return $info['currentVersion'];
48
        }
49
        throw new \Exception('The plugin has an invalid version file.');
50
    }
51
52
    /**
53
     * Returns a nice name for plugin manager list
54
     *
55
     * @return string
56
     */
57
    public function getLabel()
58
    {
59
        return 'Shopware Connect';
60
    }
61
62
    /**
63
     * @return array
64
     */
65
    public function getInfo()
66
    {
67
        return [
68
            'version' => $this->getVersion(),
69
            'label' => $this->getLabel(),
70
            'description' => file_get_contents($this->Path() . 'info.txt'),
71
            'link' => 'http://www.shopware.de/',
72
        ];
73
    }
74
75
    /**
76
     * Install plugin method
77
     *
78
     * @throws \RuntimeException
79
     * @return array
80
     */
81
    public function install()
82
    {
83
        $this->doSetup();
84
85
        return ['success' => true, 'invalidateCache' => ['backend', 'config', 'template', 'theme', 'proxy']];
86
    }
87
88
    /**
89
     * @param $version string
90
     * @return array
91
     */
92
    public function update($version)
93
    {
94
        // sometimes plugin is not installed before
95
        // but could be updated. by this way setup process
96
        // is simple and only required structure will be created
97
        // e.g. DB and attributes
98
        $fullSetup = false;
99
        if ($this->isInstalled()) {
100
            $fullSetup = true;
101
        }
102
103
        $this->doSetup($fullSetup);
104
        $this->doUpdate($version);
105
106
        return ['success' => true, 'invalidateCache' => ['backend', 'config', 'template', 'theme', 'proxy']];
107
    }
108
109
    /**
110
     * Uninstall plugin method
111
     *
112
     * @return array
113
     */
114
    public function uninstall()
115
    {
116
        $this->doUninstall();
117
118
        return ['success' => true, 'invalidateCache' => ['backend', 'config', 'template', 'theme', 'proxy']];
119
    }
120
121
    /**
122
     * Performs the default setup of the system.
123
     *
124
     * This can be used by the update as well as by the install method
125
     *
126
     * @param bool $fullSetup
127
     * @throws RuntimeException
128
     */
129 View Code Duplication
    public function doSetup($fullSetup = true)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
130
    {
131
        $this->registerMyLibrary();
132
        $modelManager = Shopware()->Models();
133
        $setup = new Setup(
134
            $this,
135
            $modelManager,
136
            Shopware()->Db(),
137
            new \ShopwarePlugins\Connect\Bootstrap\Menu(
138
                $this,
139
                $this->getConfigComponents(),
140
                $modelManager,
141
                $this->assertMinimumVersion('5.2.6')
142
            )
143
        );
144
        $setup->run($fullSetup);
145
    }
146
147
    /**
148
     * Performs the update of the system
149
     *
150
     * @param $version
151
     * @return bool
152
     */
153
    public function doUpdate($version)
154
    {
155
        $this->registerMyLibrary();
156
157
        $update = new Update(
158
            $this,
159
            Shopware()->Models(),
160
            Shopware()->Db(),
161
            new Logger(Shopware()->Db()),
162
            $version
163
        );
164
165
        return $update->run();
166
    }
167
168
    /**
169
     * Uninstall the plugin
170
     */
171 View Code Duplication
    public function doUninstall()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
172
    {
173
        $this->registerMyLibrary();
174
        $modelManager = Shopware()->Models();
175
        $uninstall = new Uninstall(
176
            $this,
177
            $modelManager,
178
            Shopware()->Db(),
179
            new \ShopwarePlugins\Connect\Bootstrap\Menu(
180
                $this,
181
                $this->getConfigComponents(),
182
                $modelManager,
183
                $this->assertMinimumVersion('5.2.6')
184
            )
185
        );
186
187
        return $uninstall->run();
188
    }
189
190
    /**
191
     * Will dynamically register all needed events
192
     *
193
     * @param Enlight_Event_EventArgs $args
194
     */
195
    public function onStartDispatch(Enlight_Event_EventArgs $args)
196
    {
197
        $this->get('snippets')->addConfigDir($this->Path() . 'Snippets/');
198
        $this->registerMyLibrary();
199
        $this->registerSubscribers();
200
    }
201
202
    public function registerTemplateDir()
203
    {
204
        $this->get('template')->addTemplateDir($this->Path() . 'Views/', 'connect');
205
    }
206
207
    /**
208
     * @return ArrayCollection
209
     */
210
    public function onConsoleAddCommand()
211
    {
212
        $this->registerMyLibrary();
213
        $this->registerSubscribers();
214
215
        return new ArrayCollection([
216
            new ApiEndpointCommand(),
217
            new ImageImportCommand($this->getHelper())
218
        ]);
219
    }
220
221
    public function onInitResourceSDK()
222
    {
223
        $this->registerMyLibrary();
224
225
        return $this->getConnectFactory()->createSDK();
226
    }
227
228
    /**
229
     * Register additional namespaces for the libraries
230
     */
231
    public function registerMyLibrary()
232
    {
233
        $this->Application()->Loader()->registerNamespace(
234
            'ShopwarePlugins\\Connect',
235
            $this->Path()
236
        );
237
238
        $this->registerCustomModels();
239
    }
240
241
    /**
242
     * Lazy getter for the connectFactory
243
     *
244
     * @return ConnectFactory
245
     */
246
    public function getConnectFactory()
247
    {
248
        $this->registerMyLibrary();
249
250
        if (!$this->connectFactory) {
251
            $this->connectFactory = new ConnectFactory($this->getVersion());
252
        }
253
254
        return $this->connectFactory;
255
    }
256
257
    /**
258
     * @return Shopware\Connect\SDK
259
     */
260
    public function getSDK()
261
    {
262
        return $this->getConnectFactory()->getSDK();
263
    }
264
265
    /**
266
     * @return \ShopwarePlugins\Connect\Components\Helper
267
     */
268
    public function getHelper()
269
    {
270
        return $this->getConnectFactory()->getHelper();
271
    }
272
273
    /**
274
     * @return BasketHelper
275
     */
276
    public function getBasketHelper()
277
    {
278
        return $this->getConnectFactory()->getBasketHelper();
279
    }
280
281
    /**
282
     * @return \ShopwarePlugins\Connect\Components\Config
283
     */
284
    public function getConfigComponents()
285
    {
286
        return $this->getConnectFactory()->getConfigComponent();
287
    }
288
289
    /**
290
     * @return bool
291
     */
292
    private function isInstalled()
293
    {
294
        $builder = Shopware()->Models()->createQueryBuilder();
295
        $builder->select(['plugins'])
296
            ->from('Shopware\Models\Plugin\Plugin', 'plugins');
297
298
        $builder->where('plugins.label = :label');
299
        $builder->setParameter('label', $this->getLabel());
300
301
        $query = $builder->getQuery();
302
        $plugin = $query->getOneOrNullResult();
303
        /** @var $plugin Shopware\Models\Plugin\Plugin */
304
        if (!$plugin) {
305
            return false;
306
        }
307
308
        return (bool) $plugin->getInstalled();
309
    }
310
311
    private function registerSubscribers()
312
    {
313
        if (!$this->subscriberRegistration instanceof SubscriberRegistration) {
314
            $this->subscriberRegistration = new SubscriberRegistration(
315
                $this->getConfigComponents(),
316
                $this->get('models'),
317
                $this->get('db'),
318
                $this,
319
                $this->get('events'),
320
                $this->getSDK(),
321
                $this->getConnectFactory(),
322
                $this->getHelper(),
323
                $this->get('service_container')
324
            );
325
        }
326
327
        $this->subscriberRegistration->registerSubscribers();
328
    }
329
}
330