EzMedia   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Test Coverage

Coverage 78.33%

Importance

Changes 0
Metric Value
eloc 63
dl 0
loc 110
ccs 47
cts 60
cp 0.7833
rs 10
c 0
b 0
f 0
wmc 21

2 Methods

Rating   Name   Duplication   Size   Complexity  
F hashToFieldValue() 0 74 18
A fieldValueToHash() 0 16 3
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\FieldHandler;
4
5
use eZ\Publish\Core\FieldType\Media\Value as MediaValue;
6
use Kaliop\eZMigrationBundle\API\FieldValueConverterInterface;
7
8
class EzMedia extends FileFieldHandler implements FieldValueConverterInterface
9
{
10
    /**
11
     * Creates a value object to use as the field value when setting a media field type.
12
     *
13
     * @param array|string $fieldValue The path to the file or an array with 'path' and many other keys
14
     * @param array $context The context for execution of the current migrations. Contains f.e. the path to the migration
15
     * @return MediaValue
16
     *
17
     * @todo resolve refs more
18
     */
19 2
    public function hashToFieldValue($fieldValue, array $context = array())
20
    {
21 2
        $fileName = '';
22 2
        $mimeType = '';
23 2
        $hasController = false;
24 2
        $autoPlay = false;
25 2
        $loop = false;
26 2
        $height = 0;
27 2
        $width = 0;
28
29 2
        if ($fieldValue === null) {
0 ignored issues
show
introduced by
The condition $fieldValue === null is always false.
Loading history...
30 1
            return new MediaValue();
31 2
        } else if (is_string($fieldValue)) {
32
            $filePath = $fieldValue;
33
        } else {
34
            // BC
35 2
            if (isset($fieldValue['fileName'])) {
36
                $fileName = $this->referenceResolver->resolveReference($fieldValue['fileName']);
37
            }
38 2
            if (isset($fieldValue['mimeType'])) {
39
                $fileName = $this->referenceResolver->resolveReference($fieldValue['mimeType']);
40
            }
41 2
            if (isset($fieldValue['hasController'])) {
42
                $hasController = $this->referenceResolver->resolveReference($fieldValue['hasController']);
43
            }
44 2
            if (isset($fieldValue['inputUri'])) {
45
                $filePath = $this->referenceResolver->resolveReference($fieldValue['inputUri']);
46
            } else {
47 2
                $filePath = $this->referenceResolver->resolveReference($fieldValue['path']);
48
            }
49
            // new attribute names
50 2
            if (isset($fieldValue['filename'])) {
51 2
                $fileName = $this->referenceResolver->resolveReference($fieldValue['filename']);
52
            }
53 2
            if (isset($fieldValue['has_controller'])) {
54
                $hasController = $this->referenceResolver->resolveReference($fieldValue['has_controller']);
55
            }
56 2
            if (isset($fieldValue['autoplay'])) {
57
                $autoPlay = $this->referenceResolver->resolveReference($fieldValue['autoplay']);
58
            }
59 2
            if (isset($fieldValue['loop'])) {
60
                $loop = $this->referenceResolver->resolveReference($fieldValue['loop']);
61
            }
62 2
            if (isset($fieldValue['height'])) {
63
                $height = $this->referenceResolver->resolveReference($fieldValue['height']);
64
            }
65 2
            if (isset($fieldValue['width'])) {
66
                $width = $this->referenceResolver->resolveReference($fieldValue['width']);
67
            }
68 2
            if (isset($fieldValue['mime_type'])) {
69
                $mimeType = $this->referenceResolver->resolveReference($fieldValue['mime_type']);
70
            }
71
        }
72
73
        // 'default' format: path is relative to the 'media' dir
74 2
        $realFilePath = dirname($context['path']) . '/media/' . $filePath;
75
76
        // but in the past, when using a string, this worked as well as an absolute path, so we have to support it as well
77
        /// @todo atm this does not work for files from content fields in cluster mode
78 2
        if (!is_file($realFilePath) && is_file($filePath)) {
79
            $realFilePath = $filePath;
80
        }
81
82 2
        return new MediaValue(
83
            array(
84 2
                'path' => $realFilePath,
85 2
                'fileSize' => filesize($realFilePath),
86 2
                'fileName' => $fileName != '' ? $fileName : basename($realFilePath),
87 2
                'mimeType' => $mimeType != '' ? $mimeType : mime_content_type($realFilePath),
88 2
                'hasController' => $hasController,
89 2
                'autoplay' => $autoPlay,
90 2
                'loop'=> $loop,
91 2
                'height' => $height,
92 2
                'width' => $width,
93
            )
94
        );
95
    }
96
97
    /**
98
     * @param \eZ\Publish\Core\FieldType\Media\Value $fieldValue
99
     * @param array $context
100
     * @return array
101
     */
102 1
    public function fieldValueToHash($fieldValue, array $context = array())
103
    {
104 1
        if ($fieldValue->uri == null) {
105
            return null;
106
        }
107 1
        $binaryFile = $this->ioService->loadBinaryFile($fieldValue->id);
108
        /// @todo we should handle clustered configurations, to give back the absolute path on disk rather than the 'virtual' one
109
        return array(
110 1
            'path' => realpath($this->ioRootDir) . '/' . ($this->ioDecorator ? $this->ioDecorator->undecorate($binaryFile->uri) : $fieldValue->uri),
111 1
            'filename'=> $fieldValue->fileName,
112 1
            'mime_type' => $fieldValue->mimeType,
113 1
            'has_controller' => $fieldValue->hasController,
114 1
            'autoplay' => $fieldValue->autoplay,
115 1
            'loop'=> $fieldValue->loop,
116 1
            'width' => $fieldValue->width,
117 1
            'height' => $fieldValue->height,
118
        );
119
    }
120
}
121