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

FieldHandlerManager::hashToFieldValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 4
dl 0
loc 13
ccs 7
cts 8
cp 0.875
crap 3.0175
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core;
4
5
use Kaliop\eZMigrationBundle\API\FieldValueImporterInterface;
6
use Kaliop\eZMigrationBundle\API\FieldDefinitionConverterInterface;
7
use Kaliop\eZMigrationBundle\API\FieldValueConverterInterface;
8
use eZ\Publish\API\Repository\FieldTypeService;
9
10
class FieldHandlerManager
11
{
12
    /** @var FieldValueImporterInterface[][] */
13
    protected $fieldTypeMap;
14
    protected $fieldTypeService;
15
16 80
    public function __construct(FieldTypeService $fieldTypeService)
17
    {
18 80
        $this->fieldTypeService = $fieldTypeService;
19 80
    }
20
21
    /**
22
     * @param FieldValueImporterInterface $fieldHandler
23
     * @param string $fieldTypeIdentifier
24
     * @param string $contentTypeIdentifier
25
     * @throws \Exception
26
     */
27 80
    public function addFieldHandler($fieldHandler, $fieldTypeIdentifier, $contentTypeIdentifier = null)
28
    {
29
        // This is purely BC; at some point we will typehint to FieldValueImporterInterface
30 80
        if (!$fieldHandler instanceof FieldValueImporterInterface) {
0 ignored issues
show
introduced by
$fieldHandler is always a sub-type of Kaliop\eZMigrationBundle...dValueImporterInterface.
Loading history...
31
            throw new \Exception("Can not register object of class '" . get_class($fieldHandler) . "' as field handler because it does not support the desired interface");
32
        }
33
34 80
        if ($contentTypeIdentifier == null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $contentTypeIdentifier of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
35 80
            $contentTypeIdentifier = '*';
36
        }
37 80
        $this->fieldTypeMap[$contentTypeIdentifier][$fieldTypeIdentifier] = $fieldHandler;
38 80
    }
39
40
    /**
41
     * @param string $fieldTypeIdentifier
42
     * @param string $contentTypeIdentifier
43
     * @return bool
44
     */
45 20
    public function managesField($fieldTypeIdentifier, $contentTypeIdentifier)
46
    {
47 20
        return (isset($this->fieldTypeMap[$contentTypeIdentifier][$fieldTypeIdentifier]) ||
48 20
            isset($this->fieldTypeMap['*'][$fieldTypeIdentifier]));
49
    }
50
51
    /**
52
     * Returns true when a fieldHandler allows string refs to be pre-resolved for the field value it gets as hash.
53
     * "pre-resolved" means: resolved before the field-handler-specific processing kicks in
54
     *
55
     * @param string $fieldTypeIdentifier
56
     * @param string $contentTypeIdentifier
57
     * @return bool
58
     */
59 1
    public function doPreResolveStringReferences($fieldTypeIdentifier, $contentTypeIdentifier)
60
    {
61 1
        if (!$this->managesField($fieldTypeIdentifier, $contentTypeIdentifier)) {
62
            return true;
63
        }
64
65 1
        $fieldHandler = $this->getFieldHandler($fieldTypeIdentifier, $contentTypeIdentifier);
66
        // BC: not always defined
67 1
        if (is_callable(array($fieldHandler, 'doPreResolveStringReferences'))) {
68
            return $fieldHandler->doPreResolveStringReferences();
0 ignored issues
show
Bug introduced by
The method doPreResolveStringReferences() does not exist on Kaliop\eZMigrationBundle...dValueImporterInterface. ( Ignorable by Annotation )

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

68
            return $fieldHandler->/** @scrutinizer ignore-call */ doPreResolveStringReferences();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
69
        }
70
71 1
        return true;
72
    }
73
74
    /**
75
     * @param string $fieldTypeIdentifier
76
     * @param string $contentTypeIdentifier
77
     * @param mixed $hashValue
78
     * @param array $context
79
     * @return mixed
80
     */
81 5
    public function hashToFieldValue($fieldTypeIdentifier, $contentTypeIdentifier, $hashValue, array $context = array())
82
    {
83 5
        if ($this->managesField($fieldTypeIdentifier, $contentTypeIdentifier)) {
84 3
            $fieldHandler = $this->getFieldHandler($fieldTypeIdentifier, $contentTypeIdentifier);
85
            // BC
86 3
            if (!$fieldHandler instanceof FieldValueImporterInterface) {
0 ignored issues
show
introduced by
$fieldHandler is always a sub-type of Kaliop\eZMigrationBundle...dValueImporterInterface.
Loading history...
87
                return $fieldHandler->createValue($hashValue, $context);
88
            }
89 3
            return $fieldHandler->hashToFieldValue($hashValue, $context);
90
        }
91
92 3
        $fieldType = $this->fieldTypeService->getFieldType($fieldTypeIdentifier);
93 3
        return $fieldType->fromHash($hashValue);
94
    }
95
96
    /**
97
     * @param string $fieldTypeIdentifier
98
     * @param string $contentTypeIdentifier
99
     * @param \eZ\Publish\SPI\FieldType\Value $value
100
     * @param array $context
101
     * @return mixed
102
     */
103 7
    public function fieldValueToHash($fieldTypeIdentifier, $contentTypeIdentifier, $value, array $context = array())
104
    {
105 7
        if ($this->managesField($fieldTypeIdentifier, $contentTypeIdentifier)) {
106 1
            $fieldHandler = $this->getFieldHandler($fieldTypeIdentifier, $contentTypeIdentifier);
107 1
            if ($fieldHandler instanceof FieldValueConverterInterface) {
108
                return $fieldHandler->fieldValueToHash($value, $context);
109
            }
110
        }
111
112 7
        $fieldType = $this->fieldTypeService->getFieldType($fieldTypeIdentifier);
113 7
        return $fieldType->toHash($value);
114
    }
115
116
    /**
117
     * @param string $fieldTypeIdentifier
118
     * @param string $contentTypeIdentifier
119
     * @return bool
120
     */
121 7
    public function managesFieldDefinition($fieldTypeIdentifier, $contentTypeIdentifier)
122
    {
123 7
        if (!$this->managesField($fieldTypeIdentifier, $contentTypeIdentifier)) {
124 7
            return false;
125
        }
126
127 6
        $fieldHandler = $this->getFieldHandler($fieldTypeIdentifier, $contentTypeIdentifier);
128 6
        return ($fieldHandler instanceof FieldDefinitionConverterInterface);
129
    }
130
131
    /**
132
     * @param string $fieldTypeIdentifier
133
     * @param string $contentTypeIdentifier
134
     * @param mixed $fieldSettingsHash
135
     * @param array $context
136
     * @return mixed
137
     */
138 1
    public function hashToFieldSettings($fieldTypeIdentifier, $contentTypeIdentifier, $fieldSettingsHash, array $context = array())
139
    {
140 1
        if ($this->managesFieldDefinition($fieldTypeIdentifier, $contentTypeIdentifier)) {
141 1
            return $this->getFieldHandler($fieldTypeIdentifier, $contentTypeIdentifier)->hashToFieldSettings($fieldSettingsHash, $context);
0 ignored issues
show
Bug introduced by
The method hashToFieldSettings() does not exist on Kaliop\eZMigrationBundle...dValueImporterInterface. Did you maybe mean hashToFieldValue()? ( Ignorable by Annotation )

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

141
            return $this->getFieldHandler($fieldTypeIdentifier, $contentTypeIdentifier)->/** @scrutinizer ignore-call */ hashToFieldSettings($fieldSettingsHash, $context);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
142
        }
143
144
        return $fieldSettingsHash;
145
    }
146
147
    /**
148
     * @param string $fieldTypeIdentifier
149
     * @param string $contentTypeIdentifier
150
     * @param mixed $fieldSettings
151
     * @param array $context
152
     * @return mixed
153
     */
154 5
    public function fieldSettingsToHash($fieldTypeIdentifier, $contentTypeIdentifier, $fieldSettings, array $context = array())
155
    {
156 5
        if ($this->managesFieldDefinition($fieldTypeIdentifier, $contentTypeIdentifier)) {
157 1
            return $this->getFieldHandler($fieldTypeIdentifier, $contentTypeIdentifier)->fieldSettingsToHash($fieldSettings, $context);
0 ignored issues
show
Bug introduced by
The method fieldSettingsToHash() does not exist on Kaliop\eZMigrationBundle...dValueImporterInterface. It seems like you code against a sub-type of Kaliop\eZMigrationBundle...dValueImporterInterface such as Kaliop\eZMigrationBundle...FieldHandler\EzRelation or Kaliop\eZMigrationBundle...ieldHandler\EzSelection or Kaliop\eZMigrationBundle...\FieldHandler\EzXmlText or Kaliop\eZMigrationBundle...ldHandler\EzDateAndTime. ( Ignorable by Annotation )

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

157
            return $this->getFieldHandler($fieldTypeIdentifier, $contentTypeIdentifier)->/** @scrutinizer ignore-call */ fieldSettingsToHash($fieldSettings, $context);
Loading history...
158
        }
159
160 5
        return $fieldSettings;
161
    }
162
163
    /**
164
     * @param string $fieldTypeIdentifier
165
     * @param string $contentTypeIdentifier
166
     * @return FieldValueImporterInterface
167
     * @throws \Exception
168
     */
169 7
    protected function getFieldHandler($fieldTypeIdentifier, $contentTypeIdentifier) {
170 7
        if (isset($this->fieldTypeMap[$contentTypeIdentifier][$fieldTypeIdentifier])) {
171
            return $this->fieldTypeMap[$contentTypeIdentifier][$fieldTypeIdentifier];
172 7
        } else if (isset($this->fieldTypeMap['*'][$fieldTypeIdentifier])) {
173 7
            return $this->fieldTypeMap['*'][$fieldTypeIdentifier];
174
        }
175
176
        throw new \Exception("No complex field handler registered for field '$fieldTypeIdentifier' in content type '$contentTypeIdentifier'");
177
    }
178
}
179