Completed
Push — 1.8 ( 8d6730...a2d9e7 )
by
unknown
42:28
created

WebsiteDataConverter   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 65
Duplicated Lines 9.23 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 0
cbo 1
dl 6
loc 65
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getHeaderConversionRules() 0 11 1
A getBackendHeader() 0 4 1
B convertToImportFormat() 6 13 5
A cutFieldToLength() 0 10 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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) {
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...
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) {
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...
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