Skip to content

Commit 4f8850e

Browse files
committed
Remove duplicate logic determining component type
1 parent 93a14f1 commit 4f8850e

File tree

4 files changed

+66
-25
lines changed

4 files changed

+66
-25
lines changed

src/com/magento/idea/magento2plugin/actions/generation/OverrideInThemeAction.java

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@
1313
import com.magento.idea.magento2plugin.MagentoIcons;
1414
import com.magento.idea.magento2plugin.actions.generation.dialog.OverrideInThemeDialog;
1515
import com.magento.idea.magento2plugin.project.Settings;
16-
import com.magento.idea.magento2plugin.util.RegExUtil;
16+
import com.magento.idea.magento2plugin.util.magento.ComponentType;
1717
import com.magento.idea.magento2plugin.util.magento.GetComponentNameByDirectory;
18+
import com.magento.idea.magento2plugin.util.magento.GetComponentTypeByName;
1819
import org.jetbrains.annotations.NotNull;
1920

20-
import java.util.regex.Matcher;
21-
import java.util.regex.Pattern;
22-
2321
public class OverrideInThemeAction extends DumbAwareAction {
2422
public static String ACTION_NAME = "Override in theme...";
2523
public static String ACTION_DESCRIPTION = "Override template in project theme.";
@@ -56,31 +54,18 @@ private boolean isOverrideAllowed(VirtualFile file, Project project) throws Null
5654
boolean isAllowed = false;
5755

5856
GetComponentNameByDirectory getComponentNameByDirectory = GetComponentNameByDirectory.getInstance(project);
59-
String componentName = getComponentNameByDirectory.execute(psiFile.getContainingDirectory());
57+
String componentType = GetComponentTypeByName.execute(getComponentNameByDirectory
58+
.execute(psiFile.getContainingDirectory()));
6059

61-
if (isModule(componentName)) {
60+
if (componentType.equals(ComponentType.TYPE_MODULE)) {
6261
isAllowed = file.getPath().contains("view");
63-
} else if (isTheme(componentName)) {
62+
} else if (componentType.equals(ComponentType.TYPE_THEME)) {
6463
isAllowed = true;
6564
}
6665

6766
return isAllowed;
6867
}
6968

70-
private boolean isModule(String componentName) {
71-
Pattern modulePattern = Pattern.compile(RegExUtil.Magento.MODULE_NAME);
72-
Matcher moduleMatcher = modulePattern.matcher(componentName);
73-
74-
return moduleMatcher.find();
75-
}
76-
77-
private boolean isTheme(String componentName) {
78-
Pattern themePattern = Pattern.compile(RegExUtil.Magento.THEME_NAME);
79-
Matcher themeMatcher = themePattern.matcher(componentName);
80-
81-
return themeMatcher.find();
82-
}
83-
8469
private void setStatus(AnActionEvent event, boolean status) {
8570
event.getPresentation().setVisible(status);
8671
event.getPresentation().setEnabled(status);

src/com/magento/idea/magento2plugin/actions/generation/generator/OverrideInThemeGenerator.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
import com.intellij.psi.PsiFile;
77
import com.magento.idea.magento2plugin.indexes.ModuleIndex;
88
import com.magento.idea.magento2plugin.util.RegExUtil;
9+
import com.magento.idea.magento2plugin.util.magento.ComponentType;
910
import com.magento.idea.magento2plugin.util.magento.GetComponentNameByDirectory;
11+
import com.magento.idea.magento2plugin.util.magento.GetComponentTypeByName;
1012

1113
import java.util.ArrayList;
1214
import java.util.Collections;
@@ -35,12 +37,16 @@ public void execute(PsiFile baseFile, String themeName) {
3537
ModuleIndex moduleIndex = ModuleIndex.getInstance(project);
3638
PsiDirectory directory = moduleIndex.getModuleDirectoryByModuleName(themeName);
3739
GetComponentNameByDirectory getComponentNameByDirectory = GetComponentNameByDirectory.getInstance(project);
38-
String componentName = getComponentNameByDirectory.execute(baseFile.getContainingDirectory());
40+
String componentType = GetComponentTypeByName.execute(getComponentNameByDirectory
41+
.execute(baseFile.getContainingDirectory()));
3942

4043
List<String> pathComponents;
41-
if (isModule(componentName)) {
42-
pathComponents = getModulePathComponents(baseFile, componentName);
43-
} else if (isTheme(componentName)) {
44+
if (componentType.equals(ComponentType.TYPE_MODULE)) {
45+
pathComponents = getModulePathComponents(
46+
baseFile,
47+
getComponentNameByDirectory.execute(baseFile.getContainingDirectory())
48+
);
49+
} else if (componentType.equals(ComponentType.TYPE_THEME)) {
4450
pathComponents = getThemePathComponents(baseFile);
4551
} else {
4652
return;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
package com.magento.idea.magento2plugin.util.magento;
6+
7+
public class ComponentType {
8+
public static String TYPE_MODULE = "module";
9+
public static String TYPE_THEME = "theme";
10+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
package com.magento.idea.magento2plugin.util.magento;
6+
7+
import com.magento.idea.magento2plugin.util.RegExUtil;
8+
import org.jetbrains.annotations.Nullable;
9+
10+
import java.util.regex.Matcher;
11+
import java.util.regex.Pattern;
12+
13+
public class GetComponentTypeByName {
14+
15+
@Nullable
16+
public static String execute(String componentName) {
17+
String componentType = null;
18+
if (isModule(componentName)) {
19+
componentType = ComponentType.TYPE_MODULE;
20+
} else if (isTheme(componentName)) {
21+
componentType = ComponentType.TYPE_THEME;
22+
}
23+
24+
return componentType;
25+
}
26+
27+
private static boolean isModule(String componentName) {
28+
Pattern modulePattern = Pattern.compile(RegExUtil.Magento.MODULE_NAME);
29+
Matcher moduleMatcher = modulePattern.matcher(componentName);
30+
31+
return moduleMatcher.find();
32+
}
33+
34+
private static boolean isTheme(String componentName) {
35+
Pattern themePattern = Pattern.compile(RegExUtil.Magento.THEME_NAME);
36+
Matcher themeMatcher = themePattern.matcher(componentName);
37+
38+
return themeMatcher.find();
39+
}
40+
}

0 commit comments

Comments
 (0)