CustomerAddressExportObserver::newArtefact()   A
last analyzed

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 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Customer\Address\Observers\CustomerAddressExportObserver
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\Services\CustomerAddressBunchProcessorInterface;
19
20
/**
21
 * Prepares the artefacts for the customer address export.
22
 *
23
 * @author    Tim Wagner <[email protected]>
24
 * @copyright 2018 TechDivision GmbH <[email protected]>
25
 * @license   https://opensource.org/licenses/MIT
26
 * @link      https://github.com/techdivision/import-customer-address
27
 * @link      http://www.techdivision.com
28
 */
29
class CustomerAddressExportObserver extends AbstractCustomerAddressImportObserver
30
{
31
32
    /**
33
     * The artefact type.
34
     *
35
     * @var string
36
     */
37
    const ARTEFACT_TYPE = 'customer-import-address';
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
     * @throws \Exception
71
     */
72
    protected function process()
73
    {
74
        // load the email with the column keys of the customer-address CSV file
75
        $email = $this->getValue(\TechDivision\Import\Customer\Utils\ColumnKeys::EMAIL);
76
77
        $firstName = $this->getValue(ColumnKeys::ADDRESS_FIRSTNAME, '');
78
        $lastName = $this->getValue(ColumnKeys::ADDRESS_LASTNAME, '');
79
        $addressStreet = $this->getValue(ColumnKeys::ADDRESS_STREET, '');
80
        $addressCity = $this->getValue(ColumnKeys::ADDRESS_CITY, '');
81
        $countryId = $this->getValue(ColumnKeys::ADDRESS_COUNTRY_ID, '');
82
83
        if (empty($addressCity)
84
            && empty($addressStreet)
85
            && empty($firstName)
86
            && empty($lastName)
87
            && empty($countryId)
88
        ) {
89
            $this->getSystemLogger()->warning(
90
                sprintf(
91
                    'The customer address was not initialized, no address data specified for the email "%s"',
92
                    $email
93
                )
94
            );
95
            return;
96
        }
97
        // initialize the array for the links
98
        $artefacts = array();
99
100
        $artefacts[] = $this->newArtefact(
101
            array(
102
                ColumnKeys::ENTITY_ID                => $this->getValue(ColumnKeys::ENTITY_ID),
103
                ColumnKeys::INCREMENT_ID             => $this->getValue(ColumnKeys::ADDRESS_INCREMENT_ID),
104
                ColumnKeys::IS_ACTIVE                => $this->getValue(ColumnKeys::ADDRESS_IS_ACTIVE),
105
                ColumnKeys::EMAIL                    => $email,
106
                ColumnKeys::WEBSITE                  => $this->getValue(ColumnKeys::WEBSITE),
107
                ColumnKeys::CITY                     => $addressCity,
108
                ColumnKeys::COMPANY                  => $this->getValue(ColumnKeys::ADDRESS_COMPANY),
109
                ColumnKeys::COUNTRY_ID               => $countryId,
110
                ColumnKeys::FAX                      => $this->getValue(ColumnKeys::ADDRESS_FAX),
111
                ColumnKeys::FIRSTNAME                => $firstName,
112
                ColumnKeys::LASTNAME                 => $lastName,
113
                ColumnKeys::MIDDLENAME               => $this->getValue(ColumnKeys::ADDRESS_MIDDLENAME),
114
                ColumnKeys::POSTCODE                 => $this->getValue(ColumnKeys::ADDRESS_POSTCODE),
115
                ColumnKeys::PREFIX                   => $this->getValue(ColumnKeys::ADDRESS_PREFIX),
116
                ColumnKeys::REGION                   => $this->getValue(ColumnKeys::ADDRESS_REGION),
117
                ColumnKeys::REGION_CODE              => $this->getValue(ColumnKeys::ADDRESS_REGION_CODE),
118
                ColumnKeys::REGION_ID                => $this->getValue(ColumnKeys::ADDRESS_REGION_ID),
119
                ColumnKeys::STREET                   => $addressStreet,
120
                ColumnKeys::SUFFIX                   => $this->getValue(ColumnKeys::ADDRESS_SUFFIX),
121
                ColumnKeys::TELEPHONE                => $this->getValue(ColumnKeys::ADDRESS_TELEPHONE),
122
                ColumnKeys::VAT_ID                   => $this->getValue(ColumnKeys::ADDRESS_VAT_ID),
123
                ColumnKeys::ADDRESS_DEFAULT_BILLING  => $this->getValue(ColumnKeys::ADDRESS_DEFAULT_BILLING),
124
                ColumnKeys::ADDRESS_DEFAULT_SHIPPING => $this->getValue(ColumnKeys::ADDRESS_DEFAULT_SHIPPING)
125
            ),
126
            array(
127
                ColumnKeys::ENTITY_ID                => ColumnKeys::ENTITY_ID,
128
                ColumnKeys::INCREMENT_ID             => ColumnKeys::INCREMENT_ID,
129
                ColumnKeys::IS_ACTIVE                => ColumnKeys::ADDRESS_IS_ACTIVE,
130
                ColumnKeys::WEBSITE                  => ColumnKeys::WEBSITE,
131
                ColumnKeys::EMAIL                    => ColumnKeys::EMAIL,
132
                ColumnKeys::CITY                     => ColumnKeys::ADDRESS_CITY,
133
                ColumnKeys::COMPANY                  => ColumnKeys::ADDRESS_COMPANY,
134
                ColumnKeys::COUNTRY_ID               => ColumnKeys::ADDRESS_COUNTRY_ID,
135
                ColumnKeys::FAX                      => ColumnKeys::ADDRESS_FAX,
136
                ColumnKeys::FIRSTNAME                => ColumnKeys::ADDRESS_FIRSTNAME,
137
                ColumnKeys::LASTNAME                 => ColumnKeys::ADDRESS_LASTNAME,
138
                ColumnKeys::MIDDLENAME               => ColumnKeys::ADDRESS_MIDDLENAME,
139
                ColumnKeys::POSTCODE                 => ColumnKeys::ADDRESS_POSTCODE,
140
                ColumnKeys::PREFIX                   => ColumnKeys::ADDRESS_PREFIX,
141
                ColumnKeys::REGION                   => ColumnKeys::ADDRESS_REGION,
142
                ColumnKeys::REGION_CODE              => ColumnKeys::ADDRESS_REGION_CODE,
143
                ColumnKeys::REGION_ID                => ColumnKeys::ADDRESS_REGION_ID,
144
                ColumnKeys::STREET                   => ColumnKeys::ADDRESS_STREET,
145
                ColumnKeys::SUFFIX                   => ColumnKeys::ADDRESS_SUFFIX,
146
                ColumnKeys::TELEPHONE                => ColumnKeys::ADDRESS_TELEPHONE,
147
                ColumnKeys::VAT_ID                   => ColumnKeys::ADDRESS_VAT_ID,
148
                ColumnKeys::ADDRESS_DEFAULT_BILLING  => ColumnKeys::ADDRESS_DEFAULT_BILLING,
149
                ColumnKeys::ADDRESS_DEFAULT_SHIPPING => ColumnKeys::ADDRESS_DEFAULT_SHIPPING
150
            )
151
        );
152
153
        // append the links to the subject
154
        $this->addArtefacts($artefacts);
155
    }
156
157
    /**
158
     * Create's and return's a new empty artefact entity.
159
     *
160
     * @param array $columns             The array with the column data
161
     * @param array $originalColumnNames The array with a mapping from the old to the new column names
162
     *
163
     * @return array The new artefact entity
164
     */
165
    protected function newArtefact(array $columns, array $originalColumnNames)
166
    {
167
        return $this->getSubject()->newArtefact($columns, $originalColumnNames);
168
    }
169
170
    /**
171
     * Add the passed product type artefacts to the product with the
172
     * last entity ID.
173
     *
174
     * @param array $artefacts The product type artefacts
175
     *
176
     * @return void
177
     * @uses \TechDivision\Import\Product\Media\Subjects\MediaSubject::getLastEntityId()
178
     */
179
    protected function addArtefacts(array $artefacts)
180
    {
181
        $this->getSubject()->addArtefacts(CustomerAddressExportObserver::ARTEFACT_TYPE, $artefacts, false);
182
    }
183
}
184