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

AbstractCustomerCommand::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 Magento\Customer\Model\Customer;
6
use Magento\Customer\Model\Resource\Customer\Collection as CustomerCollection;
7
use N98\Magento\Command\AbstractMagentoCommand;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
class AbstractCustomerCommand extends AbstractMagentoCommand
12
{
13
    /**
14
     * @return array
15
     */
16
    protected function getCustomerList($search = null)
17
    {
18
        $customerCollection = $this->getCustomerCollection();
19
20
        // Filter
21
        if ($search !== null) {
22
            $filter = [
23
                ['attribute' => 'email', 'like' => '%' . $search . '%'],
24
                ['attribute' => 'firstname', 'like' => '%' . $search . '%'],
25
                ['attribute' => 'lastname', 'like' => '%' . $search . '%'],
26
            ];
27
            $customerCollection->addAttributeToFilter(
28
                $filter
29
            );
30
        }
31
32
        // Result
33
        $list = [];
34
        foreach ($customerCollection as $customer) {
35
            /* @var $customer Customer */
36
37
            $list[] = [
38
                'id'         => $customer->getId(),
39
                'firstname'  => $customer->getFirstname(),
40
                'lastname'   => $customer->getLastname(),
41
                'email'      => $customer->getEmail(),
42
                'website'    => $customer->getWebsiteId(),
43
                'created_at' => $customer->getCreatedAt(),
44
            ];
45
        }
46
47
        return $list;
48
    }
49
50
    /**
51
     * @return Customer
52
     */
53
    protected function getCustomer()
54
    {
55
        return $this->getObjectManager()->create(Customer::class);
56
    }
57
58
    /**
59
     * @return CustomerCollection
60
     */
61
    protected function getCustomerCollection()
62
    {
63
        return $this->getCustomer()->getCollection();
64
    }
65
66
    /**
67
     * @param InputInterface $input
68
     * @param OutputInterface $output
69
     *
70
     * @return Customer|void
71
     */
72
    protected function detectCustomer(InputInterface $input, OutputInterface $output)
73
    {
74
        $helperParameter = $this->getHelper('parameter');
75
        $email = $helperParameter->askEmail($input, $output);
76
        $website = $helperParameter->askWebsite($input, $output);
77
78
        $customer = $this->getCustomer();
79
        $customer->setWebsiteId($website->getId());
80
        $customer->loadByEmail($email);
81
        $customerId = $customer->getId();
82
        if ($customerId <= 0) {
83
            $output->writeln('<error>Customer was not found</error>');
84
85
            return null;
86
        }
87
88
        $customer->load($customerId);
89
90
        return $customer;
91
    }
92
}
93