Completed
Pull Request — master (#177)
by
unknown
04:33
created

src/N98/Magento/Command/Customer/InfoCommand.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace N98\Magento\Command\Customer;
4
5
use Magento\Customer\Model\Attribute as CustomerAttribute;
6
use Magento\Customer\Model\Resource\Customer;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
class InfoCommand extends AbstractCustomerCommand
12
{
13
    /**
14
     * @var array
15
     */
16
    protected $blacklist = array(
17
        'password_hash',
18
        'increment_id',
19
    );
20
21
    protected function configure()
22
    {
23
        $this
24
            ->setName('customer:info')
25
            ->addArgument('email', InputArgument::OPTIONAL, 'Email')
26
            ->addArgument('website', InputArgument::OPTIONAL, 'Website of the customer')
27
            ->setDescription('Loads basic customer info by email address.');
28
    }
29
30
    /**
31
     * @param \Symfony\Component\Console\Input\InputInterface $input
32
     * @param \Symfony\Component\Console\Output\OutputInterface $output
33
     *
34
     * @return int|void
35
     */
36
    protected function execute(InputInterface $input, OutputInterface $output)
37
    {
38
        // Detect and Init Magento
39
        $this->detectMagento($output, true);
40
        if (!$this->initMagento()) {
41
            return;
42
        }
43
44
        // Detect customer
45
        $customer = $this->detectCustomer($input, $output);
46
        if ($customer === null) {
47
            return;
48
        }
49
50
        /** @var Customer $customerResource */
51
        $customerResource = $customer->getResource();
52
53
        // Prepare Table Data
54
        $table = [];
55
        foreach ($customer->toArray() as $key => $value) {
56
            if (in_array($key, $this->blacklist)) {
57
                continue;
58
            }
59
            try {
60
                $attribute = $customerResource->getAttribute($key);
61
62
                if (!$attribute instanceof CustomerAttribute) {
63
                    $table[] = [$key, $key, $value];
64
                    continue;
65
                }
66
67
                /** @var \Magento\Eav\Model\Entity\Attribute\Frontend\DefaultFrontend $attributeFrontend */
68
                $attributeFrontend = $attribute->getFrontend();
69
70
                $tableLabel = $attributeFrontend->getLabel();
71
                $tableValue = $value;
72
73
                // @todo mwr temporary work around due to getValue throwing notice within getOptionText (array keys !== store_ids)
0 ignored issues
show
This line exceeds maximum limit of 120 characters; contains 130 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...
74
                if ($key != 'store_id') {
75
                    $tableValue = $attributeFrontend->getValue($customer);
76
                    if ($tableValue != $value) {
77
                        $tableValue .= " [$value]";
78
                    }
79
                }
80
                $table[] = [$key, $tableLabel, $tableValue];
81
            } catch (\Exception $e) {
82
                $table[] = [$key, $key, $value];
83
            }
84
        }
85
86
        // Render Table
87
        $helperTable = $this->getHelper('table');
88
        $helperTable->setHeaders(['Code', 'Name', 'Value']);
89
        $helperTable->setRows($table);
90
        $helperTable->render($output);
91
    }
92
93
    /**
94
     * @param InputInterface $input
95
     * @param OutputInterface $output
96
     *
97
     * @return \Magento\Customer\Model\Customer|void
98
     */
99
    protected function detectCustomer(InputInterface $input, OutputInterface $output)
100
    {
101
        $helperParameter = $this->getHelper('parameter');
102
        $email = $helperParameter->askEmail($input, $output);
103
        $website = $helperParameter->askWebsite($input, $output);
104
105
        $customer = $this->getCustomer();
106
        $customer->setWebsiteId($website->getId());
107
        $customer->loadByEmail($email);
108
        $customerId = $customer->getId();
109
        if ($customerId <= 0) {
110
            $output->writeln('<error>Customer was not found</error>');
111
112
            return null;
113
        }
114
115
        $customer->load($customerId);
116
117
        return $customer;
118
    }
119
}
120