Completed
Push — develop ( 21139f...eaa140 )
by Tom
12s
created

ViewCommand::execute()   F

Complexity

Conditions 15
Paths 4098

Size

Total Lines 50
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 50
rs 2.6974
cc 15
eloc 37
nc 4098
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace N98\Magento\Command\Eav\Attribute;
4
5
use InvalidArgumentException;
6
use N98\Magento\Command\AbstractMagentoCommand;
7
use N98\Util\Console\Helper\Table\Renderer\RendererFactory;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Process\Exception\RuntimeException;
13
14
class ViewCommand extends AbstractMagentoCommand
15
{
16
    protected function configure()
17
    {
18
        $this
19
            ->setName('eav:attribute:view')
20
            ->addArgument('entityType', InputArgument::REQUIRED, 'Entity Type Code like catalog_product')
21
            ->addArgument('attributeCode', InputArgument::REQUIRED, 'Attribute Code')
22
            ->setDescription('View informations about an EAV attribute')
23
            ->addOption(
24
                'format',
25
                null,
26
                InputOption::VALUE_OPTIONAL,
27
                'Output Format. One of [' . implode(',', RendererFactory::getFormats()) . ']'
28
            );
29
    }
30
31
    /**
32
     * @param InputInterface  $input
33
     * @param OutputInterface $output
34
     *
35
     * @return int|void
36
     * @throws InvalidArgumentException
37
     */
38
    protected function execute(InputInterface $input, OutputInterface $output)
39
    {
40
        $this->detectMagento($output);
41
        if (!$this->initMagento()) {
42
            return;
43
        }
44
45
        $entityType = $input->getArgument('entityType');
46
        $attributeCode = $input->getArgument('attributeCode');
47
48
        $attribute = $this->getAttribute($entityType, $attributeCode);
49
        if (!$attribute) {
50
            throw new InvalidArgumentException('Attribute was not found.');
51
        }
52
53
        $table = array(
54
            array('ID', $attribute->getId()),
55
            array('Code', $attribute->getName()),
56
            array('Attribute-Set-ID', $attribute->getAttributeSetId()),
57
            array('Visible-On-Front', $attribute->getIsVisibleOnFront() ? 'yes' : 'no'),
58
            array('Attribute-Model', $attribute->getAttributeModel() ? $attribute->getAttributeModel() : ''),
59
            array('Backend-Model', $attribute->getBackendModel() ? $attribute->getBackendModel() : ''),
60
            array('Backend-Table', $attribute->getBackendTable() ? $attribute->getBackendTable() : ''),
61
            array('Backend-Type', $attribute->getBackendType() ? $attribute->getBackendType() : ''),
62
            array('Source-Model', $attribute->getSourceModel() ? $attribute->getSourceModel() : ''),
63
            array('Cache-ID-Tags', $attribute->getCacheIdTags() ? implode(',', $attribute->getCacheIdTags()) : ''),
64
            array('Cache-Tags', $attribute->getCacheTags() ? implode(',', $attribute->getCacheTags()) : ''),
65
            array('Default-Value', $attribute->getDefaultValue() ? $attribute->getDefaultValue() : ''),
66
            array(
67
                'Flat-Columns',
68
                $attribute->getFlatColumns() ? implode(',', array_keys($attribute->getFlatColumns())) : '',
69
            ),
70
            array('Flat-Indexes', $attribute->getFlatIndexes() ? implode(',', $attribute->getFlatIndexes()) : ''),
71
        );
72
73
        if ($attribute->getFrontend()) {
74
            $table[] = array('Frontend-Label', $attribute->getFrontend()->getLabel());
75
            $table[] = array('Frontend-Class', trim($attribute->getFrontend()->getClass()));
76
            $table[] = array('Frontend-Input', trim($attribute->getFrontend()->getInputType()));
77
            $table[] = array(
78
                'Frontend-Input-Renderer-Class',
79
                trim($attribute->getFrontend()->getInputRendererClass()),
80
            );
81
        }
82
83
        $this
84
            ->getHelper('table')
85
            ->setHeaders(array('Type', 'Value'))
86
            ->renderByFormat($output, $table, $input->getOption('format'));
87
    }
88
89
    /**
90
     * @param string $entityType
91
     * @param string $attributeCode
92
     *
93
     * @return \Mage_Eav_Model_Entity_Attribute_Abstract|false
94
     */
95
    protected function getAttribute($entityType, $attributeCode)
96
    {
97
        return \Mage::getModel('eav/config')->getAttribute($entityType, $attributeCode);
98
    }
99
}
100