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

EzBinaryFile::createValue()   C

Complexity

Conditions 8
Paths 10

Size

Total Lines 33
Code Lines 20

Duplication

Lines 33
Ratio 100 %

Code Coverage

Tests 9
CRAP Score 8

Importance

Changes 0
Metric Value
dl 33
loc 33
ccs 9
cts 9
cp 1
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 20
nc 10
nop 2
crap 8
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 View Code Duplication
class EzBinaryFile 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
     * @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
        if (is_string($fieldValue)) {
20
            $filePath = $fieldValue;
21 1
        } else {
22 1
            $filePath = $fieldValue['path'];
23 1
            if (isset($fieldValue['filename'])) {
24 1
                $fileName = $fieldValue['filename'];
25 1
            }
26 1
            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