Menu::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 4
dl 0
loc 11
rs 9.9
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 \Shopware_Plugins_Backend_SwagConnect_Bootstrap
27
     */
28
    private $bootstrap;
29
30
    /**
31
     * @var \ShopwarePlugins\Connect\Components\Config
32
     */
33
    private $configComponent;
34
35
    /**
36
     * @var ModelManager
37
     */
38
    protected $modelManager;
39
40
    /**
41
     * @var string
42
     */
43
    private $shopware526installed;
44
45
    public function __construct(
46
        \Shopware_Plugins_Backend_SwagConnect_Bootstrap $bootstrap,
47
        Config $configComponent,
48
        ModelManager $modelManager,
49
        $shopware526installed
50
    ) {
51
        $this->bootstrap = $bootstrap;
52
        $this->configComponent = $configComponent;
53
        $this->modelManager = $modelManager;
54
        $this->shopware526installed = $shopware526installed;
55
    }
56
57
    /**
58
     * @return bool
59
     */
60
    public function isExists()
61
    {
62
        $menuItem = $this->getMainMenuItem();
63
        if (!$menuItem) {
64
            return false;
65
        }
66
67
        return true;
68
    }
69
70
    /**
71
     * @return \Shopware\Models\Menu\Menu | null
72
     */
73
    public function getMainMenuItem()
74
    {
75
        return $this->bootstrap->Menu()->findOneBy([
76
            'class' => self::CONNECT_CLASS,
77
            'parent' => null,
78
        ]);
79
    }
80
81
    /**
82
     * Creates Shopware Connect menu
83
     */
84
    public function create()
85
    {
86
        $connectItem = $this->getMainMenuItem();
87
        // check if shopware Connect menu item exists
88
        if (!$connectItem || $this->shopware526installed) {
89
            if ($this->shopware526installed) {
90
                $connectInstallItem = $this->bootstrap->Menu()->findOneBy(['label' => 'Einstieg', 'action' => 'ShopwareConnect']);
91
                if (null !== $connectInstallItem) {
92
                    $connectInstallItem->setActive(0);
93
                    $this->modelManager->persist($connectInstallItem);
94
                    $this->modelManager->flush();
95
                }
96
            } else {
97
                //move help menu item after Connect
98
                $helpItem = $this->bootstrap->Menu()->findOneBy(['label' => '']);
99
                $helpItem->setPosition(1);
100
                $this->modelManager->persist($helpItem);
101
                $this->modelManager->flush();
102
            }
103
104
            if ($connectItem) {
105
                $connectItem->setActive(1);
106
                $this->modelManager->persist($connectItem);
107
                $this->modelManager->flush();
108
            }
109
110
            $parent = $this->bootstrap->Menu()->findOneBy(['class' => self::CONNECT_CLASS]);
111
            if (null === $parent) {
112
                $parent = $this->bootstrap->createMenuItem([
113
                    'label' => self::CONNECT_LABEL,
114
                    'class' => 'connect-icon',
115
                    'active' => 1,
116
                ]);
117
118
                if ($this->shopware526installed) {
119
                    $parent->setClass(self::CONNECT_CLASS);
120
                    //if "Connect" menu does not exist
121
                    //it must not have pluginID, because on plugin uninstall
122
                    //it will be removed
123
                    $parent->setPlugin(null);
124
                }
125
            }
126
127
            if ($this->configComponent->getConfig('apiKey', '') == ''
128
                && !$this->configComponent->getConfig('shopwareId')) {
129
                $registerItem = $this->bootstrap->Menu()->findOneBy([
130
                    'controller' => 'Connect',
131
                    'action' => 'Register'
132
                ]);
133 View Code Duplication
                if (!$registerItem) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
134
                    $this->bootstrap->createMenuItem([
135
                        'label' => 'Register',
136
                        'controller' => 'Connect',
137
                        'action' => 'Register',
138
                        'class' => 'sprite-mousepointer-click',
139
                        'active' => 1,
140
                        'parent' => $parent
141
                    ]);
142
                }
143
            } else {
144
                // check if menu item already exists
145
                // this is possible when start update,
146
                // because setup function is called
147
                $importItem = $this->bootstrap->Menu()->findOneBy([
148
                    'controller' => 'Connect',
149
                    'action' => 'Import'
150
                ]);
151 View Code Duplication
                if (!$importItem) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
152
                    $this->bootstrap->createMenuItem([
153
                        'label' => 'Import',
154
                        'controller' => 'Connect',
155
                        'action' => 'Import',
156
                        'class' => 'sc-icon-import',
157
                        'active' => 1,
158
                        'parent' => $parent
159
                    ]);
160
                }
161
162
                $exportItem = $this->bootstrap->Menu()->findOneBy([
163
                    'controller' => 'Connect',
164
                    'action' => 'Export'
165
                ]);
166 View Code Duplication
                if (!$exportItem) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
167
                    $this->bootstrap->createMenuItem([
168
                        'label' => 'Export',
169
                        'controller' => 'Connect',
170
                        'action' => 'Export',
171
                        'class' => 'sc-icon-export',
172
                        'active' => 1,
173
                        'parent' => $parent
174
                    ]);
175
                }
