Completed
Push — master ( 167ab4...693f6f )
by
unknown
12:29
created

convertToImportFormat()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 8
Ratio 50 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 8
loc 16
rs 9.4285
c 1
b 0
f 0
cc 3
eloc 10
nc 4
nop 2
1
<?php
2
3
namespace OroCRM\Bundle\MagentoBundle\ImportExport\Converter;
4
5
use Oro\Bundle\AddressBundle\Entity\AddressType;
6
7
class CustomerAddressDataConverter extends AbstractAddressDataConverter
8
{
9
    /**
10
     * {@inheritdoc}
11
     */
12
    protected function getHeaderConversionRules()
13
    {
14
        return array_merge(
15
            parent::getHeaderConversionRules(),
16
            [
17
                'customer_address_id' => 'originId',
18
                'customer_id'         => 'owner:originId',
19
                'region_id'           => 'region:combinedCode', // Note, this is integer identifier of magento region
20
            ]
21
        );
22
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function convertToImportFormat(array $importedRecord, $skipNullValues = true)
28
    {
29
        $importedRecord = parent::convertToImportFormat($importedRecord, $skipNullValues);
30
        $importedRecord = AttributesConverterHelper::addUnknownAttributes($importedRecord, $this->context);
31
32 View Code Duplication
        if (!empty($importedRecord['is_default_shipping'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33
            $importedRecord['types'][] = ['name' => AddressType::TYPE_SHIPPING];
34
            unset($importedRecord['is_default_shipping']);
35
        }
36 View Code Duplication
        if (!empty($importedRecord['is_default_billing'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
37
            $importedRecord['types'][] = ['name' => AddressType::TYPE_BILLING];
38
            unset($importedRecord['is_default_billing']);
39
        }
40
41
        return $importedRecord;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    protected function convertImportedRegion(array $importedRecord)
48
    {
49
        if (empty($importedRecord['region']['combinedCode'])) {
50
            $importedRecord['region'] = null;
51
        } else {
52
            $importedRecord['region']['combinedCode'] = (string)$importedRecord['region']['combinedCode'];
53
        }
54
55
        return $importedRecord;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    protected function getBackendHeader()
62
    {
63
        return array_merge(parent::getBackendHeader(), ['types:0:name', 'types:1:name']);
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function convertToExportFormat(array $exportedRecord, $skipNullValues = true)
70
    {
71
        $exportedRecord = parent::convertToExportFormat($exportedRecord, $skipNullValues);
72
        $exportedRecord = $this->convertTypesToExportFormat($exportedRecord);
73
74
        unset(
75
            $exportedRecord['created_at'],
76
            $exportedRecord['updated_at']
77
        );
78
79
        return $exportedRecord;
80
    }
81
82
    /**
83
     * @param array $exportedRecord
84
     * @return array
85
     */
86
    protected function convertTypesToExportFormat(array $exportedRecord)
87
    {
88
        $exportedRecord = array_merge(
89
            $exportedRecord,
90
            [
91
                'is_default_billing' => false,
92
                'is_default_shipping' => false
93
            ]
94
        );
95
96 View Code Duplication
        if (isset($exportedRecord['types:0:name'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
            $exportedRecord = array_merge(
98
                $exportedRecord,
99
                $this->getMagentoTypeData($exportedRecord['types:0:name'])
100
            );
101
            unset($exportedRecord['types:0:name']);
102
        }
103 View Code Duplication
        if (isset($exportedRecord['types:1:name'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104
            $exportedRecord = array_merge(
105
                $exportedRecord,
106
                $this->getMagentoTypeData($exportedRecord['types:1:name'])
107
            );
108
            unset($exportedRecord['types:1:name']);
109
        }
110
111
        return $exportedRecord;
112
    }
113
114
    /**
115
     * @param string $type
116
     * @return array
117
     */
118
    protected function getMagentoTypeData($type)
119
    {
120
        if ($type === AddressType::TYPE_BILLING) {
121
            return ['is_default_billing' => true];
122
        }
123
        if ($type === AddressType::TYPE_SHIPPING) {
124
            return ['is_default_shipping' => true];
125
        }
126
127
        return [];
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133 View Code Duplication
    protected function fillEmptyColumns(array $header, array $data)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
134
    {
135
        $dataDiff = array_diff(array_keys($data), $header);
136
        $data = array_diff_key($data, array_flip($dataDiff));
137
138
        return parent::fillEmptyColumns($header, $data);
139
    }
140
}
141