FieldDescription   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 1
dl 0
loc 98
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setAssociationMapping() 0 16 5
A getTargetEntity() 0 9 3
A setFieldMapping() 0 12 5
A isIdentifier() 0 4 2
A getValue() 0 8 2
A setParentAssociationMappings() 0 10 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\DoctrinePHPCRAdminBundle\Admin;
15
16
use Sonata\AdminBundle\Admin\BaseFieldDescription;
17
18
/**
19
 * {@inheritdoc}
20
 */
21
class FieldDescription extends BaseFieldDescription
22
{
23
    public function __construct()
24
    {
25
        $this->parentAssociationMappings = [];
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     *
31
     * @throws \InvalidArgumentException if the mapping is no array or of an
32
     *                                   unknown type
33
     */
34
    public function setAssociationMapping($associationMapping): void
35
    {
36
        if (!\is_array($associationMapping)) {
37
            throw new \InvalidArgumentException('The association mapping must be an array');
38
        }
39
40
        $this->associationMapping = $associationMapping;
41
42
        if (isset($associationMapping['type'])) {
43
            $this->type = $this->type ?: $associationMapping['type'];
44
            $this->mappingType = $this->mappingType ?: $associationMapping['type'];
45
        } else {
46
            throw new \InvalidArgumentException('Unknown association mapping type');
47
        }
48
        $this->fieldName = $associationMapping['fieldName'];
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function getTargetEntity()
55
    {
56
        if (isset($this->associationMapping['targetDocument'])) {
57
            return $this->associationMapping['targetDocument'];
58
        }
59
        if (isset($this->associationMapping['referringDocument'])) {
60
            return $this->associationMapping['referringDocument'];
61
        }
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     *
67
     * @throws \InvalidArgumentException if the mapping information is not an array
68
     */
69
    public function setFieldMapping($fieldMapping): void
70
    {
71
        if (!\is_array($fieldMapping)) {
72
            throw new \InvalidArgumentException('The field mapping must be an array');
73
        }
74
75
        $this->fieldMapping = $fieldMapping;
76
77
        $this->type = $this->type ?: $fieldMapping['type'];
78
        $this->mappingType = $this->mappingType ?: $fieldMapping['type'];
79
        $this->fieldName = $this->fieldName ?: $fieldMapping['fieldName'];
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function isIdentifier()
86
    {
87
        return isset($this->fieldMapping['id']) ? $this->fieldMapping['id'] : false;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function getValue($object)
94
    {
95
        foreach ($this->parentAssociationMappings as $parentAssociationMapping) {
96
            $object = $this->getFieldValue($object, $parentAssociationMapping['fieldName']);
97
        }
98
99
        return $this->getFieldValue($object, $this->fieldName);
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     *
105
     * @throws \InvalidArgumentException if the list of mappings does contain
106
     *                                   something else than arrays
107
     */
108
    public function setParentAssociationMappings(array $parentAssociationMappings): void
109
    {
110
        foreach ($parentAssociationMappings as $parentAssociationMapping) {
111
            if (!\is_array($parentAssociationMapping)) {
112
                throw new \RuntimeException('An association mapping must be an array');
113
            }
114
        }
115
116
        $this->parentAssociationMappings = $parentAssociationMappings;
117
    }
118
}
119