Passed
Push — master ( feb03b...b59be3 )
by
unknown
11:32 queued 07:44
created

loadCustomerAddressByIncrementId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Customer\Address\Observers\CustomerAddressObserver
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-address
12
 * @link      http://www.techdivision.com
13
 */
14
15
namespace TechDivision\Import\Customer\Address\Observers;
16
17
use TechDivision\Import\Customer\Address\Utils\ColumnKeys;
18
use TechDivision\Import\Customer\Address\Utils\MemberNames;
19
use TechDivision\Import\Customer\Address\Services\CustomerAddressBunchProcessorInterface;
20
21
/**
22
 * Observer that create's the customer address itself.
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-address
28
 * @link      http://www.techdivision.com
29
 */
30
class CustomerAddressObserver extends AbstractCustomerAddressImportObserver
31
{
32
33
    /**
34
     * The customer address bunch processor instance.
35
     *
36
     * @var \TechDivision\Import\Customer\Address\Services\CustomerAddressBunchProcessorInterface
37
     */
38
    protected $customerAddressBunchProcessor;
39
40
    /**
41
     * Initialize the observer with the passed customer bunch processor instance.
42
     *
43
     * @param \TechDivision\Import\Customer\Address\Services\CustomerAddressBunchProcessorInterface $customerAddressBunchProcessor The customer address bunch processor instance
44
     */
45
    public function __construct(CustomerAddressBunchProcessorInterface $customerAddressBunchProcessor)
46
    {
47
        $this->customerAddressBunchProcessor = $customerAddressBunchProcessor;
48
    }
49
50
    /**
51
     * Return's the customer address bunch processor instance.
52
     *
53
     * @return \TechDivision\Import\Customer\Address\Services\CustomerAddressBunchProcessorInterface The customer address bunch processor instance
54
     */
55
    protected function getCustomerAddressBunchProcessor()
56
    {
57
        return $this->customerAddressBunchProcessor;
58
    }
59
60
    /**
61
     * Process the observer's business logic.
62
     *
63
     * @return void
64
     */
65
    protected function process()
66
    {
67
68
        // prepare the static entity values
69
        $customerAddress = $this->initializeCustomerAddress($this->prepareAttributes());
70
71
        // insert the entity and set the entity ID
72
        $this->setLastEntityId($this->persistCustomerAddress($customerAddress));
73
    }
74
75
    /**
76
     * Prepare the attributes of the entity that has to be persisted.
77
     *
78
     * @return array The prepared attributes
79
     */
80
    protected function prepareAttributes()
81
    {
82
83
        // load the website ID for the given code
84
        $websiteId = $this->getStoreWebsiteIdByCode($this->getValue(ColumnKeys::WEBSITE));
85
86
        // load the customer
87
        $customer = $this->loadCustomerByEmailAndWebsiteId($this->getValue(ColumnKeys::EMAIL), $websiteId);
88
89
        // initialize the customer values
90
        $entityId = $this->getValue(ColumnKeys::ENTITY_ID);
91
        $city = $this->getValue(ColumnKeys::CITY);
92
        $company = $this->getValue(ColumnKeys::COMPANY);
93
        $countryId = $this->getValue(ColumnKeys::COUNTRY_ID);
94
        $fax = $this->getValue(ColumnKeys::FAX);
95
        $firstname = $this->getValue(ColumnKeys::FIRSTNAME);
96
        $lastname = $this->getValue(ColumnKeys::LASTNAME);
97
        $middlename = $this->getValue(ColumnKeys::MIDDLENAME);
98
        $postcode = $this->getValue(ColumnKeys::POSTCODE);
99
        $prefix = $this->getValue(ColumnKeys::PREFIX);
100
        $region = $this->getValue(ColumnKeys::REGION);
101
        $regionId = $this->getValue(ColumnKeys::REGION_ID);
102
        $street = $this->getValue(ColumnKeys::STREET);
103
        $suffix = $this->getValue(ColumnKeys::SUFFIX);
104
        $telephone = $this->getValue(ColumnKeys::TELEPHONE);
105
        $vatId = $this->getValue(ColumnKeys::VAT_ID);
106
        $vatIsValid = $this->getValue(ColumnKeys::VAT_IS_VALID);
107
        $vatRequestId = $this->getValue(ColumnKeys::VAT_REQUEST_ID);
108
        $vatRequestSuccess = $this->getValue(ColumnKeys::VAT_REQUEST_SUCCESS);
109
110
        // load the customer's addtional attributes
111
        $incrementId = $this->getValue(ColumnKeys::INCREMENT_ID);
112
        $isActive = 1;
113
114
        // prepare the date format for the created at/updated at dates
115
        $createdAt = $this->getValue(ColumnKeys::CREATED_AT, date('Y-m-d H:i:s'), array($this, 'formatDate'));
116
        $updatedAt = $this->getValue(ColumnKeys::UPDATED_AT, date('Y-m-d H:i:s'), array($this, 'formatDate'));
117
        $vatRequestDate = $this->getValue(ColumnKeys::VAT_REQUEST_DATE, null, array($this, 'formatDate'));
118
119
        // return the prepared customer
120
        return $this->initializeEntity(
121
            array(
122
                MemberNames::ENTITY_ID           => $entityId,
123
                MemberNames::INCREMENT_ID        => $incrementId,
124
                MemberNames::PARENT_ID           => $customer[MemberNames::ENTITY_ID],
125
                MemberNames::CREATED_AT          => $createdAt,
126
                MemberNames::UPDATED_AT          => $updatedAt,
127
                MemberNames::IS_ACTIVE           => $isActive,
128
                MemberNames::CITY                => $city,
129
                MemberNames::COMPANY             => $company,
130
                MemberNames::COUNTRY_ID          => $countryId,
131
                MemberNames::FAX                 => $fax,
132
                MemberNames::FIRSTNAME           => $firstname,
133
                MemberNames::LASTNAME            => $lastname,
134
                MemberNames::MIDDLENAME          => $middlename,
135
                MemberNames::POSTCODE            => $postcode,
136
                MemberNames::PREFIX              => $prefix,
137
                MemberNames::REGION              => $region,
138
                MemberNames::REGION_ID           => $regionId,
139
                MemberNames::STREET              => $street,
140
                MemberNames::SUFFIX              => $suffix,
141
                MemberNames::TELEPHONE           => $telephone,
142
                MemberNames::VAT_ID              => $vatId,
143
                MemberNames::VAT_IS_VALID        => $vatIsValid,
144
                MemberNames::VAT_REQUEST_DATE    => $vatRequestDate,
145
                MemberNames::VAT_REQUEST_ID      => $vatRequestId,
146
                MemberNames::VAT_REQUEST_SUCCESS => $vatRequestSuccess
147
            )
148
        );
149
    }
150
151
    /**
152
     * Initialize the customer address with the passed attributes and returns an instance.
153
     *
154
     * @param array $attr The customer address attributes
155
     *
156
     * @return array The initialized customer address
157
     */
158
    protected function initializeCustomerAddress(array $attr)
159
    {
160
161
        // try to load the customer address with the given entity ID
162
        if ($entity = $this->loadCustomerAddress($attr[MemberNames::ENTITY_ID])) {
163
            return $this->mergeEntity($entity, $attr);
164
        }
165
166
        // try to load the customer address with the given increment ID
167
        if (!empty($attr[MemberNames::INCREMENT_ID]) && $entity = $this->loadCustomerAddressByIncrementId($attr[MemberNames::INCREMENT_ID])) {
168
            return $this->mergeEntity($entity, $attr);
169
        }
170
171
        // remove the entity ID
172
        unset($attr[MemberNames::ENTITY_ID]);
173
174
        // simply return the attributes
175
        return $attr;
176
    }
177
178
    /**
179
     * Return's the customer with the passed entity ID.
180
     *
181
     * @param integer $id The entity ID of the customer to return
182
     *
183
     * @return array|null The customer
184
     */
185
    protected function loadCustomerAddress($id)
186
    {
187
        return $this->getCustomerAddressBunchProcessor()->loadCustomerAddress($id);
188
    }
189
190
    /**
191
     * Return's the customer with the passed increment ID.
192
     *
193
     * @param string|integer $incrementId The increment ID of the customer to return
194
     *
195
     * @return array|null The customer
196
     */
197
    protected function loadCustomerAddressByIncrementId($incrementId)
198
    {
199
        return $this->getCustomerAddressBunchProcessor()->loadCustomerAddressByIncrementId($incrementId);
0 ignored issues
show
Bug introduced by
The method loadCustomerAddressByIncrementId() does not exist on TechDivision\Import\Cust...BunchProcessorInterface. Did you maybe mean loadCustomerAddress()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

199
        return $this->getCustomerAddressBunchProcessor()->/** @scrutinizer ignore-call */ loadCustomerAddressByIncrementId($incrementId);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
200
    }
201
202
    /**
203
     * Return's the customer with the passed email and website ID.
204
     *
205
     * @param string $email     The email of the customer to return
206
     * @param string $websiteId The website ID of the customer to return
207
     *
208
     * @return array|null The customer
209
     */
210
    protected function loadCustomerByEmailAndWebsiteId($email, $websiteId)
211
    {
212
        return $this->getCustomerAddressBunchProcessor()->loadCustomerByEmailAndWebsiteId($email, $websiteId);
213
    }
214
215
    /**
216
     * Persist's the passed customer address data and return's the ID.
217
     *
218
     * @param array $customerAddress The customer address data to persist
219
     *
220
     * @return string The ID of the persisted entity
221
     */
222
    protected function persistCustomerAddress($customerAddress)
223
    {
224
        return $this->getCustomerAddressBunchProcessor()->persistCustomerAddress($customerAddress);
225
    }
226
227
    /**
228
     * Set's the ID of the customer that has been created recently.
229
     *
230
     * @param string $lastEntityId The entity ID
231
     *
232
     * @return void
233
     */
234
    protected function setLastEntityId($lastEntityId)
235
    {
236
        $this->getSubject()->setLastEntityId($lastEntityId);
237
    }
238
239
    /**
240
     * Return's the store website for the passed code.
241
     *
242
     * @param string $code The code of the store website to return the ID for
243
     *
244
     * @return integer The store website ID
245
     * @throws \Exception Is thrown, if the store website with the requested code is not available
246
     */
247
    protected function getStoreWebsiteIdByCode($code)
248
    {
249
        return $this->getSubject()->getStoreWebsiteIdByCode($code);
250
    }
251
}
252