LastEntityIdObserver   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 22
c 1
b 0
f 0
dl 0
loc 115
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getStoreWebsiteIdByCode() 0 3 1
A setLastEntityId() 0 3 1
A process() 0 28 4
A __construct() 0 3 1
A loadCustomerByEmailAndWebsiteId() 0 3 1
A setIds() 0 3 1
A getCustomerBunchProcessor() 0 3 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Customer\Observers\LastEntityIdObserver
5
 *
6
 * PHP version 7
7
 *
8
 * @author    Tim Wagner <[email protected]>
9
 * @copyright 2018 TechDivision GmbH <[email protected]>
10
 * @license   https://opensource.org/licenses/MIT
11
 * @link      https://github.com/techdivision/import-customer
12
 * @link      http://www.techdivision.com
13
 */
14
15
namespace TechDivision\Import\Customer\Observers;
16
17
use TechDivision\Import\Customer\Utils\ColumnKeys;
18
use TechDivision\Import\Customer\Utils\MemberNames;
19
use TechDivision\Import\Customer\Services\CustomerBunchProcessorInterface;
20
21
/**
22
 * Observer that pre-loads the entity ID of the customer with the identifier found in the CSV file.
23
 *
24
 * @author    Tim Wagner <[email protected]>
25
 * @copyright 2018 TechDivision GmbH <[email protected]>
26
 * @license   https://opensource.org/licenses/MIT
27
 * @link      https://github.com/techdivision/import-customer
28
 * @link      http://www.techdivision.com
29
 */
30
class LastEntityIdObserver extends AbstractCustomerImportObserver
31
{
32
33
    /**
34
     * The customer bunch processor instance.
35
     *
36
     * @var \TechDivision\Import\Customer\Services\CustomerBunchProcessorInterface
37
     */
38
    protected $customerBunchProcessor;
39
40
    /**
41
     * Initialize the observer with the passed customer bunch processor instance.
42
     *
43
     * @param \TechDivision\Import\Customer\Services\CustomerBunchProcessorInterface $customerBunchProcessor The customer bunch processor instance
44
     */
45
    public function __construct(CustomerBunchProcessorInterface $customerBunchProcessor)
46
    {
47
        $this->customerBunchProcessor = $customerBunchProcessor;
48
    }
49
50
    /**
51
     * Return's the customer bunch processor instance.
52
     *
53
     * @return \TechDivision\Import\Customer\Services\CustomerBunchProcessorInterface The customer bunch processor instance
54
     */
55
    protected function getCustomerBunchProcessor()
56
    {
57
        return $this->customerBunchProcessor;
58
    }
59
60
    /**
61
     * Process the observer's business logic.
62
     *
63
     * @return array The processed row
64
     * @throws \Exception Is thrown, if the customer with the SKU can not be loaded
65
     */
66
    protected function process()
67
    {
68
69
        // load email and website code
70
        $email = $this->getValue(ColumnKeys::EMAIL);
71
        $website = $this->getValue(ColumnKeys::WEBSITE);
72
73
        // query whether or not, we've found a new identifier => means we've found a new customer
74
        if ($this->isLastIdentifier(array($email, $website))) {
75
            return;
76
        }
77
78
        // set the entity ID for the customer with the passed SKU
79
        if ($customer = $this->loadCustomerByEmailAndWebsiteId($email, $this->getStoreWebsiteIdByCode($website))) {
80
            $this->setIds($customer);
81
        } else {
82
            // initialize the error message
83
            $message = sprintf('Can\'t load entity ID for customer with email "%s" and website "%s"', $email, $website);
84
            // load the subject
85
            $subject = $this->getSubject();
86
            // query whether or not debug mode has been enabled
87
            if ($subject->isDebugMode()) {
88
                // log a warning, that the customer with the given SKU can not be loaded
89
                $subject->getSystemLogger()->warning($subject->appendExceptionSuffix($message));
90
                // skip processing the actual row
91
                $subject->skipRow();
92
            } else {
93
                throw new \Exception($message);
94
            }
95
        }
96
    }
97
98
    /**
99
     * Temporarily persist's the IDs of the passed customer.
100
     *
101
     * @param array $customer The customer to temporarily persist the IDs for
102
     *
103
     * @return void
104
     */
105
    protected function setIds(array $customer)
106
    {
107
        $this->setLastEntityId($customer[MemberNames::ENTITY_ID]);
108
    }
109
110
    /**
111
     * Set's the ID of the customer that has been created recently.
112
     *
113
     * @param string $lastEntityId The entity ID
114
     *
115
     * @return void
116
     */
117
    protected function setLastEntityId($lastEntityId)
118
    {
119
        $this->getSubject()->setLastEntityId($lastEntityId);
120
    }
121
122
    /**
123
     * Return's the customer with the passed email and website ID.
124
     *
125
     * @param string $email     The email of the customer to return
126
     * @param string $websiteId The website ID of the customer to return
127
     *
128
     * @return array|null The customer
129
     */
130
    protected function loadCustomerByEmailAndWebsiteId($email, $websiteId)
131
    {
132
        return $this->getCustomerBunchProcessor()->loadCustomerByEmailAndWebsiteId($email, $websiteId);
133
    }
134
135
    /**
136
     * Return's the store website for the passed code.
137
     *
138
     * @param string $code The code of the store website to return the ID for
139
     *
140
     * @return integer The store website ID
141
     */
142
    protected function getStoreWebsiteIdByCode($code)
143
    {
144
        return $this->getSubject()->getStoreWebsiteIdByCode($code);
145
    }
146
}
147