Completed
Push — master ( 3be5a1...d5b14f )
by Gaetano
07:33
created

EzBinaryFile::createValue()   D

Complexity

Conditions 9
Paths 11

Size

Total Lines 36
Code Lines 22

Duplication

Lines 11
Ratio 30.56 %

Code Coverage

Tests 9
CRAP Score 9

Importance

Changes 0
Metric Value
dl 11
loc 36
ccs 9
cts 9
cp 1
rs 4.909
c 0
b 0
f 0
cc 9
eloc 22
nc 11
nop 2
crap 9
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\ComplexField;
4
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 1
    public function createValue($fieldValue, array $context = array())
16
    {
17 1
        $mimeType = '';
18
        $fileName = '';
19 1
20
        if ($fieldValue === null) {
21 1
            return new BinaryFileValue();
22 1 View Code Duplication
        } 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...
23 1
            $filePath = $fieldValue;
24 1
        } else {
25 1
            $filePath = $fieldValue['path'];
26 1
            if (isset($fieldValue['filename'])) {
27
                $fileName = $fieldValue['filename'];
28
            }
29
            if (isset($fieldValue['mime_type'])) {
30
                $mimeType = $fieldValue['mime_type'];
31
            }
32
        }
33
34
        // default format: path is relative to the 'files' dir
35
        $realFilePath = dirname($context['path']) .  '/files/' . $filePath;
36
37
        // but in the past, when using a string, this worked as well as an absolute path, so we have to support it as well
38
        if (!is_file($realFilePath) && is_file($filePath)) {
39
            $realFilePath = $filePath;
40
        }
41
42
        return new BinaryFileValue(
43
            array(
44
                'path' => $realFilePath,
45
                'fileSize' => filesize($realFilePath),
46
                'fileName' => $fileName != '' ? $fileName : basename($realFilePath),
47
                'mimeType' => $mimeType != '' ? $mimeType : mime_content_type($realFilePath)
48
            )
49
        );
50
    }
51
}
52