Completed
Push — master ( 5a501b...3982a6 )
by Gaetano
08:04
created

EzBinaryFile   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 60
Duplicated Lines 31.67 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
B hashToFieldValue() 11 36 9
A fieldValueToHash() 8 8 2

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\FieldHandler;
4
5
use eZ\Publish\Core\FieldType\BinaryFile\Value as BinaryFileValue;
6
use Kaliop\eZMigrationBundle\API\FieldValueConverterInterface;
7
8
class EzBinaryFile extends FileFieldHandler implements FieldValueConverterInterface
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 hashToFieldValue($fieldValue, array $context = array())
16
    {
17
        $mimeType = '';
18
        $fileName = '';
19
20
        if ($fieldValue === null) {
21
            return new BinaryFileValue();
22 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
            $filePath = $fieldValue;
24
        } else {
25
            $filePath = $fieldValue['path'];
26
            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
    /**
53
     * @param \eZ\Publish\Core\FieldType\BinaryFile\Value $fieldValue
54
     * @param array $context
55
     * @return array
56
     *
57
     * @todo check if this works in ezplatform
58
     */
59 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...
60
    {
61
        return array(
62
            'path' => realpath($this->ioRootDir) . '/' . ($this->ioDecorator ? $this->ioDecorator->undecorate($fieldValue->uri) : $fieldValue->uri),
63
            'filename'=> $fieldValue->fileName,
64
            'mimeType' => $fieldValue->mimeType
65
        );
66
    }
67
}
68