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

EzBinaryFile::fieldValueToHash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 8
loc 8
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
crap 2
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