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

Menu::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 3
dl 10
loc 10
rs 9.4285
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
8
namespace ShopwarePlugins\Connect\Bootstrap;
9
10
use ShopwarePlugins\Connect\Components\Config;
11
use Shopware\Components\Model\ModelManager;
12
13
class Menu
14
{
15
    /**
16
     * menu item label
17
     */
18
    const CONNECT_LABEL = 'Connect';
19
20
    /**
21
     * menu item class name
22
     */
23
    const CONNECT_CLASS = 'shopware-connect';
24
25
    /**
26
     * @var \ShopwarePlugins\Connect\Components\Config
27
     */
28
    private $configComponent;
29
30
    /**
31
     * @var ModelManager
32
     */
33
    protected $modelManager;
34
35
    /**
36
     * @var string
37
     */
38
    private $shopware526installed;
39
40
    /**
41
     * @var \Doctrine\ORM\EntityRepository
42
     */
43
    private $menuRepository;
44
45 View Code Duplication
    public function __construct(
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...
46
        Config $configComponent,
47
        ModelManager $modelManager,
48
        $shopware526installed
49
    ) {
50
        $this->configComponent = $configComponent;
51
        $this->modelManager = $modelManager;
52
        $this->shopware526installed = $shopware526installed;
53
        $this->menuRepository = Shopware()->Models()->getRepository(\Shopware\Models\Menu\Menu::class);
54
    }
55
56
    /**
57
     * @return bool
58
     */
59
    public function isExists()
60
    {
61
        $menuItem = $this->getMainMenuItem();
62
        if (!$menuItem) {
63
            return false;
64
        }
65
66
        return true;
67
    }
68
69
    /**
70
     * @return \Shopware\Models\Menu\Menu
71
     */
72
    public function getMainMenuItem()
73
    {
74
        $menuItem = $this->menuRepository->findOneBy([
75
            'class' => self::CONNECT_CLASS,
76
            'parent' => null,
77
        ]);
78
79
        if(!$menuItem) {
80
            throw new \RuntimeException('Could not find entry');
81
        }
82
83
        return $menuItem;
84
    }
85
86
    /**
87
     * @param array $selector
88
     * @return mixed
89
     * @throws \RuntimeException
90
     */
91
    private function fetchOneBy(array $selector)
92
    {
93
        $menuItem = $this->menuRepository->findOneBy($selector);
94
95
        if(!$menuItem) {
96
            throw new \RuntimeException('Could not find entry');
97
        }
98
99
        return $menuItem;
100
    }
101
102
    /**
103
     * Creates Shopware Connect menu
104
     */
105
    public function synchronize()
106
    {
107
108
        try {
109
            $connectMainMenu = $this->getMainMenuItem();
110
        } catch (\RuntimeException $e) {
111
            $connectMainMenu = $this->createConnectMainMenu();
112
        }
113
114
115
        $this->activateConnectMenuItem($connectMainMenu);
116
        $this->deactivateInstallConnectMenuItem();
117
118
        if ($this->isUnregistered()) {
119
            $this->createRegisterMenuItem($connectMainMenu);
120
            $this->removeMainRegisteredMenu();
121
        } else {
122
            $this->createMainRegisteredMenu($connectMainMenu);
123
            $this->removeRegisterMenu();
124
        }
125
    }
126
127
    public function remove()
128
    {
129
130
        $this->removeRegisterMenu();
131
        $this->removeMainRegisteredMenu();
132
133
        if (!$this->shopware526installed) {
134
            return;
135
        }
136
        $this->resetInstallConnectMenu();
137
    }
138
139
    private function removeMainRegisteredMenu()
140
    {
141
        $this->removeMenuItem(['label' => 'Import', 'action' => 'Import']);
142
        $this->removeMenuItem(['label' => 'Export', 'action' => 'Export']);
143
        $this->removeMenuItem(['label' => 'Settings', 'action' => 'Settings']);
144
        $this->removeMenuItem(['label' => 'OpenConnect', 'action' => 'OpenConnect']);
145
    }
146
147
    private function removeRegisterMenu()
148
    {
149
        $this->removeMenuItem(['label' => 'Register', 'controller' => 'Connect', 'action' => 'Register',]);
150
    }
151
152
    /**
153
     * @param array $selector
154
     */
155
    private function removeMenuItem($selector)
156
    {
157
        try {
158
            $menuItem = $this->fetchOneBy($selector);
159
            $this->modelManager->remove($menuItem);
160
        } catch (\RuntimeException $e) {
161
            return;
162
        }
163
    }
164
165
    /**
166
     * @param array $values
167
     * @return \Shopware\Models\Menu\Menu
168
     */
169
    private function createMenuItem($values)
170
    {
171
        $menu = new \Shopware\Models\Menu\Menu();
172
        $menu->setLabel($values['label']);
173
        $menu->setController($values['controller']);
174
        $menu->setClass($values['class']);
175
        $menu->setAction($values['action']);
176
        $menu->setActive($values['active']);
177
        $menu->setParent($values['parent']);
178
179
        $this->modelManager->persist($menu);
180
        $this->modelManager->flush();
181
182
        return $menu;
183
    }
184
185
    /**
186
     * @return bool
187
     */
188
    private function isUnregistered()
189
    {
190
        return $this->configComponent->getConfig('apiKey', '') == ''
191
            && !$this->configComponent->getConfig('shopwareId');
192
    }
193
194
    /**
195
     * @param \Shopware\Models\Menu\Menu $parent
196
     */
197
    private function createRegisterMenuItem(\Shopware\Models\Menu\Menu $parent)
198
    {
199
        $registerItem = $this->menuRepository->findOneBy([
200
            'controller' => 'Connect',
201
            'action' => 'Register'
202
        ]);
203
        if (!$registerItem) {
204
            $this->createMenuItem([
205
                'label' => 'Register',
206
                'controller' => 'Connect',
207
                'action' => 'Register',
208
                'class' => 'sprite-mousepointer-click',
209
                'active' => 1,
210
                'parent' => $parent
211
            ]);
212
        }
213
    }
214
215
    private function resetInstallConnectMenu()
216
    {
217
        try {
218
            //if it is sem demo marketplace it will not find the connectMainMenu
219
            $connectMainMenu = $this->getMainMenuItem();
220
        } catch (\RuntimeException $e) {
221
            $connectMainMenu = $this->createConnectMainMenu();
222
        }
223
224
        //resets the label if it's changed (diff marketplace)
225
        $connectMainMenu->setLabel(self::CONNECT_LABEL);
226
227
        try {
228
            $connectInstallItem = $this->fetchOneBy(['label' => 'Einstieg', 'action' => 'ShopwareConnect']);
229
            $connectInstallItem->setParent($connectMainMenu);
230
            $this->activateConnectMenuItem($connectInstallItem);
231
        } catch (\RuntimeException $e) {
232
            $this->createInstallConnectMenu($connectMainMenu);
233
        }
234
    }
235
236
    /**
237
     * @param \Shopware\Models\Menu\Menu $parent
238
     */
239
    private function createMainRegisteredMenu(\Shopware\Models\Menu\Menu $parent)
240
    {
241
        // check if menu item already exists
242
        // this is possible when start update,
243
        // because setup function is called
244
        if($this->isMenuAlreadyCreated()) {
245
            return;
246
        }
247
248
        $this->createMenuItem([
249
            'label' => 'Import',
250
            'controller' => 'Connect',
251
            'action' => 'Import',
252
            'class' => 'sc-icon-import',
253
            'active' => 1,
254
            'parent' => $parent
255
        ]);
256
257
        $this->createMenuItem([
258
            'label' => 'Export',
259
            'controller' => 'Connect',
260
            'action' => 'Export',
261
            'class' => 'sc-icon-export',
262
            'active' => 1,
263
            'parent' => $parent
264
        ]);
265
266
        $this->createMenuItem([
267
            'label' => 'Settings',
268
            'controller' => 'Connect',
269
            'action' => 'Settings',
270
            'class' => 'sprite-gear',
271
            'active' => 1,
272
            'parent' => $parent
273
        ]);
274
275
        $this->createMenuItem([
276
            'label' => 'OpenConnect',
277
            'controller' => 'Connect',
278
            'action' => 'OpenConnect',
279
            'onclick' => 'window.open("connect/autoLogin")',
280
            'class' => 'connect-icon',
281
            'active' => 1,
282
            'parent' => $parent
283
        ]);
284
    }
285
286
    private function deactivateInstallConnectMenuItem()
287
    {
288
        try {
289
            $connectInstallItem = $this->fetchOneBy(['label' => 'Einstieg', 'action' => 'ShopwareConnect']);
290
291
            $connectInstallItem->setActive(0);
292
            $this->modelManager->persist($connectInstallItem);
293
            $this->modelManager->flush();
294
        } catch (\RuntimeException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
295
        }
296
    }
297
298
    private function isMenuAlreadyCreated()
299
    {
300
        try {
301
            $importItem = $this->fetchOneBy([
0 ignored issues
show
Unused Code introduced by
$importItem is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
302
                'controller' => 'Connect',
303
                'action' => 'Import'
304
            ]);
305
306
            return true;
307
        } catch (\RuntimeException $e) {
308
            return false;
309
        }
310
    }
311
312
    /**
313
     * @param \Shopware\Models\Menu\Menu $connectItem
314
     */
315
    private function activateConnectMenuItem(\Shopware\Models\Menu\Menu $connectItem)
316
    {
317
        $connectItem->setActive(1);
318
        $this->modelManager->persist($connectItem);
319
        $this->modelManager->flush();
320
    }
321
322
    /**
323
     * @param \Shopware\Models\Menu\Menu $connectMainMenu
324
     */
325
    private function createInstallConnectMenu(\Shopware\Models\Menu\Menu $connectMainMenu)
326
    {
327
        $connectInstallItem = $this->createMenuItem([
328
            'label' => 'Einstieg',
329
            'controller' => 'PluginManager',
330
            'class' => 'sprite-mousepointer-click',
331
            'action' => 'ShopwareConnect',
332
            'active' => 1,
333
            'parent' => $connectMainMenu
334
        ]);
335
        $connectInstallItem->setPlugin(null);
336
        $this->modelManager->persist($connectInstallItem);
337
        $this->modelManager->flush();
338
    }
339
340
    /**
341
     * @return \Shopware\Models\Menu\Menu
342
     */
343
    private function createConnectMainMenu()
344
    {
345
        $connectMainMenu = $this->createMenuItem([
346
            'label' => self::CONNECT_LABEL,
347
            'class' => self::CONNECT_CLASS,
348
            'active' => 1,
349
        ]);
350
        $connectMainMenu->setPlugin(null);
351
        $this->modelManager->persist($connectMainMenu);
352
        $this->modelManager->flush();
353
        return $connectMainMenu;
354
    }
355
}
356