Completed
Push — master ( dee99f...2e0f75 )
by Tim
20s queued 10s
created

AbstractDefaultAddressImportObserver   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
eloc 23
dl 0
loc 81
ccs 0
cts 30
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A saveDefaultAddressByType() 0 16 3
A prepareCustomerArray() 0 7 1
A __construct() 0 8 1
1
<?php
2
3
/**
4
 * NOTICE OF LICENSE
5
 *
6
 * This source file is subject to the Open Software License (OSL 3.0)
7
 * that is available through the world-wide-web at this URL:
8
 * http://opensource.org/licenses/osl-3.0.php
9
 *
10
 * @author    Vadim Justus <[email protected]>
11
 * @author    Harald Deiser <[email protected]>
12
 * @copyright 2018 TechDivision GmbH <[email protected]>
13
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
14
 * @link      https://github.com/techdivision/import-customer-address
15
 * @link      http://www.techdivision.com
16
 */
17
18
namespace TechDivision\Import\Customer\Address\Observers;
19
20
use TechDivision\Import\Customer\Address\Repositories\CustomerAddressRepositoryInterface;
21
use TechDivision\Import\Customer\Observers\AbstractCustomerImportObserver;
22
use TechDivision\Import\Customer\Actions\CustomerActionInterface;
23
use TechDivision\Import\Customer\Services\CustomerBunchProcessorInterface;
24
use TechDivision\Import\Customer\Utils\ColumnKeys;
25
use TechDivision\Import\Customer\Utils\MemberNames as OriginalMemberNames;
26
use TechDivision\Import\Utils\EntityStatus;
27
28
/**
29
 * Abstract class to set shipping and billing address.
30
 *
31
 * @copyright  Copyright (c) 2019 TechDivision GmbH (http://www.techdivision.com)
32
 * @author     TechDivision Team Allstars <[email protected]>
33
 * @link       http://www.techdivision.com/
34
 */
35
abstract class AbstractDefaultAddressImportObserver extends AbstractCustomerImportObserver
36
{
37
    /**
38
     * Constants to define default types.
39
     */
40
    const TYPE_DEFAULT_SHIPPING = OriginalMemberNames::DEFAULT_SHIPPING;
41
    const TYPE_DEFAULT_BILLING = OriginalMemberNames::DEFAULT_BILLING;
42
43
    /**
44
     * @var CustomerBunchProcessorInterface
45
     */
46
    protected $customerBunchProcessor;
47
48
    /**
49
     * @var CustomerAddressRepositoryInterface
50
     */
51
    protected $customerAddressRepository;
52
53
    /**
54
     * @var CustomerActionInterface
55
     */
56
    protected $customerAction;
57
58
    /**
59
     * DefaultShippingObserver constructor.
60
     *
61
     * @param CustomerBunchProcessorInterface $customerBunchProcessor
62
     * @param CustomerAddressRepositoryInterface $customerAddressRepository
63
     * @param CustomerActionInterface $customerAction
64
     */
65
    public function __construct(
66
        CustomerBunchProcessorInterface $customerBunchProcessor,
67
        CustomerAddressRepositoryInterface $customerAddressRepository,
68
        CustomerActionInterface $customerAction
69
    ) {
70
        $this->customerBunchProcessor = $customerBunchProcessor;
71
        $this->customerAddressRepository = $customerAddressRepository;
72
        $this->customerAction = $customerAction;
73
    }
74
75
    /**
76
     * Save default address by type.
77
     *
78
     * @param string $type
79
     */
80
    protected function saveDefaultAddressByType($type)
81
    {
82
        $defaultShipping = $this->getValue('_address_' . $type . '_');
83
84
        if ($defaultShipping) {
85
            // load email and website code
86
            $email = $this->getValue('_' . ColumnKeys::EMAIL);
87
            $websiteCode = $this->getValue(ColumnKeys::WEBSITE);
88
            $websiteId = $this->getSubject()->getStoreWebsiteIdByCode($websiteCode);
89
90
            $customer = $this->customerBunchProcessor->loadCustomerByEmailAndWebsiteId($email, $websiteId);
91
            $addressId = $this->getSubject()->getLastEntityId();
92
93
            if (isset($addressId)) {
94
                $customer = $this->prepareCustomerArray($type, $customer, $addressId);
95
                $this->customerAction->persist($customer);
0 ignored issues
show
Bug introduced by
It seems like $customer can also be of type null; however, parameter $row of TechDivision\Import\Acti...ionInterface::persist() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

95
                $this->customerAction->persist(/** @scrutinizer ignore-type */ $customer);
Loading history...
96
            }
97
        }
98
    }
99
100
    /**
101
     * Prepare customer array.
102
     *
103
     * @param string $type
104
     * @param array $customer
105
     * @param int $addressId
106
     *
107
     * @return mixed
108
     */
109
    protected function prepareCustomerArray($type, $customer, $addressId)
110
    {
111
        unset($customer[OriginalMemberNames::CREATED_AT]);
112
        $customer[$type] = $addressId;
113
        $customer[EntityStatus::MEMBER_NAME] = EntityStatus::STATUS_UPDATE;
114
115
        return $customer;
116
    }
117
}
118