Extractor::getIdName()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Pgs\ElasticOM;
4
5
use Pgs\ElasticOM\Annotation\ClassMetadata;
6
use Pgs\ElasticOM\Annotation\Id;
7
8
class Extractor
9
{
10 3
    public function getIdName(string $entityClass): string
11
    {
12 3
        $ids = array_keys(array_filter(
13 3
            ClassMetadata::getRawConfig($entityClass),
14 3
            function (array $values) {
15 3
                return !empty($values[Id::KEY_NAME]);
16 3
            }
17
        ));
18
19 3
        if (count($ids) !== 1) {
20 2
            throw new Exception(sprintf(
21 2
                'No or more than one identifier specified for entity "%s". Every entity must have one identifier',
22
                $entityClass
23
            ));
24
        }
25
26 1
        return reset($ids);
27
    }
28
29 1
    public function getFieldNames(string $entityClass): array
30
    {
31 1
        return array_keys(ClassMetadata::getRawConfig($entityClass));
32
    }
33
}
34