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

EzBinaryFile   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 41
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 2
dl 41
loc 41
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 8

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\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