Completed
Push — master ( 12f48c...b47ac0 )
by Gaetano
06:59
created

ComplexFieldManager::getComplexFieldValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
cc 2
eloc 7
c 3
b 0
f 2
nc 2
nop 3
dl 0
loc 15
rs 9.4285
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\ComplexField;
4
5
use Kaliop\eZMigrationBundle\API\ComplexFieldInterface;
6
use eZ\Publish\API\Repository\FieldTypeService;
7
8
class ComplexFieldManager
9
{
10
    /** @var ComplexFieldInterface[]  */
11
    protected $fieldTypeMap;
12
    protected $fieldTypeService;
13
14
    public function __construct(FieldTypeService $fieldTypeService)
15
    {
16
        $this->fieldTypeService = $fieldTypeService;
17
    }
18
19
    /**
20
     * @param ComplexFieldInterface $complexField
21
     * @param string $contentTypeIdentifier
22
     */
23
    public function addComplexField(ComplexFieldInterface $complexField, $contentTypeIdentifier)
24
    {
25
        $this->fieldTypeMap[$contentTypeIdentifier] = $complexField;
26
    }
27
28
    /**
29
     * @param string $fieldTypeIdentifier
30
     * @param array $fieldValueArray
31
     * @param array $context
32
     * @return ComplexFieldInterface
33
     */
34
    public function getComplexFieldValue($fieldTypeIdentifier, array $fieldValueArray, array $context = array())
35
    {
36
        if (array_key_exists($fieldTypeIdentifier, $this->fieldTypeMap))
37
        {
38
            $fieldService = $this->fieldTypeMap[$fieldTypeIdentifier];
39
            return $fieldService->createValue($fieldValueArray, $context);
40
        }
41
        else
42
        {
43
            $fieldType = $this->fieldTypeService->getFieldType($fieldTypeIdentifier);
44
            return $fieldType->fromHash($fieldValueArray);
45
            // was: error
46
            //throw new \InvalidArgumentException("Field of type '$fieldTypeIdentifier' can not be handled as it does not have a complex field class defined");
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
47
        }
48
    }
49
}
50