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

EzImage   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 73.08%

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 65
ccs 19
cts 26
cp 0.7308
rs 10
c 0
b 0
f 0
wmc 11

2 Methods

Rating   Name   Duplication   Size   Complexity  
A fieldValueToHash() 0 10 3
B hashToFieldValue() 0 33 8
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\FieldHandler;
4
5
use eZ\Publish\Core\FieldType\Image\Value as ImageValue;
6
use Kaliop\eZMigrationBundle\API\FieldValueConverterInterface;
7
8
class EzImage extends FileFieldHandler implements FieldValueConverterInterface
9
{
10
    /**
11
     * Creates a value object to use as the field value when setting an image field type.
12
     *
13
     * @param array|string $fieldValue The path to the file or an array with 'path' and 'alt_text' keys
14
     * @param array $context The context for execution of the current migrations. Contains f.e. the path to the migration
15
     * @return ImageValue
16
     *
17
     * @todo resolve refs more
18
     */
19 1
    public function hashToFieldValue($fieldValue, array $context = array())
20
    {
21 1
        $altText = '';
22 1
        $fileName = '';
23
24 1
        if ($fieldValue === null) {
0 ignored issues
show
introduced by
The condition $fieldValue === null is always false.
Loading history...
25 1
            return new ImageValue();
0 ignored issues
show
Deprecated Code introduced by
The class eZ\Publish\Core\FieldType\Image\Value has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

25
            return /** @scrutinizer ignore-deprecated */ new ImageValue();
Loading history...
26 1
        } else if (is_string($fieldValue)) {
27 1
            $filePath = $fieldValue;
28
        } else {
29 1
            $filePath = $fieldValue['path'];
30 1
            if (isset($fieldValue['alt_text'])) {
31 1
                $altText = $fieldValue['alt_text'];
32
            }
33 1
            if (isset($fieldValue['filename'])) {
34 1
                $fileName = $fieldValue['filename'];
35
            }
36
        }
37
38
        // default format: path is relative to the 'images' dir
39 1
        $realFilePath = dirname($context['path']) . '/images/' . $filePath;
40
41
        // but in the past, when using a string, this worked as well as an absolute path, so we have to support it as well
42 1
        if (!is_file($realFilePath) && is_file($filePath)) {
43
            $realFilePath = $filePath;
44
        }
45
46 1
        return new ImageValue(
0 ignored issues
show
Deprecated Code introduced by
The class eZ\Publish\Core\FieldType\Image\Value has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

46
        return /** @scrutinizer ignore-deprecated */ new ImageValue(
Loading history...
47
            array(
48 1
                'path' => $realFilePath,
49 1
                'fileSize' => filesize($realFilePath),
50 1
                'fileName' => $fileName != '' ? $fileName : basename($realFilePath),
51 1
                'alternativeText' => $altText
52
            )
53
        );
54
    }
55
56
    /**
57
     * @param \eZ\Publish\Core\FieldType\Image\Value $fieldValue
58
     * @param array $context
59
     * @return array
60
     *
61
     * @todo check out if this works in ezplatform
62
     */
63
    public function fieldValueToHash($fieldValue, array $context = array())
64
    {
65
        if ($fieldValue->uri == null) {
66
            return null;
67
        }
68
69
        return array(
70
            'path' => realpath($this->ioRootDir) . '/' . ($this->ioDecorator ? $this->ioDecorator->undecorate($fieldValue->uri) : $fieldValue->uri),
71
            'filename'=> $fieldValue->fileName,
72
            'alternativeText' => $fieldValue->alternativeText
73
        );
74
    }
75
}
76