Code Duplication    Length = 41-43 lines in 2 locations

Core/ComplexField/EzBinaryFile.php 1 location

@@ 8-48 (lines=41) @@
5
use eZ\Publish\Core\FieldType\BinaryFile\Value as BinaryFileValue;
6
use Kaliop\eZMigrationBundle\API\ComplexFieldInterface;
7
8
class EzBinaryFile extends AbstractComplexField implements ComplexFieldInterface
9
{
10
    /**
11
     * @param array|string $fieldValue The path to the file or an array with 'path' key
12
     * @param array $context The context for execution of the current migrations. Contains f.e. the path to the migration
13
     * @return BinaryFileValue
14
     */
15
    public function createValue($fieldValue, array $context = array())
16
    {
17
        $mimeType = '';
18
        $fileName = '';
19
        if (is_string($fieldValue)) {
20
            $filePath = $fieldValue;
21
        } else {
22
            $filePath = $fieldValue['path'];
23
            if (isset($fieldValue['filename'])) {
24
                $fileName = $fieldValue['filename'];
25
            }
26
            if (isset($fieldValue['mime_type'])) {
27
                $mimeType = $fieldValue['mime_type'];
28
            }
29
        }
30
31
        // default format: path is relative to the 'files' dir
32
        $realFilePath = dirname($context['path']) .  '/files/' . $filePath;
33
34
        // but in the past, when using a string, this worked as well as an absolute path, so we have to support it as well
35
        if (!is_file($realFilePath) && is_file($filePath)) {
36
            $realFilePath = $filePath;
37
        }
38
39
        return new BinaryFileValue(
40
            array(
41
                'path' => $realFilePath,
42
                'fileSize' => filesize($realFilePath),
43
                'fileName' => $fileName != '' ? $fileName : basename($realFilePath),
44
                'mimeType' => $mimeType != '' ? $mimeType : mime_content_type($realFilePath)
45
            )
46
        );
47
    }
48
}
49

Core/ComplexField/EzImage.php 1 location

@@ 8-50 (lines=43) @@
5
use eZ\Publish\Core\FieldType\Image\Value as ImageValue;
6
use Kaliop\eZMigrationBundle\API\ComplexFieldInterface;
7
8
class EzImage extends AbstractComplexField implements ComplexFieldInterface
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
    public function createValue($fieldValue, array $context = array())
18
    {
19
        $altText = '';
20
        $fileName = '';
21
        if (is_string($fieldValue)) {
22
            $filePath = $fieldValue;
23
        } else {
24
            $filePath = $fieldValue['path'];
25
            if (isset($fieldValue['alt_text'])) {
26
                $altText = $fieldValue['alt_text'];
27
            }
28
            if (isset($fieldValue['filename'])) {
29
                $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