FieldDescription   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 1
dl 0
loc 86
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setAssociationMapping() 0 12 4
A getTargetEntity() 0 6 2
A setFieldMapping() 0 12 5
A setParentAssociationMappings() 0 10 3
A isIdentifier() 0 4 2
A getValue() 0 8 2
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\DoctrineMongoDBAdminBundle\Admin;
15
16
use Sonata\AdminBundle\Admin\BaseFieldDescription;
17
18
class FieldDescription extends BaseFieldDescription
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function __construct()
24
    {
25
        $this->parentAssociationMappings = [];
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function setAssociationMapping($associationMapping)
32
    {
33
        if (!\is_array($associationMapping)) {
34
            throw new \RuntimeException('The association mapping must be an array');
35
        }
36
37
        $this->associationMapping = $associationMapping;
38
39
        $this->type = $this->type ?: $associationMapping['type'];
40
        $this->mappingType = $this->mappingType ?: $associationMapping['type'];
41
        $this->fieldName = $associationMapping['fieldName'];
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function getTargetEntity()
48
    {
49
        if ($this->associationMapping) {
0 ignored issues
show
Bug Best Practice introduced by Thomas Rabaix
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...
50
            return $this->associationMapping['targetDocument'];
51
        }
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