Completed
Pull Request — master (#420)
by Tobias
12:51
created

getSnHttpClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 26 and the first side effect is on line 20.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

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