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

InfoCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 8
rs 9.4285
c 1
b 0
f 1
cc 1
eloc 6
nc 1
nop 0
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) {
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...
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
Coding Style introduced by
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