Completed
Push — 2.x ( 359f6e )
by Sullivan
14:45
created

FieldDescription   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1
Metric Value
wmc 19
lcom 1
cbo 1
dl 0
loc 88
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A isIdentifier() 0 4 2
A getTargetEntity() 0 8 2
A setParentAssociationMappings() 0 10 3
A getValue() 0 8 2
A setAssociationMapping() 0 12 4
B setFieldMapping() 0 12 5
1
<?php
2
3
/*
4
 * This file is part of the Sonata package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\DoctrineORMAdminBundle\Admin;
13
14
use Sonata\AdminBundle\Admin\BaseFieldDescription;
15
16
class FieldDescription extends BaseFieldDescription
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21
    public function __construct()
22
    {
23
        $this->parentAssociationMappings = array();
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function setAssociationMapping($associationMapping)
30
    {
31
        if (!is_array($associationMapping)) {
32
            throw new \RuntimeException('The association mapping must be an array');
33
        }
34
35
        $this->associationMapping = $associationMapping;
36
37
        $this->type        = $this->type ? : $associationMapping['type'];
38
        $this->mappingType = $this->mappingType ? : $associationMapping['type'];
39
        $this->fieldName   = $associationMapping['fieldName'];
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function getTargetEntity()
46
    {
47
        if ($this->associationMapping) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->associationMapping of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
48
            return $this->associationMapping['targetEntity'];
49
        }
50
51
        return null;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function setFieldMapping($fieldMapping)
58
    {
59
        if (!is_array($fieldMapping)) {
60
            throw new \RuntimeException('The field mapping must be an array');
61
        }
62
63
        $this->fieldMapping = $fieldMapping;
64
65
        $this->type        = $this->type ? : $fieldMapping['type'];
66
        $this->mappingType = $this->mappingType ? : $fieldMapping['type'];
67
        $this->fieldName   = $this->fieldName ? : $fieldMapping['fieldName'];
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function setParentAssociationMappings(array $parentAssociationMappings)
74
    {
75
        foreach ($parentAssociationMappings as $parentAssociationMapping) {
76
            if (!is_array($parentAssociationMapping)) {
77
                throw new \RuntimeException('An association mapping must be an array');
78
            }
79
        }
80
81
        $this->parentAssociationMappings = $parentAssociationMappings;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function isIdentifier()
88
    {
89
        return isset($this->fieldMapping['id']) ? $this->fieldMapping['id'] : false;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function getValue($object)
96
    {
97
        foreach ($this->parentAssociationMappings as $parentAssociationMapping) {
98
            $object = $this->getFieldValue($object, $parentAssociationMapping['fieldName']);
99
        }
100
101
        return $this->getFieldValue($object, $this->fieldName);
102
    }
103
}
104