Completed
Pull Request — master (#421)
by Christian
03:14
created

SwagConnect::getInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 9
rs 9.6666
c 1
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 30 and the first side effect is on line 24.

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
namespace SwagConnect;
9
10
use Doctrine\Common\Collections\ArrayCollection;
11
use Shopware\Components\Plugin;
12
use Shopware\Components\Plugin\Context\InstallContext;
13
use Shopware\Components\Plugin\Context\UninstallContext;
14
use Shopware\Components\Plugin\Context\UpdateContext;
15
use ShopwarePlugins\Connect\Bootstrap\SubscriberRegistration;
16
use ShopwarePlugins\Connect\Bootstrap\Uninstall;
17
use ShopwarePlugins\Connect\Bootstrap\Update;
18
use ShopwarePlugins\Connect\Bootstrap\Setup;
19
use ShopwarePlugins\Connect\Commands\ApiEndpointCommand;
20
use ShopwarePlugins\Connect\Components\BasketHelper;
21
use ShopwarePlugins\Connect\Components\ConnectFactory;
22
use ShopwarePlugins\Connect\Components\Logger;
23
24
require_once __DIR__ . '/vendor/autoload.php';
25
26
/**
27
 * @category  Shopware
28
 * @package   Shopware\Plugins\SwagConnect
29
 */
30
class SwagConnect extends Plugin
31
{
32
    /**
33
     * @var SubscriberRegistration
34
     */
35
    private $subscriberRegistration;
36
37
    /**
38
     * @var ConnectFactory
39
     */
40
    private $connectFactory;
41
42
    /**
43
     * Returns the current version of the plugin.
44
     *
45
     * @throws \Exception
46
     * @return string|void
47
     */
48
    public function getVersion()
49
    {
50
        $info = json_decode(file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'plugin.json'), true);
51
52
        if ($info) {
53
            return $info['currentVersion'];
54
        }
55
        throw new \Exception('The plugin has an invalid version file.');
56
    }
57
58
    /**
59
     * Returns a nice name for plugin manager list
60
     *
61
     * @return string
62
     */
63
    public function getLabel()
64
    {
65
        return 'Shopware Connect';
66
    }
67
68
    /**
69
     * @return array
70
     */
71
    public function getInfo()
72
    {
73
        return [
74
            'version' => $this->getVersion(),
75
            'label' => $this->getLabel(),
76
            'description' => file_get_contents($this->getPath() . 'info.txt'),
77
            'link' => 'http://www.shopware.de/',
78
        ];
79
    }
80
81
    /**
82
     * Install plugin method
83
     *
84
     * @param $context InstallContext
85
     * @throws \RuntimeException
86
     * @return array
87
     */
88
    public function install(InstallContext $context)
89
    {
90
        $this->doSetup($context);
91
92
        return ['success' => true, 'invalidateCache' => ['backend', 'config']];
93
    }
94
95
    /**
96
     * @param $context UpdateContext
97
     * @return array
98
     */
99
    public function update(UpdateContext $context)
100
    {
101
        // sometimes plugin is not installed before
102
        // but could be updated. by this way setup process
103
        // is simple and only required structure will be created
104
        // e.g. DB and attributes
105
        $fullSetup = false;
106
        if ($this->isInstalled()) {
107
            $fullSetup = true;
108
        }
109
110
        $this->doSetup($context, $fullSetup);
111
        $this->doUpdate($context->getUpdateVersion());
112
113
        return ['success' => true, 'invalidateCache' => ['backend', 'config', 'template', 'theme', 'proxy']];
114
    }
115
116
    /**
117
     * Uninstall plugin method
118
     * @param $context UninstallContext
119
     * @return bool
120
     */
121
    public function uninstall(UninstallContext $context)
122
    {
123
        $this->doUninstall($context);
124
125
        return true;
126
    }
127
128
    /**
129
     * Performs the default setup of the system.
130
     *
131
     * This can be used by the update as well as by the install method
132
     *
133
     * @param InstallContext $context
134
     * @param bool $fullSetup
135
     * @throws RuntimeException
136
     */
137 View Code Duplication
    public function doSetup($context, $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...
138
    {
139
        $modelManager = Shopware()->Models();
140
        $setup = new Setup(
141
            $modelManager,
142
            Shopware()->Db(),
143
            new \ShopwarePlugins\Connect\Bootstrap\Menu(
144
                $this->getConfigComponents(),
145
                $modelManager,
146
                $context->assertMinimumVersion('5.2.6')
147
            )
148
        );
149
        $setup->run($fullSetup, $this->getPath());
150
    }
151
152
    /**
153
     * Performs the update of the system
154
     *
155
     * @param $version
156
     * @return bool
157
     */
158
    public function doUpdate($version)
159
    {
160
        $update = new Update(
161
            Shopware()->Models(),
162
            Shopware()->Db(),
163
            new Logger(Shopware()->Db()),
164
            $version
165
        );
166
167
        return $update->run();
168
    }
169
170
    /**
171
     * Uninstall the plugin
172
     * @param $context UninstallContext
173
     * @return bool
174
     */
175 View Code Duplication
    public function doUninstall($context)
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...
176
    {
177
        $modelManager = Shopware()->Models();
178
        $uninstall = new Uninstall(
179
            $modelManager,
180
            Shopware()->Db(),
181
            new \ShopwarePlugins\Connect\Bootstrap\Menu(
182
                $this->getConfigComponents(),
183
                $modelManager,
184
                $context->assertMinimumVersion('5.2.6')
185
            )
186
        );
187
188
        return $uninstall->run();
189
    }
190
191
    public static function getSubscribedEvents()
192
    {
193
        return [
194
            'Enlight_Bootstrap_InitResource_ConnectSDK' => 'onInitResourceSDK',
195
            'Enlight_Controller_Front_DispatchLoopStartup' => 'onStartDispatch',
196
            'Shopware_Console_Add_Command' => 'onConsoleAddCommand'
197
        ];
198
    }
199
200
    /**
201
     * Will dynamically register all needed events
202
     *
203
     * @param Enlight_Event_EventArgs $args
0 ignored issues
show
Documentation introduced by
Should the type for parameter $args not be \Enlight_Event_EventArgs?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
204
     */
205
    public function onStartDispatch(\Enlight_Event_EventArgs $args)
206
    {
207
        $this->container->get('template')->addTemplateDir($this->getPath() . '/Views/', 'connect');
208
        $this->container->get('snippets')->addConfigDir($this->getPath() . '/Snippets/');
209
        $this->registerSubscribers();
210
    }
211
212
    /**
213
     * @return ArrayCollection
214
     */
215
    public function onConsoleAddCommand()
216
    {
217
        $this->registerSubscribers();
218
219
        return new ArrayCollection(
220
            [
221
                new ApiEndpointCommand()
222
            ]
223
        );
224
    }
225
226
    public function onInitResourceSDK()
227
    {
228
        return $this->getConnectFactory()->createSDK();
229
    }
230
231
    /**
232
     * Lazy getter for the connectFactory
233
     *
234
     * @return ConnectFactory
235
     */
236
    public function getConnectFactory()
237
    {
238
        if (!$this->connectFactory) {
239
            $this->connectFactory = new ConnectFactory($this->getVersion());
240
        }
241
242
        return $this->connectFactory;
243
    }
244
245
    /**
246
     * @return \Shopware\Connect\SDK
247
     */
248
    public function getSDK()
249
    {
250
        return $this->getConnectFactory()->getSDK();
251
    }
252
253
    /**
254
     * @return \ShopwarePlugins\Connect\Components\Helper
255
     */
256
    public function getHelper()
257
    {
258
        return $this->getConnectFactory()->getHelper();
259
    }
260
261
    /**
262
     * @return BasketHelper
263
     */
264
    public function getBasketHelper()
265
    {
266
        return $this->getConnectFactory()->getBasketHelper();
267
    }
268
269
    /**
270
     * @return \ShopwarePlugins\Connect\Components\Config
271
     */
272
    public function getConfigComponents()
273
    {
274
        return $this->getConnectFactory()->getConfigComponent();
275
    }
276
277
    /**
278
     * @return bool
279
     */
280
    private function isInstalled()
281
    {
282
        $builder = Shopware()->Models()->createQueryBuilder();
283
        $builder->select(['plugins'])
284
            ->from('Shopware\Models\Plugin\Plugin', 'plugins');
285
286
        $builder->where('plugins.label = :label');
287
        $builder->setParameter('label', $this->getLabel());
288
289
        $query = $builder->getQuery();
290
        $plugin = $query->getOneOrNullResult();
291
        /** @var $plugin \Shopware\Models\Plugin\Plugin */
292
        if (!$plugin) {
293
            return false;
294
        }
295
296
        return (bool) $plugin->getInstalled();
297
    }
298
299
    private function registerSubscribers()
300
    {
301
        if (!$this->subscriberRegistration instanceof SubscriberRegistration) {
302
            $this->subscriberRegistration = new SubscriberRegistration(
303
                $this->getConfigComponents(),
304
                $this->container->get('models'),
305
                $this->container->get('db'),
306
                $this,
307
                $this->container->get('events'),
308
                $this->getSDK(),
309
                $this->getConnectFactory(),
310
                $this->getHelper(),
311
                $this->container->get('service_container')
312
            );
313
        }
314
315
        $this->subscriberRegistration->registerSubscribers();
316
    }
317
}
318