Completed
Push — master ( ac3b8b...3c857d )
by Gaetano
18:21
created

EzMedia::hashToFieldValue()   F

Complexity

Conditions 18
Paths 4099

Size

Total Lines 76
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 76
c 0
b 0
f 0
rs 2.2902
cc 18
eloc 51
nc 4099
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
    public function hashToFieldValue($fieldValue, array $context = array())
18
    {
19
        $fileName = '';
20
        $mimeType = '';
21
        $hasController = false;
22
        $autoPlay = false;
23
        $loop = false;
24
        $height = 0;
25
        $width = 0;
26
27
        if ($fieldValue === null) {
28
            return new MediaValue();
29
        } else if (is_string($fieldValue)) {
30
            $filePath = $fieldValue;
31
        } else {
32
            // BC
33
            if (isset($fieldValue['fileName'])) {
34
                $fileName = $fieldValue['fileName'];
35
            }
36
            if (isset($fieldValue['mimeType'])) {
37
                $fileName = $fieldValue['mimeType'];
38
            }
39
            if (isset($fieldValue['hasController'])) {
40
                $hasController = $fieldValue['hasController'];
41
            }
42
            if (isset($fieldValue['inputUri'])) {
43
                $filePath = $fieldValue['inputUri'];
44
            } else {
45
                $filePath = $fieldValue['path'];
46
            }
47
            // new attribute names
48
            if (isset($fieldValue['filename'])) {
49
                $fileName = $fieldValue['filename'];
50
            }
51
            if (isset($fieldValue['has_controller'])) {
52
                $hasController = $fieldValue['has_controller'];
53
            }
54
            if (isset($fieldValue['autoplay'])) {
55
                $autoPlay = $fieldValue['autoplay'];
56
            }
57
            if (isset($fieldValue['loop'])) {
58
                $loop = $fieldValue['loop'];
59
            }
60
            if (isset($fieldValue['height'])) {
61
                $height = $fieldValue['height'];
62
            }
63
            if (isset($fieldValue['width'])) {
64
                $width = $fieldValue['width'];
65
            }
66
            if (isset($fieldValue['mime_type'])) {
67
                $mimeType = $fieldValue['mime_type'];
68
            }
69
        }
70
71
        // 'default' format: path is relative to the 'media' dir
72
        $realFilePath = dirname($context['path']) . '/media/' . $filePath;
73
74
        // but in the past, when using a string, this worked as well as an absolute path, so we have to support it as well
75
        if (!is_file($realFilePath) && is_file($filePath)) {
76
            $realFilePath = $filePath;
77
        }
78
79
        return new MediaValue(
80
            array(
81
                'path' => $realFilePath,
82
                'fileSize' => filesize($realFilePath),
83
                'fileName' => $fileName != '' ? $fileName : basename($realFilePath),
84
                'mimeType' => $mimeType != '' ? $mimeType : mime_content_type($realFilePath),
85
                'hasController' => $hasController,
86
                'autoplay' => $autoPlay,
87
                'loop'=> $loop,
88
                'height' => $height,
89
                'width' => $width,
90
            )
91
        );
92
    }
93
94
    /**
95
     * @param \eZ\Publish\Core\FieldType\Media\Value $fieldValue
96
     * @param array $context
97
     * @return array
98
     *
99
     * @todo check out if this works in ezplatform
100
     */
101
    public function fieldValueToHash($fieldValue, array $context = array())
102
    {
103
        return array(
104
            'path' => realpath($this->ioRootDir) . '/' . ($this->ioDecorator ? $this->ioDecorator->undecorate($fieldValue->uri) : $fieldValue->uri),
105
            'filename'=> $fieldValue->fileName,
106
            'mime_type' => $fieldValue->mimeType,
107
            'has_controller' => $fieldValue->hasController,
108
            'autoplay' => $fieldValue->autoplay,
109
            'loop'=> $fieldValue->loop,
110
            'width' => $fieldValue->width,
111
            'height' => $fieldValue->height,
112
        );
113
    }
114
}
115