Completed
Push — master ( 2915b6...fe9ad6 )
by Kévin
13s
created

IdentifierManagerTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 52
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C normalizeIdentifiers() 0 38 8
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
namespace ApiPlatform\Core\Bridge\Doctrine\Orm\Util;
13
14
use ApiPlatform\Core\Exception\PropertyNotFoundException;
15
use Doctrine\Common\Persistence\ObjectManager;
16
17
/**
18
 * @internal
19
 */
20
trait IdentifierManagerTrait
21
{
22
    /**
23
     * Transform and check the identifier, composite or not.
24
     *
25
     * @param int|string    $id
26
     * @param ObjectManager $manager
27
     * @param string        $resourceClass
28
     *
29
     * @throws PropertyNotFoundException
30
     *
31
     * @return array
32
     */
33
    public function normalizeIdentifiers($id, ObjectManager $manager, string $resourceClass): array
34
    {
35
        $identifierValues = [$id];
36
        $doctrineMetadataIdentifier = $manager->getClassMetadata($resourceClass)->getIdentifier();
37
38
        if (2 <= count($doctrineMetadataIdentifier)) {
39
            $identifiers = explode(';', $id);
40
            $identifiersMap = [];
41
42
            // first transform identifiers to a proper key/value array
43
            foreach ($identifiers as $identifier) {
44
                $keyValue = explode('=', $identifier);
45
                $identifiersMap[$keyValue[0]] = $keyValue[1];
46
            }
47
        }
48
49
        $identifiers = [];
50
        $i = 0;
51
52
        foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $propertyName) {
0 ignored issues
show
Bug introduced by
The property propertyNameCollectionFactory does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
53
            $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName);
0 ignored issues
show
Bug introduced by
The property propertyMetadataFactory does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
54
55
            $identifier = $propertyMetadata->isIdentifier();
56
            if (null === $identifier || false === $identifier) {
57
                continue;
58
            }
59
60
            $identifier = !isset($identifiersMap) ? $identifierValues[$i] ?? null : $identifiersMap[$propertyName] ?? null;
61
            if (null === $identifier) {
62
                throw new PropertyNotFoundException(sprintf('Invalid identifier "%s", "%s" has not been found.', $id, $propertyName));
63
            }
64
65
            $identifiers[$propertyName] = $identifier;
66
            ++$i;
67
        }
68
69
        return $identifiers;
70
    }
71
}
72