Skip to content

Eav attribute generation refactoring #538

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace ${NAMESPACE};
use $use;
#end

class ${NAME} implements ${IMPLEMENTS} {
class ${CLASS_NAME} implements ${IMPLEMENTS} {

/**
* @var ${MODULE_DATA_SETUP_INTERFACE}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ class ${NAME} extends ${EXTENDS}
*/
public function getAllOptions(): array
{
// TODO: Implement getAllOptions() method.
return [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

package com.magento.idea.magento2plugin.actions.generation.data;

import com.magento.idea.magento2plugin.magento.packages.File;
import com.magento.idea.magento2plugin.magento.packages.Package;
import com.magento.idea.magento2plugin.magento.files.EavAttributeDataPatchFile;
import com.magento.idea.magento2plugin.magento.packages.eav.EavEntity;

@SuppressWarnings({"PMD.TooManyFields"})
Expand Down Expand Up @@ -144,25 +143,9 @@ public String getInput() {

@Override
public String getNamespace() {
if (namespace == null) {
namespace = getDataPathNamespace();
}

return namespace;
}

private String getDataPathNamespace() {
final String[] parts = moduleName.split(Package.vendorModuleNameSeparator);
if (parts[0] == null || parts[1] == null || parts.length > 2) {
return null;
}
final String directoryPart = getDirectory().replace(
File.separator,
Package.fqnSeparator
);
return parts[0] + Package.fqnSeparator + parts[1] + Package.fqnSeparator + directoryPart;
}

@Override
public String getModuleName() {
return moduleName;
Expand All @@ -171,7 +154,7 @@ public String getModuleName() {
@Override
public String getDirectory() {
if (directory == null) {
directory = "Setup/Patch/Data";
directory = EavAttributeDataPatchFile.DEFAULT_DIR;
}

return directory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,17 @@

package com.magento.idea.magento2plugin.actions.generation.data;

import com.magento.idea.magento2plugin.magento.files.SourceModelPhp;
import com.magento.idea.magento2plugin.magento.packages.File;
import com.magento.idea.magento2plugin.magento.packages.Package;
import com.magento.idea.magento2plugin.magento.files.SourceModelFile;

public class SourceModelData {
private String className;
private String namespace;
private String moduleName;
private String directory;

public String getClassName() {
return className;
}

/**
* Constructor.
*/
public String getNamespace() {
if (namespace == null) {
namespace = getDefaultSourceModelNamespace();
}

return namespace;
}

/**
* Provides default namespace.
*
* @return String
*/
public String getDefaultSourceModelNamespace() {
final String[] parts = moduleName.split(Package.vendorModuleNameSeparator);
if (parts[0] == null || parts[1] == null || parts.length > 2) {
return null;
}
final String directoryPart = getDirectory().replace(
File.separator,
Package.fqnSeparator
);
return parts[0] + Package.fqnSeparator + parts[1] + Package.fqnSeparator + directoryPart;
}

/**
* Get default namespace.
*
Expand All @@ -63,7 +32,7 @@ public String getModuleName() {
*/
public String getDirectory() {
if (this.directory == null) {
return SourceModelPhp.DEFAULT_DIR;
return SourceModelFile.DEFAULT_DIR;
}

return this.directory;
Expand All @@ -73,10 +42,6 @@ public void setClassName(final String className) {
this.className = className;
}

public void setNamespace(final String namespace) {
this.namespace = namespace;
}

public void setModuleName(final String moduleName) {
this.moduleName = moduleName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.NotEmptyRule;
import com.magento.idea.magento2plugin.actions.generation.generator.EavAttributeSetupPatchGenerator;
import com.magento.idea.magento2plugin.actions.generation.generator.SourceModelGenerator;
import com.magento.idea.magento2plugin.magento.packages.Package;
import com.magento.idea.magento2plugin.magento.packages.eav.*;
import com.magento.idea.magento2plugin.magento.files.SourceModelFile;
import com.magento.idea.magento2plugin.magento.packages.eav.AttributeInput;
import com.magento.idea.magento2plugin.magento.packages.eav.AttributeScope;
import com.magento.idea.magento2plugin.magento.packages.eav.AttributeSourceModel;
import com.magento.idea.magento2plugin.magento.packages.eav.AttributeType;
import com.magento.idea.magento2plugin.magento.packages.eav.EavEntity;
import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectoryUtil;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
Expand All @@ -38,7 +42,12 @@
import org.codehaus.plexus.util.StringUtils;
import org.jetbrains.annotations.NotNull;

@SuppressWarnings({"PMD.TooManyFields", "PMD.ExcessiveImports", "PMD.TooManyMethods", "PMD.UnusedPrivateField"})
@SuppressWarnings({
"PMD.TooManyFields",
"PMD.ExcessiveImports",
"PMD.TooManyMethods",
"PMD.UnusedPrivateField"
})
public class NewEavAttributeDialog extends AbstractDialog {
private final String moduleName;
private JPanel contentPanel;
Expand Down Expand Up @@ -285,22 +294,25 @@ private void generateSourceModelFile() {
final ComboBoxItemData selectedSource = (ComboBoxItemData) sourceComboBox.getSelectedItem();

if (selectedSource == null
|| !selectedSource.getText().equals(AttributeSourceModel.GENERATE_SOURCE.getSource())) {
|| !selectedSource.getText().equals(
AttributeSourceModel.GENERATE_SOURCE.getSource()
)) {
return;
}

sourceModelData.setModuleName(moduleName);
sourceModelData.setClassName(sourceModelNameTexField.getText().trim());
sourceModelData.setDirectory(sourceModelDirectoryTexField.getText().trim());

new SourceModelGenerator(project, sourceModelData)
new SourceModelGenerator(sourceModelData, project, true)
.generate(NewEavAttributeAction.ACTION_NAME, false);
}

private void generateDataPatchFile() {
new EavAttributeSetupPatchGenerator(
populateProductEntityData(new ProductEntityData()),
project
project,
true
).generate(NewEavAttributeAction.ACTION_NAME, true);
}

Expand Down Expand Up @@ -331,19 +343,20 @@ private String getAttributeSource() {
final ComboBoxItemData selectedItem = (ComboBoxItemData) sourceComboBox.getSelectedItem();

if (selectedItem == null
|| selectedItem.getText().equals(AttributeSourceModel.NULLABLE_SOURCE.getSource())) {
|| selectedItem.getText().equals(
AttributeSourceModel.NULLABLE_SOURCE.getSource()
)) {
return null;
}

if (selectedItem.getText().equals(AttributeSourceModel.GENERATE_SOURCE.getSource())
&& sourceModelData != null) {

return String.join(
Package.fqnSeparator,
"",
sourceModelData.getNamespace(),
sourceModelData.getClassName()
);
return "\\" + new SourceModelFile(
sourceModelData.getModuleName(),
sourceModelData.getClassName(),
sourceModelData.getDirectory()
).getClassFqn();
}

return sourceComboBox.getSelectedItem().toString();
Expand All @@ -360,7 +373,8 @@ private String getAttributeScope() {
}

private String getAttributeInput() {
final ComboBoxItemData selectedAttributeInput = (ComboBoxItemData) inputComboBox.getSelectedItem();
final ComboBoxItemData selectedAttributeInput =
(ComboBoxItemData) inputComboBox.getSelectedItem();

if (selectedAttributeInput != null) {
return selectedAttributeInput.getText().trim();
Expand Down
Loading