Passed
Pull Request — master (#1)
by Tim
02:51
created

loadCustomerByEmailAndWebsiteId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Customer\Observers\PreLoadEntityIdObserver
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2018 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import-customer
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Customer\Observers;
22
23
use TechDivision\Import\Customer\Utils\ColumnKeys;
24
use TechDivision\Import\Customer\Services\CustomerBunchProcessorInterface;
25
26
/**
27
 * Observer that pre-loads the entity ID of the customer with the identifier found in the CSV file.
28
 *
29
 * @author    Tim Wagner <[email protected]>
30
 * @copyright 2018 TechDivision GmbH <[email protected]>
31
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
32
 * @link      https://github.com/techdivision/import-customer
33
 * @link      http://www.techdivision.com
34
 */
35
class PreLoadEntityIdObserver extends AbstractCustomerImportObserver
36
{
37
38
    /**
39
     * The customer bunch processor instance.
40
     *
41
     * @var \TechDivision\Import\Customer\Services\CustomerBunchProcessorInterface
42
     */
43
    protected $customerBunchProcessor;
44
45
    /**
46
     * Initialize the observer with the passed customer bunch processor instance.
47
     *
48
     * @param \TechDivision\Import\Customer\Services\CustomerBunchProcessorInterface $customerBunchProcessor The customer bunch processor instance
49
     */
50
    public function __construct(CustomerBunchProcessorInterface $customerBunchProcessor)
51
    {
52
        $this->customerBunchProcessor = $customerBunchProcessor;
53
    }
54
55
    /**
56
     * Return's the customer bunch processor instance.
57
     *
58
     * @return \TechDivision\Import\Customer\Services\CustomerBunchProcessorInterface The customer bunch processor instance
59
     */
60
    protected function getCustomerBunchProcessor()
61
    {
62
        return $this->customerBunchProcessor;
63
    }
64
65
    /**
66
     * Process the observer's business logic.
67
     *
68
     * @return array The processed row
69
     * @throws \Exception Is thrown, if the customer with the SKU can not be loaded
70
     */
71
    protected function process()
72
    {
73
74
        // load email and website code
75
        $email = $this->getValue(ColumnKeys::EMAIL);
76
        $website = $this->getValue(ColumnKeys::WEBSITE);
77
78
        // query whether or not, we've found a new identifier => means we've found a new customer
79
        if ($this->isLastIdentifier(array($email, $website))) {
80
            return;
81
        }
82
83
        // preserve the entity ID for the customer with the passed identifier
84
        if ($customer = $this->loadCustomerByEmailAndWebsiteId($email, $website)) {
85
            $this->preLoadEntityId($customer);
86
        } else {
87
            // initialize the error message
88
            $message = sprintf('Can\'t pre-load customer with email "%s" and website "%s"', $email, $website);
89
            // load the subject
90
            $subject = $this->getSubject();
91
            // query whether or not debug mode has been enabled
92
            if ($subject->isDebugMode()) {
93
                $subject->getSystemLogger()->warning($subject->appendExceptionSuffix($message));
94
            } else {
95
                throw new \Exception($message);
96
            }
97
        }
98
    }
99
100
    /**
101
     * Pre-load the entity ID for the passed customer.
102
     *
103
     * @param array $customer The customer to be pre-loaded
104
     *
105
     * @return void
106
     */
107
    protected function preLoadEntityId(array $customer)
108
    {
109
        $this->getSubject()->preLoadEntityId($customer);
110
    }
111
112
    /**
113
     * Return's the customer with the passed email and website ID.
114
     *
115
     * @param string $email     The email of the customer to return
116
     * @param string $websiteId The website ID of the customer to return
117
     *
118
     * @return array|null The customer
119
     */
120
    protected function loadCustomerByEmailAndWebsiteId($email, $websiteId)
121
    {
122
        return $this->getCustomerBunchProcessor()->loadCustomerByEmailAndWebsiteId($email, $websiteId);
123
    }
124
}
125