IdentifierExtractor   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 80
rs 10
c 0
b 0
f 0
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getIdentifierKeyOfClass() 0 26 5
A getIdentifierValue() 0 7 2
A getIdentifierKey() 0 12 4
A __construct() 0 3 1
1
<?php
2
namespace W2w\Lib\Apie\Core;
3
4
use ReflectionClass;
5
use ReflectionException;
6
use ReflectionMethod;
7
use ReflectionProperty;
8
use W2w\Lib\ApieObjectAccessNormalizer\ObjectAccess\ObjectAccessInterface;
9
10
/**
11
 * Extracts the identifier from a resource.
12
 */
13
class IdentifierExtractor
14
{
15
    private $objectAccess;
16
17
    public function __construct(ObjectAccessInterface $objectAccess)
18
    {
19
        $this->objectAccess = $objectAccess;
20
    }
21
22
    /**
23
     * Determines the identifier from a class without having an instance of the class.
24
     *
25
     * @param string $className
26
     * @param array $context
27
     * @return string|null
28
     */
29
    public function getIdentifierKeyOfClass(string $className, array $context = []): ?string
30
    {
31
        if (!empty($context['identifier'])) {
32
            return $context['identifier'];
33
        }
34
        $todo = [
35
            [ReflectionMethod::class, 'getId', 'id'],
36
            [ReflectionMethod::class, 'id', 'id'],
37
            [ReflectionProperty::class, 'id', 'id'],
38
            [ReflectionMethod::class, 'getUuid', 'uuid'],
39
            [ReflectionMethod::class, 'uuid', 'uuid'],
40
            [ReflectionProperty::class, 'uuid', 'uuid'],
41
        ];
42
        while (!empty($todo)) {
43
            list($reflectionClass, $property, $result) = array_shift($todo);
44
            try {
45
                /** @var ReflectionProperty|ReflectionMethod $test */
46
                $test = new $reflectionClass($className, $property);
47
                if ($test->isPublic()) {
48
                    return $result;
49
                }
50
            } catch (ReflectionException $e) {
51
                $e->getMessage();//ignore
52
            }
53
        }
54
        return null;
55
    }
56
57
    /**
58
     * Returns the name of the identifier of a resource. If it could not be determined,
59
     * it returns null.
60
     *
61
     * @param mixed $resource
62
     * @param array $context
63
     * @return string|null
64
     */
65
    public function getIdentifierKey($resource, array $context = []): ?string
66
    {
67
        if (isset($context['identifier'])) {
68
            return $context['identifier'];
69
        }
70
        $fields = $this->objectAccess->getGetterFields(new ReflectionClass($resource));
71
        foreach (['id', 'uuid'] as $id) {
72
            if (in_array($id, $fields)) {
73
                return $id;
74
            }
75
        }
76
        return null;
77
    }
78
79
    /**
80
     * Return the value of the identifer of a resource.
81
     *
82
     * @param mixed $resource
83
     * @param array $context
84
     * @return mixed|null
85
     */
86
    public function getIdentifierValue($resource, array $context = [])
87
    {
88
        $key = $this->getIdentifierKey($resource, $context);
89
        if (empty($key)) {
90
            return null;
91
        }
92
        return $this->objectAccess->getValue($resource, $key);
93
    }
94
}
95