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

EzImage::createValue()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 33
Code Lines 20

Duplication

Lines 33
Ratio 100 %

Code Coverage

Tests 9
CRAP Score 7

Importance

Changes 0
Metric Value
dl 33
loc 33
ccs 9
cts 9
cp 1
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 20
nc 10
nop 2
crap 7
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