Passed
Push — master ( d7c002...75ad7f )
by Tim
14:22 queued 04:48
created

CustomerAddressObserver::persistCustomerAddress()   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
        $city = $this->getValue(ColumnKeys::CITY);
97
        $company = $this->getValue(ColumnKeys::COMPANY);
98
        $countryId = $this->getValue(ColumnKeys::COUNTRY_ID);
99
        $fax = $this->getValue(ColumnKeys::FAX);
100
        $firstname = $this->getValue(ColumnKeys::FIRSTNAME);
101
        $lastname = $this->getValue(ColumnKeys::LASTNAME);
102
        $middlename = $this->getValue(ColumnKeys::MIDDLENAME);
103
        $postcode = $this->getValue(ColumnKeys::POSTCODE);
104
        $prefix = $this->getValue(ColumnKeys::PREFIX);
105
        $region = $this->getValue(ColumnKeys::REGION);
106
        $regionId = $this->getValue(ColumnKeys::REGION_ID);
107
        $street = $this->getValue(ColumnKeys::STREET);
108
        $suffix = $this->getValue(ColumnKeys::SUFFIX);
109
        $telephone = $this->getValue(ColumnKeys::TELEPHONE);
110
        $vatId = $this->getValue(ColumnKeys::VAT_ID);
111
        $vatIsValid = $this->getValue(ColumnKeys::VAT_IS_VALID);
112
        $vatRequestId = $this->getValue(ColumnKeys::VAT_REQUEST_ID);
113
        $vatRequestSuccess = $this->getValue(ColumnKeys::VAT_REQUEST_SUCCESS);
114
115
        // load the customer's addtional attributes
116
        $incrementId = null;
117
        $isActive = 1;
118
119
        // prepare the date format for the created at/updated at dates
120
        $createdAt = $this->getValue(ColumnKeys::CREATED_AT, date('Y-m-d H:i:s'), array($this, 'formatDate'));
121
        $updatedAt = $this->getValue(ColumnKeys::UPDATED_AT, date('Y-m-d H:i:s'), array($this, 'formatDate'));
122
        $vatRequestDate = $this->getValue(ColumnKeys::VAT_REQUEST_DATE, date('Y-m-d H:i:s'), array($this, 'formatDate'));
123
124
        // return the prepared customer
125
        return $this->initializeEntity(
126
            array(
127
                MemberNames::INCREMENT_ID        => $incrementId,
128
                MemberNames::PARENT_ID           => $customer[MemberNames::ENTITY_ID],
129
                MemberNames::CREATED_AT          => $createdAt,
130
                MemberNames::UPDATED_AT          => $updatedAt,
131
                MemberNames::IS_ACTIVE           => $isActive,
132
                MemberNames::CITY                => $city,
133
                MemberNames::COMPANY             => $company,
134
                MemberNames::COUNTRY_ID          => $countryId,
135
                MemberNames::FAX                 => $fax,
136
                MemberNames::FIRSTNAME           => $firstname,
137
                MemberNames::LASTNAME            => $lastname,
138
                MemberNames::MIDDLENAME          => $middlename,
139
                MemberNames::POSTCODE            => $postcode,
140
                MemberNames::PREFIX              => $prefix,
141
                MemberNames::REGION              => $region,
142
                MemberNames::REGION_ID           => $regionId,
143
                MemberNames::STREET              => $street,
144
                MemberNames::SUFFIX              => $suffix,
145
                MemberNames::TELEPHONE           => $telephone,
146
                MemberNames::VAT_ID              => $vatId,
147
                MemberNames::VAT_IS_VALID        => $vatIsValid,
148
                MemberNames::VAT_REQUEST_DATE    => $vatRequestDate,
149
                MemberNames::VAT_REQUEST_ID      => $vatRequestId,
150
                MemberNames::VAT_REQUEST_SUCCESS => $vatRequestSuccess
151
            )
152
        );
153
    }
154
155
    /**
156
     * Initialize the customer address with the passed attributes and returns an instance.
157
     *
158
     * @param array $attr The customer address attributes
159
     *
160
     * @return array The initialized customer address
161
     */
162
    protected function initializeCustomerAddress(array $attr)
163
    {
164
        return $attr;
165
    }
166
167
    /**
168
     * Return's the customer with the passed email and website ID.
169
     *
170
     * @param string $email     The email of the customer to return
171
     * @param string $websiteId The website ID of the customer to return
172
     *
173
     * @return array|null The customer
174
     */
175
    protected function loadCustomerByEmailAndWebsiteId($email, $websiteId)
176
    {
177
        return $this->getCustomerAddressBunchProcessor()->loadCustomerByEmailAndWebsiteId($email, $websiteId);
0 ignored issues
show
Bug introduced by
The method loadCustomerByEmailAndWebsiteId() does not exist on TechDivision\Import\Cust...BunchProcessorInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to TechDivision\Import\Cust...BunchProcessorInterface. ( Ignorable by Annotation )

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

177
        return $this->getCustomerAddressBunchProcessor()->/** @scrutinizer ignore-call */ loadCustomerByEmailAndWebsiteId($email, $websiteId);
Loading history...
178
    }
179
180
    /**
181
     * Persist's the passed customer address data and return's the ID.
182
     *
183
     * @param array $customerAddress The customer address data to persist
184
     *
185
     * @return string The ID of the persisted entity
186
     */
187
    protected function persistCustomerAddress($customerAddress)
188
    {
189
        return $this->getCustomerAddressBunchProcessor()->persistCustomerAddress($customerAddress);
190
    }
191
192
    /**
193
     * Set's the ID of the customer that has been created recently.
194
     *
195
     * @param string $lastEntityId The entity ID
196
     *
197
     * @return void
198
     */
199
    protected function setLastEntityId($lastEntityId)
200
    {
201
        $this->getSubject()->setLastEntityId($lastEntityId);
202
    }
203
204
    /**
205
     * Return's the store website for the passed code.
206
     *
207
     * @param string $code The code of the store website to return the ID for
208
     *
209
     * @return integer The store website ID
210
     * @throws \Exception Is thrown, if the store website with the requested code is not available
211
     */
212
    protected function getStoreWebsiteIdByCode($code)
213
    {
214
        return $this->getSubject()->getStoreWebsiteIdByCode($code);
215
    }
216
}
217