Completed
Push — master ( 2e0f75...bf7261 )
by Tim
11s
created

getCustomerBunchProcessor()   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
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Customer\Address\Observers\AbstractDefaultAddressImportObserver
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    Vadim Justus <[email protected]>
15
 * @author    Harald Deiser <[email protected]>
16
 * @author    Tim Wagner <[email protected]>
17
 * @copyright 2019 TechDivision GmbH <[email protected]>
18
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
19
 * @link      https://github.com/techdivision/import-customer-address
20
 * @link      http://www.techdivision.com
21
 */
22
23
namespace TechDivision\Import\Customer\Address\Observers;
24
25
use TechDivision\Import\Customer\Utils\MemberNames;
26
use TechDivision\Import\Customer\Address\Utils\ColumnKeys;
27
use TechDivision\Import\Customer\Services\CustomerBunchProcessorInterface;
28
use TechDivision\Import\Customer\Observers\AbstractCustomerImportObserver;
29
30
/**
31
 * Abstract class that provides the functionality to update a customers default
32
 * shipping and billing address.
33
 *
34
 * @author    Vadim Justus <[email protected]>
35
 * @author    Harald Deiser <[email protected]>
36
 * @author    Tim Wagner <[email protected]>
37
 * @copyright 2019 TechDivision GmbH <[email protected]>
38
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
39
 * @link      https://github.com/techdivision/import-customer-address
40
 * @link      http://www.techdivision.com
41
 */
42
abstract class AbstractDefaultAddressImportObserver extends AbstractCustomerImportObserver
43
{
44
45
    /**
46
     * The customer bunch processor instance.
47
     *
48
     * @var \TechDivision\Import\Customer\Services\CustomerBunchProcessorInterface
49
     */
50
    protected $customerBunchProcessor;
51
52
    /**
53
     * The mapping for the default billing/shipping address column => member name.
54
     *
55
     * @var array
56
     */
57
    protected $defaultAddressMapping = array(
58
        ColumnKeys::ADDRESS_DEFAULT_BILLING  => MemberNames::DEFAULT_BILLING,
59
        ColumnKeys::ADDRESS_DEFAULT_SHIPPING => MemberNames::DEFAULT_SHIPPING
60
    );
61
62
    /**
63
     * DefaultShippingObserver constructor.
64
     *
65
     * @param \TechDivision\Import\Customer\Services\CustomerBunchProcessorInterface $customerBunchProcessor The processor instance
66
     */
67
    public function __construct(CustomerBunchProcessorInterface $customerBunchProcessor)
68
    {
69
        $this->customerBunchProcessor = $customerBunchProcessor;
70
    }
71
72
    /**
73
     * Returns the customer bunch processor instance.
74
     *
75
     * @return \TechDivision\Import\Customer\Services\CustomerBunchProcessorInterface The processor instance
76
     */
77
    protected function getCustomerBunchProcessor()
78
    {
79
        return $this->customerBunchProcessor;
80
    }
81
82
    /**
83
     * Maps the passed customer address column name to the matching customer member name.
84
     *
85
     * @param string $columnName The column name to map
86
     *
87
     * @return string The mapped customer member name
88
     * @throws \Exception Is thrown if the column can't be mapped
89
     */
90
    protected function mapColumName($columnName)
91
    {
92
93
        // query whether or not we can match the column name
94
        if (isset($this->defaultAddressMapping[$columnName])) {
95
            return $this->defaultAddressMapping[$columnName];
96
        }
97
98
        // throw an exception if NOT
99
        throw new \Exception(sprintf('Can\'t map member name to default address column "%s"', $columnName));
100
    }
101
102
    /**
103
     * Save default address by type.
104
     *
105
     * @param string $type The address type to save
106
     *
107
     * @return void
108
     */
109
    protected function saveDefaultAddressByType($type)
110
    {
111
112
        // load email and website ID
113
        $email     = $this->getValue(ColumnKeys::EMAIL);
114
        $websiteId = $this->getSubject()->getStoreWebsiteIdByCode($this->getValue(ColumnKeys::WEBSITE));
115
116
        // try to load the customer with the given email + website ID
117
        if ($customer = $this->getCustomerBunchProcessor()->loadCustomerByEmailAndWebsiteId($email, $websiteId)) {
118
            // initialize an empty address ID
119
            $addressId = null;
120
121
            // query whether or not we've a default shipping/billing address
122
            if ((integer) $this->getValue($type) === 1) {
123
                $addressId = $this->getSubject()->getLastEntityId();
124
            }
125
126
            // finally update the customer
127
            $this->getCustomerBunchProcessor()->persistCustomer(
128
                $this->mergeEntity(
129
                    $customer,
130
                    array(
131
                        $this->mapColumName($type) => $addressId,
132
                        MemberNames::UPDATED_AT    => $this->formatDate(date('Y-m-d H:i:s'))
0 ignored issues
show
Deprecated Code introduced by
The function TechDivision\Import\Obse...tObserver::formatDate() has been deprecated: Will be removed with version 1.0.0, use subject method instead ( Ignorable by Annotation )

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

132
                        MemberNames::UPDATED_AT    => /** @scrutinizer ignore-deprecated */ $this->formatDate(date('Y-m-d H:i:s'))

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
133
                    )
134
                )
135
            );
136
        }
137
    }
138
}
139