Passed
Pull Request — master (#203)
by
unknown
09:54
created

EzImageAsset   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 45
rs 10
c 0
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A hashToFieldValue() 0 29 6
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\FieldHandler;
4
5
use eZ\Publish\Core\FieldType\ImageAsset\Value;
0 ignored issues
show
Bug introduced by
The type eZ\Publish\Core\FieldType\ImageAsset\Value was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Kaliop\eZMigrationBundle\API\FieldValueImporterInterface;
7
use Kaliop\eZMigrationBundle\API\FieldDefinitionConverterInterface;
8
use Kaliop\eZMigrationBundle\Core\Matcher\ContentMatcher;
9
10
class EzImageAsset extends AbstractFieldHandler implements FieldValueImporterInterface
11
{
12
    protected $contentMatcher;
13
14
    public function __construct(ContentMatcher $contentMatcher)
15
    {
16
        $this->contentMatcher = $contentMatcher;
17
    }
18
19
    /**
20
     * Creates a value object to use as the field value when setting an ez image asset field type.
21
     *
22
     * @param array|string|int $fieldValue The definition of the field value, structured in the yml file
23
     * @param array $context The context for execution of the current migrations. Contains f.e. the path to the migration
24
     * @return Value
25
     */
26
    public function hashToFieldValue($fieldValue, array $context = array())
27
    {
28
        $altText = '';
29
30
        if ($fieldValue === null) {
0 ignored issues
show
introduced by
The condition $fieldValue === null is always false.
Loading history...
31
            return new Value();
32
        }
33
        if (isset($fieldValue['alt_text'])) {
34
            $altText = $fieldValue['alt_text'];
35
        }
36
37
        if (is_array($fieldValue) && array_key_exists('destinationContentId', $fieldValue)) {
38
            // fromHash format
39
            $id = $fieldValue['destinationContentId'];
40
        } else {
41
            // simplified format
42
            $id = $fieldValue;
43
        }
44
45
        if ($id === null) {
46
            return new Value();
47
        }
48
49
        // 1. resolve relations
50
        $id = $this->referenceResolver->resolveReference($id);
51
        // 2. resolve remote ids
52
        $id = $this->contentMatcher->matchOneByKey($id)->id;
53
54
        return new Value($id, $altText);
55
    }
56
}