Passed
Push — master ( e18bff...6e2c26 )
by Kévin
04:13
created

DoctrineExtractor::isReadable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[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
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm\PropertyInfo;
15
16
use Doctrine\Common\Collections\Collection;
17
use Doctrine\Common\Persistence\Mapping\MappingException;
18
use Doctrine\Common\Persistence\ObjectManager;
19
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
20
use Doctrine\ODM\MongoDB\Types\Type as MongoDbType;
21
use Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface;
22
use Symfony\Component\PropertyInfo\PropertyListExtractorInterface;
23
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
24
use Symfony\Component\PropertyInfo\Type;
25
26
/**
27
 * Extracts data using Doctrine MongoDB ODM metadata.
28
 *
29
 * @experimental
30
 *
31
 * @author Kévin Dunglas <[email protected]>
32
 * @author Alan Poulain <[email protected]>
33
 */
34
final class DoctrineExtractor implements PropertyListExtractorInterface, PropertyTypeExtractorInterface, PropertyAccessExtractorInterface
35
{
36
    private $objectManager;
37
38
    public function __construct(ObjectManager $objectManager)
39
    {
40
        $this->objectManager = $objectManager;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function getProperties($class, array $context = [])
47
    {
48
        if (null === $metadata = $this->getMetadata($class)) {
49
            return null;
50
        }
51
52
        return $metadata->getFieldNames();
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function getTypes($class, $property, array $context = [])
59
    {
60
        if (null === $metadata = $this->getMetadata($class)) {
61
            return null;
62
        }
63
64
        $reflectionMetadata = new \ReflectionClass($metadata);
65
66
        if ($metadata->hasAssociation($property)) {
67
            $class = $metadata->getAssociationTargetClass($property);
68
69
            if ($metadata->isSingleValuedAssociation($property)) {
70
                if ($reflectionMetadata->hasMethod('isNullable')) {
71
                    $nullable = $metadata->isNullable($property);
72
                } else {
73
                    $nullable = false;
74
                }
75
76
                return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, $class)];
77
            }
78
79
            $collectionKeyType = Type::BUILTIN_TYPE_INT;
80
81
            return [
82
                new Type(
83
                    Type::BUILTIN_TYPE_OBJECT,
84
                    false,
85
                    Collection::class,
86
                    true,
87
                    new Type($collectionKeyType),
88
                    new Type(Type::BUILTIN_TYPE_OBJECT, false, $class)
89
                ),
90
            ];
91
        }
92
93
        if ($metadata->hasField($property)) {
94
            $typeOfField = $metadata->getTypeOfField($property);
95
            $nullable = $reflectionMetadata->hasMethod('isNullable') && $metadata->isNullable($property);
96
97
            switch ($typeOfField) {
98
                case MongoDbType::DATE:
99
                    return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, 'DateTime')];
100
                case MongoDbType::HASH:
101
                    return [new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true)];
102
                case MongoDbType::COLLECTION:
103
                    return [new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true, new Type(Type::BUILTIN_TYPE_INT))];
104
                default:
105
                    $builtinType = $this->getPhpType($typeOfField);
0 ignored issues
show
Bug introduced by
It seems like $typeOfField can also be of type null; however, parameter $doctrineType of ApiPlatform\Core\Bridge\...Extractor::getPhpType() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

105
                    $builtinType = $this->getPhpType(/** @scrutinizer ignore-type */ $typeOfField);
Loading history...
106
107
                    return $builtinType ? [new Type($builtinType, $nullable)] : null;
108
            }
109
        }
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115
    public function isReadable($class, $property, array $context = []): ?bool
116
    {
117
        return null;
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123
    public function isWritable($class, $property, array $context = []): ?bool
124
    {
125
        if (
126
            null === ($metadata = $this->getMetadata($class))
127
            || ClassMetadata::GENERATOR_TYPE_NONE === $metadata->generatorType
128
            || !\in_array($property, $metadata->getIdentifierFieldNames(), true)
129
        ) {
130
            return null;
131
        }
132
133
        return false;
134
    }
135
136
    private function getMetadata(string $class): ?ClassMetadata
137
    {
138
        try {
139
            return $this->objectManager->getClassMetadata($class);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->objectMana...etClassMetadata($class) returns the type Doctrine\Common\Persistence\Mapping\ClassMetadata which includes types incompatible with the type-hinted return Doctrine\ODM\MongoDB\Mapping\ClassMetadata|null.
Loading history...
140
        } catch (MappingException $exception) {
141
            return null;
142
        }
143
    }
144
145
    /**
146
     * Gets the corresponding built-in PHP type.
147
     */
148
    private function getPhpType(string $doctrineType): ?string
149
    {
150
        switch ($doctrineType) {
151
            case MongoDbType::INTEGER:
152
            case MongoDbType::INT:
153
            case MongoDbType::INTID:
154
            case MongoDbType::KEY:
155
                return Type::BUILTIN_TYPE_INT;
156
            case MongoDbType::FLOAT:
157
                return Type::BUILTIN_TYPE_FLOAT;
158
            case MongoDbType::STRING:
159
            case MongoDbType::ID:
160
            case MongoDbType::OBJECTID:
161
            case MongoDbType::TIMESTAMP:
162
            case MongoDbType::BINDATA:
163
            case MongoDbType::BINDATABYTEARRAY:
164
            case MongoDbType::BINDATACUSTOM:
165
            case MongoDbType::BINDATAFUNC:
166
            case MongoDbType::BINDATAMD5:
167
            case MongoDbType::BINDATAUUID:
168
            case MongoDbType::BINDATAUUIDRFC4122:
169
                return Type::BUILTIN_TYPE_STRING;
170
            case MongoDbType::BOOLEAN:
171
            case MongoDbType::BOOL:
172
                return Type::BUILTIN_TYPE_BOOL;
173
        }
174
175
        return null;
176
    }
177
}
178