Passed
Push — master ( e4ea65...5bc50c )
by Christian
15:36 queued 10s
created

src/Administration/Resources/app/administration/src/module/sw-extension/component/sw-extension-card-base/index.js   C

Complexity

Total Complexity 57
Complexity/F 1.39

Size

Lines of Code 306
Function Count 41

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 171
c 0
b 0
f 0
dl 0
loc 306
rs 5.04
wmc 57
mnd 16
bc 16
fnc 41
bpm 0.3902
cpm 1.3902
noi 2

How to fix   Complexity   

Complexity

Complex classes like src/Administration/Resources/app/administration/src/module/sw-extension/component/sw-extension-card-base/index.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import template from './sw-extension-card-base.html.twig';
2
import './sw-extension-card-base.scss';
3
4
const { Component, Utils, Filter } = Shopware;
0 ignored issues
show
Bug introduced by
The variable Shopware seems to be never declared. If this is a global, consider adding a /** global: Shopware */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
5
6
/**
7
 * @private
8
 */
9
Component.register('sw-extension-card-base', {
10
    template,
11
12
    inheritAttrs: false,
13
14
    inject: ['shopwareExtensionService', 'extensionStoreActionService', 'cacheApiService'],
15
16
    mixins: ['sw-extension-error'],
17
18
    props: {
19
        extension: {
20
            type: Object,
21
            required: true
22
        }
23
    },
24
25
    data() {
26
        return {
27
            isLoading: false,
28
            showUninstallModal: false,
29
            showRemovalModal: false,
30
            showPermissionsModal: false,
31
            permissionsAccepted: false,
32
            showPrivacyModal: false,
33
            permissionModalActionLabel: null
34
        };
35
    },
36
37
    computed: {
38
        dateFilter() {
39
            return Shopware.Filter.getByName('date');
0 ignored issues
show
Bug introduced by
The variable Shopware seems to be never declared. If this is a global, consider adding a /** global: Shopware */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
40
        },
41
42
        defaultThemeAsset() {
43
            return this.assetFilter('administration/static/img/theme/default_theme_preview.jpg');
44
        },
45
46
        extensionCardClasses() {
47
            return {
48
                'is--deactivated': this.isInstalled && !this.extension.active
49
            };
50
        },
51
52
        licensedExtension() {
53
            return this.extension.storeLicense;
54
        },
55
56
        description() {
57
            if (this.extension.shortDescription) {
58
                return this.extension.shortDescription;
59
            }
60
61
            return this.extension.description;
62
        },
63
64
        image() {
65
            if (this.extension.icon) {
66
                return this.extension.icon;
67
            }
68
69
            if (this.extension.iconRaw) {
70
                return `data:image/png;base64, ${this.extension.iconRaw}`;
71
            }
72
73
            return this.defaultThemeAsset;
74
        },
75
76
        isActive: {
77
            get() {
78
                if (!this.isInstalled) {
79
                    return false;
80
                }
81
82
                return this.extension.active;
83
            },
84
            set(active) {
85
                if (!this.isInstalled) {
86
                    return;
87
                }
88
89
                this.extension.active = active;
90
91
                this.$nextTick(() => {
92
                    this.changeExtensionStatus();
93
                }, 0);
94
            }
95
        },
96
97
        isInstalled() {
98
            return this.extension.installedAt !== null;
99
        },
100
101
        canBeOpened() {
102
            return this.shopwareExtensionService.canBeOpened(this.extension);
103
        },
104
105
        privacyPolicyLink() {
106
            return this.extension.privacyPolicyLink;
107
        },
108
109
        permissions() {
110
            return Object.keys(this.extension.permissions).length ?
111
                this.extension.permissions : null;
112
        },
113
114
        assetFilter() {
115
            return Filter.getByName('asset');
116
        },
117
118
        isRemovable() {
119
            if (this.extension.installedAt === null && this.extension.source === 'local') {
120
                return true;
121
            }
122
123
            return false;
124
        },
125
126
        isUninstallable() {
127
            if (this.extension.installedAt !== null) {
128
                return true;
129
            }
130
131
            return false;
132
        },
133
134
        isUpdateable() {
135
            if (this.extension.latestVersion === null) {
136
                return false;
137
            }
138
139
            return this.extension.latestVersion !== this.extension.version;
140
        }
141
    },
142
143
    methods: {
144
        emitUpdateList() {
145
            this.$emit('updateList');
146
        },
147
148
        getHelp() {
149
            // implemented in SAAS-1137
150
        },
151
152
        openPrivacyAndSafety() {
153
            window.open(this.extension.privacyPolicyLink, '_blank');
154
        },
155
156
        openRemovalModal() {
157
            this.showRemovalModal = true;
158
        },
159
160
        openUninstallModal() {
161
            this.showUninstallModal = true;
162
        },
163
164
        closeRemovalModal() {
165
            this.showRemovalModal = false;
166
        },
167
168
        closeUninstallModal() {
169
            this.showUninstallModal = false;
170
        },
171
172
        async closeModalAndUninstallExtension(removeData) {
173
            this.showUninstallModal = false;
174
            this.isLoading = true;
175
176
            try {
177
                await this.shopwareExtensionService.uninstallExtension(
178
                    this.extension.name,
179
                    this.extension.type,
180
                    removeData
181
                );
182
                this.clearCacheAndReloadPage();
183
            } catch (e) {
184
                this.showExtensionErrors(e);
185
            } finally {
186
                this.isLoading = false;
187
            }
188
        },
189
190
        async updateExtension() {
191
            this.isLoading = true;
192
193
            try {
194
                if (this.extension.updateSource === 'remote') {
195
                    await this.extensionStoreActionService.downloadExtension(this.extension.name);
196
                }
197
198
                await this.shopwareExtensionService.updateExtension(
199
                    this.extension.name,
200
                    this.extension.type
201
                );
202
                this.clearCacheAndReloadPage();
203
            } catch (e) {
204
                this.showExtensionErrors(e);
205
            } finally {
206
                this.isLoading = false;
207
            }
208
        },
209
210
        async closeModalAndRemoveExtension() {
211
            // we close the modal in the called methods before updating the listing
212
            if (this.extension.storeLicense === null) {
213
                await this.removeExtension();
214
                this.showRemovalModal = false;
215
216
                return;
217
            }
218
219
            await this.cancelAndRemoveExtension();
220
            this.showRemovalModal = false;
221
        },
222
223
        async openExtension() {
224
            const openLink = await this.shopwareExtensionService.getOpenLink(this.extension);
225
226
            if (openLink) {
227
                this.$router.push(openLink);
228
            }
229
        },
230
231
        openPermissionsModalForInstall() {
232
            if (!this.permissions) {
233
                this.permissionsAccepted = true;
234
                this.installExtension();
235
236
                return;
237
            }
238
239
            this.permissionModalActionLabel = this.$tc('sw-extension-store.component.sw-extension-card-base.labelAcceptAndInstall');
240
            this.showPermissionsModal = true;
241
        },
242
243
        openPermissionsModal() {
244
            this.permissionModalActionLabel = null;
245
            this.showPermissionsModal = true;
246
        },
247
248
        closePermissionsModal() {
249
            this.permissionModalActionLabel = null;
250
            this.showPermissionsModal = false;
251
        },
252
253
        async closePermissionsModalAndInstallExtension() {
254
            this.permissionsAccepted = true;
255
            this.closePermissionsModal();
256
            await this.installExtension();
257
        },
258
259
        /*
260
         * Interface for deriving components
261
         */
262
        async changeExtensionStatus() {
263
            Utils.debug.warn(this._name, 'No implementation of changeExtensionStatus found');
264
        },
265
266
        installExtension() {
267
            Utils.debug.warn(this._name, 'No implementation of installExtension found');
268
        },
269
270
        async removeExtension() {
271
            try {
272
                this.showRemovalModal = false;
273
                this.isLoading = true;
274
275
                await this.shopwareExtensionService.removeExtension(
276
                    this.extension.name,
277
                    this.extension.type
278
                );
279
                this.extension.active = false;
280
            } catch (e) {
281
                this.showStoreError(e);
282
            } finally {
283
                this.isLoading = false;
284
            }
285
        },
286
287
        cancelAndRemoveExtension() {
288
            Utils.debug.warn(this._name, 'No implementation of cancelAndRemoveExtension found');
289
        },
290
291
        openPrivacyModal() {
292
            this.showPrivacyModal = true;
293
        },
294
295
        closePrivacyModal() {
296
            this.showPrivacyModal = false;
297
        },
298
299
        clearCacheAndReloadPage() {
300
            return this.cacheApiService.clear()
301
                .then(() => {
302
                    window.location.reload();
303
                });
304
        }
305
    }
306
});
307