1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OroCRM\Bundle\MagentoBundle\ImportExport\Converter; |
4
|
|
|
|
5
|
|
|
use Oro\Bundle\IntegrationBundle\ImportExport\DataConverter\IntegrationAwareDataConverter; |
6
|
|
|
|
7
|
|
|
class WebsiteDataConverter extends IntegrationAwareDataConverter |
8
|
|
|
{ |
9
|
|
|
const NAME_MAX_LENGTH = 255; |
10
|
|
|
const CODE_MAX_LENGTH = 32; |
11
|
|
|
const ELLIPSIS = '...'; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* {@inheritdoc} |
15
|
|
|
*/ |
16
|
|
|
protected function getHeaderConversionRules() |
17
|
|
|
{ |
18
|
|
|
return [ |
19
|
|
|
'website_id' => 'originId', |
20
|
|
|
'code' => 'code', |
21
|
|
|
'name' => 'name', |
22
|
|
|
'sort_order' => 'sortOrder', |
23
|
|
|
'default_group_id' => 'defaultGroupId', |
24
|
|
|
'is_default' => 'default' |
25
|
|
|
]; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
|
|
protected function getBackendHeader() |
32
|
|
|
{ |
33
|
|
|
return array_values($this->getHeaderConversionRules()); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
|
|
public function convertToImportFormat(array $importedRecord, $skipNullValues = true) |
40
|
|
|
{ |
41
|
|
|
|
42
|
|
View Code Duplication |
if (!empty($importedRecord['name']) && mb_strlen($importedRecord['name']) > self::NAME_MAX_LENGTH) { |
|
|
|
|
43
|
|
|
$importedRecord['name'] = $this->cutFieldToLength($importedRecord['name'], self::NAME_MAX_LENGTH); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
View Code Duplication |
if (!empty($importedRecord['code']) && mb_strlen($importedRecord['code']) > self::CODE_MAX_LENGTH) { |
|
|
|
|
47
|
|
|
$importedRecord['code'] = $this->cutFieldToLength($importedRecord['code'], self::CODE_MAX_LENGTH); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return parent::convertToImportFormat($importedRecord, $skipNullValues); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Cuts field value to max length and add ellipsis |
55
|
|
|
* |
56
|
|
|
* @param string $fieldValue |
57
|
|
|
* @param int $maxLength |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
private function cutFieldToLength($fieldValue, $maxLength) |
62
|
|
|
{ |
63
|
|
|
$ellipsisLength = mb_strlen(self::ELLIPSIS); |
64
|
|
|
$fieldValue = sprintf( |
65
|
|
|
'%s%s', |
66
|
|
|
mb_strcut($fieldValue, 0, $maxLength - $ellipsisLength), |
67
|
|
|
self::ELLIPSIS |
68
|
|
|
); |
69
|
|
|
return $fieldValue; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
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.