• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 10537652610

24 Aug 2024 10:04AM UTC coverage: 7.707%. Remained the same
10537652610

push

github

dunglas
cleanup

12490 of 162060 relevant lines covered (7.71%)

22.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.47
/src/Doctrine/Odm/Metadata/Property/DoctrineMongoDbOdmPropertyMetadataFactory.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
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\Doctrine\Odm\Metadata\Property;
15

16
use ApiPlatform\Metadata\ApiProperty;
17
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
18
use Doctrine\ODM\MongoDB\DocumentManager;
19
use Doctrine\Persistence\ManagerRegistry;
20

21
/**
22
 * Use Doctrine metadata to populate the identifier property.
23
 *
24
 * @author Kévin Dunglas <dunglas@gmail.com>
25
 * @author Alan Poulain <contact@alanpoulain.eu>
26
 */
27
final class DoctrineMongoDbOdmPropertyMetadataFactory implements PropertyMetadataFactoryInterface
28
{
29
    public function __construct(private readonly ManagerRegistry $managerRegistry, private readonly PropertyMetadataFactoryInterface $decorated)
30
    {
31
    }
1,408✔
32

33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function create(string $resourceClass, string $property, array $options = []): ApiProperty
37
    {
38
        $propertyMetadata = $this->decorated->create($resourceClass, $property, $options);
320✔
39

40
        if (null !== $propertyMetadata->isIdentifier()) {
320✔
41
            return $propertyMetadata;
8✔
42
        }
43

44
        $manager = $this->managerRegistry->getManagerForClass($resourceClass);
320✔
45
        if (!$manager instanceof DocumentManager) {
320✔
46
            return $propertyMetadata;
78✔
47
        }
48

49
        $doctrineClassMetadata = $manager->getClassMetadata($resourceClass);
256✔
50
        $identifiers = $doctrineClassMetadata->getIdentifier();
256✔
51

52
        foreach ($identifiers as $identifier) {
256✔
53
            if ($identifier === $property) {
256✔
54
                $propertyMetadata = $propertyMetadata->withIdentifier(true);
188✔
55

56
                if (null !== $propertyMetadata->isWritable()) {
188✔
57
                    break;
188✔
58
                }
59

60
                $propertyMetadata = $propertyMetadata->withWritable(false);
×
61

62
                break;
×
63
            }
64
        }
65

66
        if (null === $propertyMetadata->isIdentifier()) {
256✔
67
            $propertyMetadata = $propertyMetadata->withIdentifier(false);
254✔
68
        }
69

70
        return $propertyMetadata;
256✔
71
    }
72
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc