ViewCommand   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 4
Bugs 2 Features 1
Metric Value
wmc 13
c 4
b 2
f 1
lcom 1
cbo 3
dl 0
loc 136
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 15 1
A execute() 0 21 3
B getTable() 0 27 6
B getTableInput() 0 45 3
1
<?php
2
3
namespace N98\Magento\Command\Eav\Attribute;
4
5
use N98\Util\Console\Helper\Table\Renderer\RendererFactory;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
class ViewCommand extends AbstractAttributeCommand
12
{
13
    /**
14
     * Setup
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
            ->addOption(
23
                'format',
24
                null,
25
                InputOption::VALUE_OPTIONAL,
26
                'Output Format. One of [' . implode(',', RendererFactory::getFormats()) . ']'
27
            )
28
            ->setDescription('View information about an EAV attribute')
29
            ->setHelp('Enter an entity type code and an attribute code to see information about an EAV attribute.');
30
    }
31
32
    /**
33
     * @param  InputInterface $input
34
     * @param  OutputInterface $output
35
     * @return void
36
     * @throws InvalidArgumentException If the attribute doesn't exist
37
     */
38
    protected function execute(InputInterface $input, OutputInterface $output)
39
    {
40
        $this->detectMagento($output, true);
41
        if (!$this->initMagento()) {
42
            return;
43
        }
44
45
        $entityType = $input->getArgument('entityType');
46
        $attributeCode = $input->getArgument('attributeCode');
47
        $attribute = $this->getAttribute($entityType, $attributeCode);
48
        if (!$attribute->getId()) {
49
            throw new \InvalidArgumentException('Attribute was not found.');
50
        }
51
52
        $table = $this->getTable($attribute);
53
54
        $this
55
            ->getHelper('table')
56
            ->setHeaders(array('Type', 'Value'))
57
            ->renderByFormat($output, $table, $input->getOption('format'));
58
    }
59
60
    /**
61
     * Define the contents for the table. The key is the magic method name e.g.
62
     * get[Name](), and the value is an array containing first the label to display, then optionally
63
     * a callback for how to process the attribute value for display
64
     * @param  bool $isFrontend
65
     * @return array
66
     */
67
    public function getTableInput($isFrontend = false)
68
    {
69
        $table = array(
70
            'Id' => array('ID'),
71
            'Name' => array('Code'),
72
            'AttributeSetId' => array('Attribute-Set-ID'),
73
            'VisibleOnFront' => array('Visible-On-Front', function ($value) {
74
                return $value ? 'yes' : 'no';
75
            }),
76
            'AttributeModel' => array('Attribute-Model'),
77
            'BackendModel' => array('Backend-Model'),
78
            'BackendTable' => array('Backend-Table'),
79
            'BackendType' => array('Backend-Type'),
80
            'SourceModel' => array('Source-Model'),
81
            'CacheIdTags' => array('Cache-ID-Tags', function ($values) {
82
                return implode(',', (array) $values);
83
            }),
84
            'CacheTags' => array('Cache-Tags', function ($values) {
85
                return implode(',', (array) $values);
86
            }),
87
            'DefaultValue' => array('Default-Value'),
88
            'FlatColumns' => array(
89
                'Flat-Columns',
90
                function ($values) {
91
                    return implode(',', array_keys((array) $values));
92
                }
93
            ),
94
            'FlatIndexes' => array(
95
                'Flat-Indexes',
96
                function ($values) {
97
                    return implode(',', array_keys((array) $values));
98
                }
99
            )
100
        );
101
102
103
        if ($isFrontend) {
104
            $table['Frontend/Label'] = array('Frontend-Label');
105
            $table['Frontend/Class'] = array('Frontend-Class');
106
            $table['Frontend/InputType'] = array('Frontend-Input-Type');
107
            $table['Frontend/InputRendererClass'] = array('Frontend-Input-Renderer-Class');
108
        }
109
110
        return $table;
111
    }
112
113
    /**
114
     * Given an attribute and an input data table, construct the output table and call
115
     * the formatting callbacks if necessary
116
     * @param  Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute
117
     * @return array
118
     */
119
    private function getTable($attribute)
120
    {
121
        $table = array();
122
123
        foreach ($this->getTableInput($attribute->getFrontend()) as $code => $info) {
124
            $label = array_shift($info);
125
            $callback = is_array($info) ? array_shift($info) : null;
126
127
            // Support nested getters
128
            $levels = explode('/', $code);
129
            $value = $attribute;
130
            foreach ($levels as $level) {
131
                $value = $value->{'get' . $level}();
132
            }
133
134
            // Optional formatting callback
135
            $value = is_callable($callback) ? $callback($value) : $value;
136
137
            if ($value === array()) {
138
                $value = '';
139
            }
140
141
            $table[] = array($label, trim($value));
142
        }
143
144
        return $table;
145
    }
146
}
147