Completed
Pull Request — master (#324)
by Simon
04:20
created

Shopware_Plugins_Backend_SwagConnect_Bootstrap   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 311
Duplicated Lines 10.93 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 27
lcom 1
cbo 7
dl 34
loc 311
rs 10
c 0
b 0
f 0

22 Methods

Rating   Name   Duplication   Size   Complexity  
A getVersion() 0 10 2
A getLabel() 0 4 1
A getInfo() 0 9 1
A install() 0 6 1
A update() 0 16 2
A uninstall() 0 6 1
A doSetup() 17 17 1
A doUpdate() 0 12 1
A doUninstall() 17 17 1
A onStartDispatch() 0 5 1
A onConsoleAddCommand() 0 9 1
A onInitResourceSDK() 0 6 1
A registerMyLibrary() 0 17 1
A getConnectFactory() 0 10 2
A getSDK() 0 4 1
A getHelper() 0 4 1
A getBasketHelper() 0 4 1
A getConfigComponents() 0 4 1
A getMarketplaceGateway() 0 4 1
A getMarketplaceApplier() 0 4 1
A isInstalled() 0 18 2
A registerSubscribers() 0 17 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Shopware 5.2
4
 * Copyright © 2016 shopware AG
5
 *
6
 * According to our dual licensing model, this program can be used either
7
 * under the terms of the GNU Affero General Public License, version 3,
8
 * or under a proprietary license.
9
 *
10
 * The texts of the GNU Affero General Public License with an additional
11
 * permission and of our proprietary license can be found at and
12
 * in the LICENSE file you have received along with this program.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * "Shopware" is a registered trademark of shopware AG.
20
 * The licensing of the program under the AGPLv3 does not imply a
21
 * trademark license. Therefore any rights, title and interest in
22
 * our trademarks remain entirely with us.
23
 */
24
25
use Doctrine\Common\Collections\ArrayCollection;
26
use ShopwarePlugins\Connect\Bootstrap\SubscriberRegistration;
27
use ShopwarePlugins\Connect\Bootstrap\Uninstall;
28
use ShopwarePlugins\Connect\Bootstrap\Update;
29
use ShopwarePlugins\Connect\Bootstrap\Setup;
30
use ShopwarePlugins\Connect\Commands\ApiEndpointCommand;
31
use ShopwarePlugins\Connect\Components\ConnectFactory;
32
33
/**
34
 * @category  Shopware
35
 * @package   Shopware\Plugins\SwagConnect
36
 */
37
final class Shopware_Plugins_Backend_SwagConnect_Bootstrap extends Shopware_Components_Plugin_Bootstrap
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
38
{
39
    /**
40
     * @var SubscriberRegistration
41
     */
42
    private $subscriberRegistration;
43
44
    /**
45
     * @var ConnectFactory
46
     */
47
    private $connectFactory;
48
49
    /**
50
     * Returns the current version of the plugin.
51
     *
52
     * @return string|void
53
     * @throws Exception
54
     */
55
    public function getVersion()
56
    {
57
        $info = json_decode(file_get_contents(__DIR__ . DIRECTORY_SEPARATOR .'plugin.json'), true);
58
59
        if ($info) {
60
            return $info['currentVersion'];
61
        } else {
62
            throw new \Exception('The plugin has an invalid version file.');
63
        }
64
    }
65
66
    /**
67
     * Returns a nice name for plugin manager list
68
     *
69
     * @return string
70
     */
71
    public function getLabel()
72
    {
73
        return 'Shopware Connect';
74
    }
75
76
    /**
77
     * @return array
78
     */
79
    public function getInfo()
80
    {
81
        return array(
82
            'version' => $this->getVersion(),
83
            'label' => $this->getLabel(),
84
            'description' => file_get_contents($this->Path() . 'info.txt'),
85
            'link' => 'http://www.shopware.de/',
86
        );
87
    }
88
89
    /**
90
     * Install plugin method
91
     *
92
     * @throws \RuntimeException
93
     * @return bool
94
     */
95
    public function install()
96
    {
97
        $this->doSetup();
98
99
        return array('success' => true, 'invalidateCache' => array('backend', 'config'));
100
    }
101
102
    /**
103
     * @param $version string
104
     * @return array
105
     */
106
    public function update($version)
107
    {
108
        // sometimes plugin is not installed before
109
        // but could be updated. by this way setup process
110
        // is simple and only required structure will be created
111
        // e.g. DB and attributes
112
        $fullSetup = false;
113
        if ($this->isInstalled()) {
114
            $fullSetup = true;
115
        }
116
117
        $this->doSetup($fullSetup);
118
        $this->doUpdate($version);
119
120
        return ['success' => true, 'invalidateCache' => ['backend', 'config', 'template', 'theme']];
121
    }
122
123
    /**
124
     * Uninstall plugin method
125
     *
126
     * @return bool
127
     */
128
    public function uninstall()
129
    {
130
        $this->doUninstall();
131
132
        return true;
133
    }
134
135
    /**
136
     * Performs the default setup of the system.
137
     *
138
     * This can be used by the update as well as by the install method
139
     *
140
     * @param bool $fullSetup
141
     * @throws RuntimeException
142
     */
143 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...
144
    {
145
        $this->registerMyLibrary();
146
        $modelManager = Shopware()->Models();
147
        $setup = new Setup(
148
            $this,
149
            $modelManager,
150
            Shopware()->Db(),
151
            new \ShopwarePlugins\Connect\Bootstrap\Menu(
152
                $this,
153
                $this->getConfigComponents(),
154
                $modelManager,
155
                $this->assertMinimumVersion('5.2.6')
156
            )
157
        );
158
        $setup->run($fullSetup);
159
    }
160
161
    /**
162
     * Performs the update of the system
163
     *
164
     * @param $version
165
     * @return bool
166
     */
167
    public function doUpdate($version)
168
    {
169
        $this->registerMyLibrary();
170
171
        $update = new Update(
172
            $this,
173
            Shopware()->Models(),
174
            Shopware()->Db(),
175
            $version
176
        );
177
        return $update->run();
178
    }
179
180
    /**
181
     * Uninstall the plugin
182
     */
183 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...
184
    {
185
        $this->registerMyLibrary();
186
        $modelManager = Shopware()->Models();
187
        $uninstall = new Uninstall(
188
            $this,
189
            $modelManager,
190
            Shopware()->Db(),
191
            new \ShopwarePlugins\Connect\Bootstrap\Menu(
192
                $this,
193
                $this->getConfigComponents(),
194
                $modelManager,
195
                $this->assertMinimumVersion('5.2.6')
196
            )
197
        );
198
        return $uninstall->run();
199
    }
200
201
    /**
202
     * Will dynamically register all needed events
203
     *
204
     * @param Enlight_Event_EventArgs $args
205
     */
206
    public function onStartDispatch(Enlight_Event_EventArgs $args)
207
    {
208
        $this->registerMyLibrary();
209
        $this->registerSubscribers();
210
    }
211
212
    /**
213
     * @return ArrayCollection
214
     */
215
    public function onConsoleAddCommand()
216
    {
217
        $this->registerMyLibrary();
218
        $this->registerSubscribers();
219
220
        return new ArrayCollection([
221
            new ApiEndpointCommand()
222
        ]);
223
    }
224
225
    public function onInitResourceSDK()
226
    {
227
        $this->registerMyLibrary();
228
229
        return $this->getConnectFactory()->createSDK();
230
    }
231
232
    /**
233
     * Register additional namespaces for the libraries
234
     */
235
    public function registerMyLibrary()
236
    {
237
        $this->Application()->Loader()->registerNamespace(
238
            'Shopware\\Connect',
239
            $this->Path() . 'Library/Shopware/Connect/'
240
        );
241
        $this->Application()->Loader()->registerNamespace(
242
            'Firebase\\JWT',
243
            $this->Path() . 'Library/Firebase/JWT/'
244
        );
245
        $this->Application()->Loader()->registerNamespace(
246
            'ShopwarePlugins\\Connect',
247
            $this->Path()
248
        );
249
250
        $this->registerCustomModels();
251
    }
252
253
    /**
254
     * Lazy getter for the connectFactory
255
     *
256
     * @return ConnectFactory
257
     */
258
    public function getConnectFactory()
259
    {
260
        $this->registerMyLibrary();
261
262
        if (!$this->connectFactory) {
263
            $this->connectFactory = new ConnectFactory($this->getVersion());
264
        }
265
266
        return $this->connectFactory;
267
    }
268
269
    /**
270
     * @return Shopware\Connect\SDK
271
     */
272
    public function getSDK()
273
    {
274
        return $this->getConnectFactory()->getSDK();
275
    }
276
277
    /**
278
     * @return \ShopwarePlugins\Connect\Components\Helper
279
     */
280
    public function getHelper()
281
    {
282
        return $this->getConnectFactory()->getHelper();
283
    }
284
285
    public function getBasketHelper()
286
    {
287
        return $this->getConnectFactory()->getBasketHelper();
288
    }
289
290
    /**
291
     * @return \ShopwarePlugins\Connect\Components\Config
292
     */
293
    public function getConfigComponents()
294
    {
295
        return $this->getConnectFactory()->getConfigComponent();
296
    }
297
298
    public function getMarketplaceGateway()
299
    {
300
        return $this->getConnectFactory()->getMarketplaceGateway();
301
    }
302
303
    public function getMarketplaceApplier()
304
    {
305
        return $this->getConnectFactory()->getMarketplaceApplier();
306
    }
307
308
    /**
309
     * @return bool
310
     */
311
    private function isInstalled()
312
    {
313
        $builder = Shopware()->Models()->createQueryBuilder();
314
        $builder->select(array('plugins'))
315
            ->from('Shopware\Models\Plugin\Plugin', 'plugins');
316
317
        $builder->where('plugins.label = :label');
318
        $builder->setParameter('label', $this->getLabel());
319
320
        $query = $builder->getQuery();
321
        $plugin = $query->getOneOrNullResult();
322
        /** @var $plugin Shopware\Models\Plugin\Plugin */
323
        if (!$plugin) {
324
            return false;
325
        }
326
327
        return (bool) $plugin->getInstalled();
328
    }
329
330
    private function registerSubscribers()
331
    {
332
        if (!$this->subscriberRegistration instanceof  SubscriberRegistration) {
333
            $this->subscriberRegistration = new SubscriberRegistration(
334
                $this->getConfigComponents(),
335
                $this->get('models'),
336
                $this->get('db'),
337
                $this,
338
                $this->get('events'),
339
                $this->getSDK(),
340
                $this->getConnectFactory(),
341
                $this->getHelper()
342
            );
343
        }
344
345
        $this->subscriberRegistration->registerSubscribers($this->assertMinimumVersion('5.2'));
346
    }
347
}
348