Passed
Push — 14.x ( 542410 )
by Tim
04:31
created

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