Completed
Push — master ( 4b4114...730efc )
by Gaetano
07:30
created

EzImage   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 43
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
C createValue() 33 33 7

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\ComplexFieldInterface;
7
8 View Code Duplication
class EzImage extends AbstractComplexField implements ComplexFieldInterface
0 ignored issues
show
Duplication introduced by
This class 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...
9
{
10
    /**
11
     * Creates a value object to use as the field value when setting an image field type.
12
     *
13
     * @param array|string $fieldValue The path to the file or an array with 'path' and 'alt_text' keys
14
     * @param array $context The context for execution of the current migrations. Contains f.e. the path to the migration
15
     * @return ImageValue
16
     */
17 1
    public function createValue($fieldValue, array $context = array())
18
    {
19 1
        $altText = '';
20 1
        $fileName = '';
21
        if (is_string($fieldValue)) {
22 1
            $filePath = $fieldValue;
23
        } else {
24 1
            $filePath = $fieldValue['path'];
25 1
            if (isset($fieldValue['alt_text'])) {
26 1
                $altText = $fieldValue['alt_text'];
27
            }
28 1
            if (isset($fieldValue['filename'])) {
29 1
                $fileName = $fieldValue['filename'];
30
            }
31
        }
32
33
        // default format: path is relative to the 'images' dir
34
        $realFilePath = dirname($context['path']) . '/images/' . $filePath['path'];
35
36
        // but in the past, when using a string, this worked as well as an absolute path, so we have to support it as well
37
        if (!is_file($realFilePath) && is_file($filePath)) {
38
            $realFilePath = $filePath;
39
        }
40
41
        return new ImageValue(
42
            array(
43
                'path' => $realFilePath,
44
                'fileSize' => filesize($realFilePath),
45
                'fileName' => $fileName != '' ? $fileName : basename($realFilePath),
46
                'alternativeText' => $altText
47
            )
48
        );
49
    }
50
}
51