FieldDescription::isIdentifier()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
    public function __construct()
21
    {
22
        $this->parentAssociationMappings = [];
23
    }
24
25
    public function setAssociationMapping($associationMapping): void
26
    {
27
        if (!\is_array($associationMapping)) {
28
            throw new \RuntimeException('The association mapping must be an array');
29
        }
30
31
        $this->associationMapping = $associationMapping;
32
33
        $this->type = $this->type ?: $associationMapping['type'];
34
        $this->mappingType = $this->mappingType ?: $associationMapping['type'];
35
        $this->fieldName = $associationMapping['fieldName'];
36
    }
37
38
    /**
39
     * NEXT_MAJOR: Remove this method.
40
     *
41
     * @deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x and will be removed in version 4.0. Use FieldDescription::getTargetModel() instead.
42
     */
43
    public function getTargetEntity()
44
    {
45
        @trigger_error(sprintf(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
46
            'Method %s() is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x and will be removed in version 4.0.'
47
            .' Use %s::getTargetModel() instead.',
48
            __METHOD__,
49
            __CLASS__
50
        ), E_USER_DEPRECATED);
51
52
        return $this->getTargetModel();
53
    }
54
55
    /**
56
     * @final since sonata-project/doctrine-mongodb-admin-bundle 3.x.
57
     */
58
    public function getTargetModel(): ?string
59
    {
60
        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...
61
            return $this->associationMapping['targetDocument'];
62
        }
63
64
        return null;
65
    }
66
67
    public function setFieldMapping($fieldMapping): void
68
    {
69
        if (!\is_array($fieldMapping)) {
70
            throw new \RuntimeException('The field mapping must be an array');
71
        }
72
73
        $this->fieldMapping = $fieldMapping;
74
75
        $this->type = $this->type ?: $fieldMapping['type'];
76
        $this->mappingType = $this->mappingType ?: $fieldMapping['type'];
77
        $this->fieldName = $this->fieldName ?: $fieldMapping['fieldName'];
78
    }
79
80
    public function setParentAssociationMappings(array $parentAssociationMappings): void
81
    {
82
        foreach ($parentAssociationMappings as $parentAssociationMapping) {
83
            if (!\is_array($parentAssociationMapping)) {
84
                throw new \RuntimeException('An association mapping must be an array');
85
            }
86
        }
87
88
        $this->parentAssociationMappings = $parentAssociationMappings;
89
    }
90
91
    public function isIdentifier()
92
    {
93
        return $this->fieldMapping['id'] ?? false;
94
    }
95
96
    public function getValue($object)
97
    {
98
        foreach ($this->parentAssociationMappings as $parentAssociationMapping) {
99
            $object = $this->getFieldValue($object, $parentAssociationMapping['fieldName']);
100
        }
101
102
        return $this->getFieldValue($object, $this->fieldName);
103
    }
104
}
105