Completed
Push — develop ( f3a4fe...df0b60 )
by Tom
03:55
created

RemoveCommand::execute()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 39
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 8.439
c 0
b 0
f 0
cc 6
eloc 25
nc 6
nop 2
1
<?php
2
3
namespace N98\Magento\Command\Eav\Attribute;
4
5
use Magento\Eav\Model\Config as EavConfig;
6
use Magento\Eav\Setup\EavSetup;
7
use Magento\Eav\Setup\EavSetupFactory;
8
use N98\Magento\Command\AbstractMagentoCommand;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
class RemoveCommand extends AbstractMagentoCommand
14
{
15
    /**
16
     * @var EavConfig
17
     */
18
    private $eavConfig;
19
20
    /**
21
     * @var EavSetupFactory
22
     */
23
    private $eavSetupFactory;
24
25
    /**
26
     * @param EavConfig $eavConfig
27
     * @param EavSetupFactory $eavSetupFactory
28
     * @return void
29
     */
30
    public function inject(
31
        EavConfig $eavConfig,
32
        EavSetupFactory $eavSetupFactory
33
    ) {
34
        $this->eavConfig = $eavConfig;
35
        $this->eavSetupFactory = $eavSetupFactory;
36
    }
37
38
    /**
39
     * @return void
40
     */
41
    protected function configure()
42
    {
43
        $this
44
            ->setName('eav:attribute:remove')
45
            ->addArgument(
46
                'entityType',
47
                InputArgument::REQUIRED,
48
                'Entity Type Code, e.g. catalog_product'
49
            )
50
            ->addArgument(
51
                'attributeCode',
52
                InputArgument::REQUIRED | InputArgument::IS_ARRAY,
53
                'Attribute Code'
54
            )
55
            ->setDescription('Remove attribute for a given attribute code');
56
    }
57
58
    /**
59
     * @param InputInterface $input
60
     * @param OutputInterface $output
61
     * @return int|void
62
     */
63
    protected function execute(InputInterface $input, OutputInterface $output)
64
    {
65
        $this->detectMagento($output);
66
        if (!$this->initMagento()) {
67
            return;
68
        }
69
70
        if ($this->runsInProductionMode($input, $output)) {
71
            $output->writeln('This command is not available in production mode');
72
            return;
73
        }
74
75
        $entityType = $input->getArgument('entityType');
76
77
        try {
78
            $existingAttributeCodes = $this->eavConfig->getEntityAttributeCodes($entityType);
79
        } catch (\Exception $e) {
80
            throw new \InvalidArgumentException($e->getMessage());
81
        }
82
83
        /** @var EavSetup $eavSetup */
84
        $eavSetup = $this->eavSetupFactory->create();
85
        foreach ($input->getArgument('attributeCode') as $attributeCode) {
86
            if (!in_array($attributeCode, $existingAttributeCodes)) {
87
                $output->writeln(sprintf(
88
                    '<comment>Attribute "%s" does not exist for entity type "%s", skipped</comment>',
89
                    $attributeCode,
90
                    $entityType
91
                ));
92
            } else {
93
                $eavSetup->removeAttribute($entityType, $attributeCode);
94
                $output->writeln(sprintf(
95
                    '<info>Successfully removed attribute "%s" from entity type "%s"</info>',
96
                    $attributeCode,
97
                    $entityType
98
                ));
99
            }
100
        }
101
    }
102
}
103