Completed
Push — master ( 2d125d...510a23 )
by Gaetano
10:25
created

EzImage   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 69
Duplicated Lines 27.54 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 19
loc 69
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C hashToFieldValue() 11 36 8
A fieldValueToHash() 8 8 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 Kaliop\eZMigrationBundle\Core\ComplexField;
4
5
use eZ\Publish\Core\FieldType\Image\Value as ImageValue;
6
use Kaliop\eZMigrationBundle\API\FieldValueConverterInterface;
7
8
class EzImage extends AbstractComplexField implements FieldValueConverterInterface
9
{
10
    protected $legacyRootDir;
11
12
    public function __construct($legacyRootDir)
13
    {
14
        $this->legacyRootDir = $legacyRootDir;
15
    }
16
17 1
    /**
18
     * Creates a value object to use as the field value when setting an image field type.
19 1
     *
20 1
     * @param array|string $fieldValue The path to the file or an array with 'path' and 'alt_text' keys
21
     * @param array $context The context for execution of the current migrations. Contains f.e. the path to the migration
22 1
     * @return ImageValue
23
     */
24 1
    public function hashToFieldValue($fieldValue, array $context = array())
25 1
    {
26 1
        $altText = '';
27
        $fileName = '';
28 1
29 1
        if ($fieldValue === null) {
30
            return new ImageValue();
31 View Code Duplication
        } else if (is_string($fieldValue)) {
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...
32
            $filePath = $fieldValue;
33
        } else {
34
            $filePath = $fieldValue['path'];
35
            if (isset($fieldValue['alt_text'])) {
36
                $altText = $fieldValue['alt_text'];
37
            }
38
            if (isset($fieldValue['filename'])) {
39
                $fileName = $fieldValue['filename'];
40
            }
41
        }
42
43
        // default format: path is relative to the 'images' dir
44
        $realFilePath = dirname($context['path']) . '/images/' . $filePath;
45
46
        // but in the past, when using a string, this worked as well as an absolute path, so we have to support it as well
47
        if (!is_file($realFilePath) && is_file($filePath)) {
48
            $realFilePath = $filePath;
49
        }
50
51
        return new ImageValue(
52
            array(
53
                'path' => $realFilePath,
54
                'fileSize' => filesize($realFilePath),
55
                'fileName' => $fileName != '' ? $fileName : basename($realFilePath),
56
                'alternativeText' => $altText
57
            )
58
        );
59
    }
60
61
    /**
62
     * @param \eZ\Publish\Core\FieldType\Image\Value $fieldValue
63
     * @param array $context
64
     * @return array
65
     *
66
     * @todo check out if this works in ezplatform
67
     */
68 View Code Duplication
    public function fieldValueToHash($fieldValue, array $context = array())
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...
69
    {
70
        return array(
71
            'path' => realpath($this->legacyRootDir) . $fieldValue->uri,
72
            'filename'=> $fieldValue->fileName,
73
            'alternativeText' => $fieldValue->alternativeText
74
        );
75
    }
76
}
77