Completed
Pull Request — develop (#169)
by Robbie
18:02 queued 06:04
created

ViewCommand::getTableInput()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 29
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 29
rs 8.8571
cc 3
eloc 22
nc 2
nop 1
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\InputOption;
8
use Symfony\Component\Console\Input\InputInterface;
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) { return $value ? 'yes' : 'no'; }),
74
            'AttributeModel' => array('Attribute-Model'),
75
            'BackendModel'   => array('Backend-Model'),
76
            'BackendTable'   => array('Backend-Table'),
77
            'BackendType'    => array('Backend-Type'),
78
            'SourceModel'    => array('Source-Model'),
79
            'CacheIdTags'    => array('Cache-ID-Tags', function($values) { return implode(',', (array)$values); }),
80
            'CacheTags'      => array('Cache-Tags', function($values) { return implode(',', (array)$values); }),
81
            'DefaultValue'   => array('Default-Value'),
82
            'FlatColumns'    => array('Flat-Columns', function($values) { return implode(',', array_keys((array)$values)); }),
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 126 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
83
            'FlatIndexes'    => array('Flat-Indexes', function($values) { return implode(',', array_keys((array)$values)); })
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 125 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
84
        );
85
86
87
        if ($isFrontend) {
88
            $table['Frontend/Label']              = array('Frontend-Label');
89
            $table['Frontend/Class']              = array('Frontend-Class');
90
            $table['Frontend/InputType']          = array('Frontend-Input-Type');
91
            $table['Frontend/InputRendererClass'] = array('Frontend-Input-Renderer-Class');
92
        }
93
94
        return $table;
95
    }
96
97
    /**
98
     * Given an attribute and an input data table, construct the output table and call
99
     * the formatting callbacks if necessary
100
     * @param  Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute
101
     * @return array
102
     */
103
    private function getTable($attribute)
104
    {
105
        $table = array();
106
107
        foreach ($this->getTableInput($attribute->getFrontend()) as $code => $info) {
108
            $label    = array_shift($info);
109
            $callback = is_array($info) ? array_shift($info) : null;
110
111
            // Support nested getters
112
            $levels = explode('/', $code);
113
            $value  = $attribute;
114
            foreach ($levels as $level) {
115
                $value = $value->{'get' . $level}();
116
            }
117
118
            // Optional formatting callback
119
            $value = is_callable($callback) ? $callback($value) : $value;
120
            
121
            if ($value === []) {
122
                $value = '';
123
            }
124
125
            $table[] = array($label, trim($value));
126
        }
127
128
        return $table;
129
    }
130
}
131