Completed
Push — master ( 2d125d...510a23 )
by Gaetano
10:25
created

EzBinaryFile   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 67
Duplicated Lines 28.36 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 19
loc 67
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
D hashToFieldValue() 11 36 9
A fieldValueToHash() 8 8 1

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\FieldValueConverterInterface;
7
8
class EzBinaryFile extends AbstractComplexField implements FieldValueConverterInterface
9
{
10
    protected $legacyRootDir;
11
12
    public function __construct($legacyRootDir)
13
    {
14
        $this->legacyRootDir = $legacyRootDir;
15 1
    }
16
17 1
    /**
18
     * @param array|string $fieldValue The path to the file or an array with 'path' key
19 1
     * @param array $context The context for execution of the current migrations. Contains f.e. the path to the migration
20
     * @return BinaryFileValue
21 1
     */
22 1
    public function hashToFieldValue($fieldValue, array $context = array())
23 1
    {
24 1
        $mimeType = '';
25 1
        $fileName = '';
26 1
27
        if ($fieldValue === null) {
28
            return new BinaryFileValue();
29 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...
30
            $filePath = $fieldValue;
31
        } else {
32
            $filePath = $fieldValue['path'];
33
            if (isset($fieldValue['filename'])) {
34
                $fileName = $fieldValue['filename'];
35
            }
36
            if (isset($fieldValue['mime_type'])) {
37
                $mimeType = $fieldValue['mime_type'];
38
            }
39
        }
40
41
        // default format: path is relative to the 'files' dir
42
        $realFilePath = dirname($context['path']) . '/files/' . $filePath;
43
44
        // but in the past, when using a string, this worked as well as an absolute path, so we have to support it as well
45
        if (!is_file($realFilePath) && is_file($filePath)) {
46
            $realFilePath = $filePath;
47
        }
48
49
        return new BinaryFileValue(
50
            array(
51
                'path' => $realFilePath,
52
                'fileSize' => filesize($realFilePath),
53
                'fileName' => $fileName != '' ? $fileName : basename($realFilePath),
54
                'mimeType' => $mimeType != '' ? $mimeType : mime_content_type($realFilePath)
55
            )
56
        );
57
    }
58
59
    /**
60
     * @param \eZ\Publish\Core\FieldType\BinaryFile\Value $fieldValue
61
     * @param array $context
62
     * @return array
63
     *
64
     * @todo check out if this works in ezplatform
65
     */
66 View Code Duplication
    public function fieldValueToHash($fieldValue, array $context = array())
0 ignored issues
show
Duplication introduced by
This method 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...
67
    {
68
        return array(
69
            'path' => realpath($this->legacyRootDir) . $fieldValue->uri,
70
            'filename'=> $fieldValue->fileName,
71
            'mimeType' => $fieldValue->mimeType
72
        );
73
    }
74
}
75