176
177
                $settingsItem = $this->bootstrap->Menu()->findOneBy([
178
                    'controller' => 'Connect',
179
                    'action' => 'Settings'
180
                ]);
181 View Code Duplication
                if (!$settingsItem) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
182
                    $this->bootstrap->createMenuItem([
183
                        'label' => 'Settings',
184
                        'controller' => 'Connect',
185
                        'action' => 'Settings',
186
                        'class' => 'sprite-gear',
187
                        'active' => 1,
188
                        'parent' => $parent
189
                    ]);
190
                }
191
192
                $openConnectItem = $this->bootstrap->Menu()->findOneBy([
193
                    'controller' => 'Connect',
194
                    'action' => 'OpenConnect'
195
                ]);
196 View Code Duplication
                if (!$openConnectItem) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
197
                    $this->bootstrap->createMenuItem([
198
                        'label' => 'OpenConnect',
199
                        'controller' => 'Connect',
200
                        'action' => 'OpenConnect',
201
                        'onclick' => 'window.open("connect/autoLogin")',
202
                        'class' => 'connect-icon',
203
                        'active' => 1,
204
                        'parent' => $parent
205
                    ]);
206
                }
207
            }
208
        }
209
    }
210
211
    /**
212
     * Re-Activate the connect install menu item, if version is >= 5.2.6
213
     */
214
    public function remove()
215
    {
216
        if (!$this->shopware526installed) {
217
            return;
218
        }
219
220
        //if it is sem demo marketplace it will not find the correct menu item
221
        $connectMainMenu = $this->bootstrap->Menu()->findOneBy([
222
            'class' => self::CONNECT_CLASS,
223
            'parent' => null,
224
        ]);
225
226
        if (!$connectMainMenu) {
227
            $connectMainMenu = $this->bootstrap->createMenuItem([
228
                'label' => self::CONNECT_LABEL,
229
                'class' => self::CONNECT_CLASS,
230
                'active' => 1,
231
            ]);
232
            $connectMainMenu->setPlugin(null);
233
        } else {
234
            //resets the label if it's changed (diff marketplace)
235
            $connectMainMenu->setLabel(self::CONNECT_LABEL);
236
        }
237
238
        $connectInstallItem = $this->bootstrap->Menu()->findOneBy(['label' => 'Einstieg', 'action' => 'ShopwareConnect']);
239
        if (null !== $connectInstallItem) {
240
            $connectInstallItem->setActive(1);
241
            $connectInstallItem->setParent($connectMainMenu);
242 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
243
            $connectInstallItem = $this->bootstrap->createMenuItem([
244
                'label' => 'Einstieg',
245
                'controller' => 'PluginManager',
246
                'class' => 'sprite-mousepointer-click',
247
                'action' => 'ShopwareConnect',
248
                'active' => 1,
249
                'parent' => $connectMainMenu
250
            ]);
251
            $connectInstallItem->setPlugin(null);
252
        }
253
    }
254
}
255