Completed
Push — master ( 6764cb...4e8215 )
by Christian
03:24 queued 11s
created

InfoCommand::detectCustomer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace N98\Magento\Command\Customer;
4
5
use Exception;
6
use Magento\Customer\Model\Attribute as CustomerAttribute;
7
use Magento\Customer\Model\Resource\Customer;
8
use Magento\Eav\Model\Entity\Attribute\Frontend\DefaultFrontend;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
class InfoCommand extends AbstractCustomerCommand
14
{
15
    /**
16
     * @var array
17
     */
18
    protected $blacklist = array(
19
        'password_hash',
20
        'increment_id',
21
    );
22
23
    protected function configure()
24
    {
25
        $this
26
            ->setName('customer:info')
27
            ->addArgument('email', InputArgument::OPTIONAL, 'Email')
28
            ->addArgument('website', InputArgument::OPTIONAL, 'Website of the customer')
29
            ->setDescription('Loads basic customer info by email address.');
30
    }
31
32
    /**
33
     * @param InputInterface $input
34
     * @param OutputInterface $output
35
     *
36
     * @return int|void
37
     * @throws Exception
38
     */
39
    protected function execute(InputInterface $input, OutputInterface $output)
40
    {
41
        // Detect and Init Magento
42
        $this->detectMagento($output, true);
43
        if (!$this->initMagento()) {
44
            return;
45
        }
46
47
        // Detect customer
48
        $customer = $this->detectCustomer($input, $output);
49
        if ($customer === null) {
50
            return;
51
        }
52
53
        /** @var Customer $customerResource */
54
        $customerResource = $customer->getResource();
55
56
        // Prepare Table Data
57
        $table = [];
58
        foreach ($customer->toArray() as $key => $value) {
59
            if (in_array($key, $this->blacklist)) {
60
                continue;
61
            }
62
            try {
63
                $attribute = $customerResource->getAttribute($key);
64
65
                if (!$attribute instanceof CustomerAttribute) {
0 ignored issues
show
Bug introduced by
The class Magento\Customer\Model\Attribute does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
66
                    $table[] = [$key, $key, $value];
67
                    continue;
68
                }
69
70
                /** @var DefaultFrontend $attributeFrontend */
71
                $attributeFrontend = $attribute->getFrontend();
72
73
                $tableLabel = $attributeFrontend->getLabel();
74
                $tableValue = $value;
75
76
                // @todo mwr temporary work around due to getValue throwing notice within getOptionText
77
                // (array keys !== store_ids)
78
                if ($key != 'store_id') {
79
                    $tableValue = $attributeFrontend->getValue($customer);
80
                    if ($tableValue != $value) {
81
                        $tableValue .= " [$value]";
82
                    }
83
                }
84
                $table[] = [$key, $tableLabel, $tableValue];
85
            } catch (Exception $e) {
86
                $table[] = [$key, $key, $value];
87
            }
88
        }
89
90
        // Render Table
91
        $helperTable = $this->getHelper('table');
92
        $helperTable->setHeaders(['Code', 'Name', 'Value']);
93
        $helperTable->setRows($table);
94
        $helperTable->render($output);
95
    }
96
}
97