Completed
Branch master (e35419)
by Gaetano
06:40
created

EzBinaryFile::hashToFieldValue()   B

Complexity

Conditions 9
Paths 11

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 9.0101

Importance

Changes 0
Metric Value
cc 9
eloc 21
nc 11
nop 2
dl 0
loc 33
ccs 19
cts 20
cp 0.95
crap 9.0101
rs 8.0555
c 0
b 0
f 0
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
     * @todo resolve refs more
16
     */
17 1
    public function hashToFieldValue($fieldValue, array $context = array())
18
    {
19 1
        $mimeType = '';
20 1
        $fileName = '';
21
22 1
        if ($fieldValue === null) {
0 ignored issues
show
introduced by
The condition $fieldValue === null is always false.
Loading history...
23 1
            return new BinaryFileValue();
24 1
        } if (is_string($fieldValue)) {
25 1
            $filePath = $fieldValue;
26
        } else {
27 1
            $filePath = $fieldValue['path'];
28 1
            if (isset($fieldValue['filename'])) {
29 1
                $fileName = $fieldValue['filename'];
30
            }
31 1
            if (isset($fieldValue['mime_type'])) {
32 1
                $mimeType = $fieldValue['mime_type'];
33
            }
34
        }
35
36
        // default format: path is relative to the 'files' dir
37 1
        $realFilePath = dirname($context['path']) . '/files/' . $filePath;
38
39
        // but in the past, when using a string, this worked as well as an absolute path, so we have to support it as well
40 1
        if (!is_file($realFilePath) && is_file($filePath)) {
41
            $realFilePath = $filePath;
42
        }
43
44 1
        return new BinaryFileValue(
45
            array(
46 1
                'path' => $realFilePath,
47 1
                'fileSize' => filesize($realFilePath),
48 1
                'fileName' => $fileName != '' ? $fileName : basename($realFilePath),
49 1
                'mimeType' => $mimeType != '' ? $mimeType : mime_content_type($realFilePath)
50
            )
51
        );
52
    }
53
54
    /**
55
     * @param \eZ\Publish\Core\FieldType\BinaryFile\Value $fieldValue
56
     * @param array $context
57
     * @return array
58
     *
59
     * @todo check if this works in ezplatform
60
     */
61
    public function fieldValueToHash($fieldValue, array $context = array())
62
    {
63
        if ($fieldValue->uri == null) {
64
            return null;
65
        }
66
67
        return array(
68
            'path' => realpath($this->ioRootDir) . '/' . ($this->ioDecorator ? $this->ioDecorator->undecorate($fieldValue->uri) : $fieldValue->uri),
69
            'filename'=> $fieldValue->fileName,
70
            'mimeType' => $fieldValue->mimeType
71
        );
72
    }
73
}
74