Completed
Push — master ( 75ad7f...4e986c )
by Tim
14s
created

CustomerAddressObserver::loadCustomerAddress()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * TechDivision\Import\Customer\Address\Observers\CustomerAddressObserver
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-address
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Customer\Address\Observers;
22
23
use TechDivision\Import\Customer\Address\Utils\ColumnKeys;
24
use TechDivision\Import\Customer\Address\Utils\MemberNames;
25
use TechDivision\Import\Customer\Address\Services\CustomerAddressBunchProcessorInterface;
26
27
/**
28
 * Observer that create's the customer address itself.
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-address
34
 * @link      http://www.techdivision.com
35
 */
36
class CustomerAddressObserver extends AbstractCustomerAddressImportObserver
37
{
38
39
    /**
40
     * The customer address bunch processor instance.
41
     *
42
     * @var \TechDivision\Import\Customer\Address\Services\CustomerAddressBunchProcessorInterface
43
     */
44
    protected $customerAddressBunchProcessor;
45
46
    /**
47
     * Initialize the observer with the passed customer bunch processor instance.
48
     *
49
     * @param \TechDivision\Import\Customer\Address\Services\CustomerAddressBunchProcessorInterface $customerAddressBunchProcessor The customer address bunch processor instance
50
     */
51
    public function __construct(CustomerAddressBunchProcessorInterface $customerAddressBunchProcessor)
52
    {
53
        $this->customerAddressBunchProcessor = $customerAddressBunchProcessor;
54
    }
55
56
    /**
57
     * Return's the customer address bunch processor instance.
58
     *
59
     * @return \TechDivision\Import\Customer\Address\Services\CustomerAddressBunchProcessorInterface The customer address bunch processor instance
60
     */
61
    protected function getCustomerAddressBunchProcessor()
62
    {
63
        return $this->customerAddressBunchProcessor;
64
    }
65
66
    /**
67
     * Process the observer's business logic.
68
     *
69
     * @return void
70
     */
71
    protected function process()
72
    {
73
74
        // prepare the static entity values
75
        $customerAddress = $this->initializeCustomerAddress($this->prepareAttributes());
76
77
        // insert the entity and set the entity ID
78
        $this->setLastEntityId($this->persistCustomerAddress($customerAddress));
79
    }
80
81
    /**
82
     * Prepare the attributes of the entity that has to be persisted.
83
     *
84
     * @return array The prepared attributes
85
     */
86
    protected function prepareAttributes()
87
    {
88
89
        // load the website ID for the given code
90
        $websiteId = $this->getStoreWebsiteIdByCode($this->getValue(ColumnKeys::WEBSITE));
91
92
        // load the customer
93
        $customer = $this->loadCustomerByEmailAndWebsiteId($this->getValue(ColumnKeys::EMAIL), $websiteId);
94
95
        // initialize the customer values
96
        $entityId = $this->getValue(ColumnKeys::ENTITY_ID);
97
        $city = $this->getValue(ColumnKeys::CITY);
98
        $company = $this->getValue(ColumnKeys::COMPANY);
99
        $countryId = $this->getValue(ColumnKeys::COUNTRY_ID);
100
        $fax = $this->getValue(ColumnKeys::FAX);
101
        $firstname = $this->getValue(ColumnKeys::FIRSTNAME);
102
        $lastname = $this->getValue(ColumnKeys::LASTNAME);
103
        $middlename = $this->getValue(ColumnKeys::MIDDLENAME);
104
        $postcode = $this->getValue(ColumnKeys::POSTCODE);
105
        $prefix = $this->getValue(ColumnKeys::PREFIX);
106
        $region = $this->getValue(ColumnKeys::REGION);
107
        $regionId = $this->getValue(ColumnKeys::REGION_ID);
108
        $street = $this->getValue(ColumnKeys::STREET);
109
        $suffix = $this->getValue(ColumnKeys::SUFFIX);
110
        $telephone = $this->getValue(ColumnKeys::TELEPHONE);
111
        $vatId = $this->getValue(ColumnKeys::VAT_ID);
112
        $vatIsValid = $this->getValue(ColumnKeys::VAT_IS_VALID);
113
        $vatRequestId = $this->getValue(ColumnKeys::VAT_REQUEST_ID);
114
        $vatRequestSuccess = $this->getValue(ColumnKeys::VAT_REQUEST_SUCCESS);
115
116
        // load the customer's addtional attributes
117
        $incrementId = null;
118
        $isActive = 1;
119
120
        // prepare the date format for the created at/updated at dates
121
        $createdAt = $this->getValue(ColumnKeys::CREATED_AT, date('Y-m-d H:i:s'), array($this, 'formatDate'));
122
        $updatedAt = $this->getValue(ColumnKeys::UPDATED_AT, date('Y-m-d H:i:s'), array($this, 'formatDate'));
123
        $vatRequestDate = $this->getValue(ColumnKeys::VAT_REQUEST_DATE, date('Y-m-d H:i:s'), array($this, 'formatDate'));
124
125
        // return the prepared customer
126
        return $this->initializeEntity(
127
            array(
128
                MemberNames::ENTITY_ID           => $entityId,
129
                MemberNames::INCREMENT_ID        => $incrementId,
130
                MemberNames::PARENT_ID           => $customer[MemberNames::ENTITY_ID],
131
                MemberNames::CREATED_AT          => $createdAt,
132
                MemberNames::UPDATED_AT          => $updatedAt,
133
                MemberNames::IS_ACTIVE           => $isActive,
134
                MemberNames::CITY                => $city,
135
                MemberNames::COMPANY             => $company,
136
                MemberNames::COUNTRY_ID          => $countryId,
137
                MemberNames::FAX                 => $fax,
138
                MemberNames::FIRSTNAME           => $firstname,
139
                MemberNames::LASTNAME            => $lastname,
140
                MemberNames::MIDDLENAME          => $middlename,
141
                MemberNames::POSTCODE            => $postcode,
142
                MemberNames::PREFIX              => $prefix,
143
                MemberNames::REGION              => $region,
144
                MemberNames::REGION_ID           => $regionId,
145
                MemberNames::STREET              => $street,
146
                MemberNames::SUFFIX              => $suffix,
147
                MemberNames::TELEPHONE           => $telephone,
148
                MemberNames::VAT_ID              => $vatId,
149
                MemberNames::VAT_IS_VALID        => $vatIsValid,
150
                MemberNames::VAT_REQUEST_DATE    => $vatRequestDate,
151
                MemberNames::VAT_REQUEST_ID      => $vatRequestId,
152
                MemberNames::VAT_REQUEST_SUCCESS => $vatRequestSuccess
153
            )
154
        );
155
    }
156
157
    /**
158
     * Initialize the customer address with the passed attributes and returns an instance.
159
     *
160
     * @param array $attr The customer address attributes
161
     *
162
     * @return array The initialized customer address
163
     */
164
    protected function initializeCustomerAddress(array $attr)
165
    {
166
167
        // try to load the customer address with the given entity ID
168
        if ($entity = $this->loadCustomerAddress($attr[MemberNames::ENTITY_ID])) {
169
            return $this->mergeEntity($entity, $attr);
170
        }
171
172
        // remove the entity ID
173
        unset($attr[MemberNames::ENTITY_ID]);
174
175
        // simply return the attributes
176
        return $attr;
177
    }
178
179
    /**
180
     * Return's the customer with the passed entity ID.
181
     *
182
     * @param integer $id The entity ID of the customer to return
183
     *
184
     * @return array|null The customer
185
     */
186
    protected function loadCustomerAddress($id)
187
    {
188
        return $this->getCustomerAddressBunchProcessor()->loadCustomerAddress($id);
189
    }
190
191
    /**
192
     * Return's the customer with the passed email and website ID.
193
     *
194
     * @param string $email     The email of the customer to return
195
     * @param string $websiteId The website ID of the customer to return
196
     *
197
     * @return array|null The customer
198
     */
199
    protected function loadCustomerByEmailAndWebsiteId($email, $websiteId)
200
    {
201
        return $this->getCustomerAddressBunchProcessor()->loadCustomerByEmailAndWebsiteId($email, $websiteId);
202
    }
203
204
    /**
205
     * Persist's the passed customer address data and return's the ID.
206
     *
207
     * @param array $customerAddress The customer address data to persist
208
     *
209
     * @return string The ID of the persisted entity
210
     */
211
    protected function persistCustomerAddress($customerAddress)
212
    {
213
        return $this->getCustomerAddressBunchProcessor()->persistCustomerAddress($customerAddress);
214
    }
215
216
    /**
217
     * Set's the ID of the customer that has been created recently.
218
     *
219
     * @param string $lastEntityId The entity ID
220
     *
221
     * @return void
222
     */
223
    protected function setLastEntityId($lastEntityId)
224
    {
225
        $this->getSubject()->setLastEntityId($lastEntityId);
226
    }
227
228
    /**
229
     * Return's the store website for the passed code.
230
     *
231
     * @param string $code The code of the store website to return the ID for
232
     *
233
     * @return integer The store website ID
234
     * @throws \Exception Is thrown, if the store website with the requested code is not available
235
     */
236
    protected function getStoreWebsiteIdByCode($code)
237
    {
238
        return $this->getSubject()->getStoreWebsiteIdByCode($code);
239
    }
240
}
241