Metadata::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\DoctrineQueryFilter\Metadata;
6
7
use Arp\DoctrineQueryFilter\Metadata\Exception\MetadataException;
8
use Doctrine\ORM\Mapping\ClassMetadata;
9
use Doctrine\ORM\Mapping\MappingException;
10
11
final class Metadata implements MetadataInterface
12
{
13
    /**
14
     * @param ClassMetadata<object> $metadata
15
     */
16
    public function __construct(private readonly ClassMetadata $metadata)
17
    {
18
    }
19
20
    public function getName(): string
21
    {
22
        return $this->metadata->getName();
23
    }
24
25
    public function hasField(string $fieldName): bool
26
    {
27
        return $this->metadata->hasField($fieldName);
28
    }
29
30
    /**
31
     * @throws MetadataException
32
     */
33
    public function getFieldMapping(string $fieldName): array
34
    {
35
        try {
36
            return $this->metadata->getFieldMapping($fieldName);
37
        } catch (MappingException $e) {
38
            throw new MetadataException(
39
                sprintf('Unable to find field mapping for field \'%s::%s\'', $this->getName(), $fieldName),
40
                $e->getCode(),
41
                $e
42
            );
43
        }
44
    }
45
46
    /**
47
     * @throws MetadataException
48
     */
49
    public function getFieldType(string $fieldName): string
50
    {
51
        $type = $this->getFieldMapping($fieldName)['type'] ?? '';
52
53
        if (empty($type)) {
54
            throw new MetadataException(
55
                sprintf('Unable to resolve field data type for \'%s::%s\'', $this->getName(), $fieldName)
56
            );
57
        }
58
59
        return $type;
60
    }
61
62
    public function hasAssociation(string $fieldName): bool
63
    {
64
        return $this->metadata->hasAssociation($fieldName);
65
    }
66
67
    /**
68
     * @throws MetadataException
69
     */
70
    public function getAssociationMapping(string $fieldName): array
71
    {
72
        try {
73
            return $this->metadata->getAssociationMapping($fieldName);
74
        } catch (MappingException $e) {
75
            throw new MetadataException(
76
                sprintf('Unable to find association mapping for field \'%s::%s\'', $this->getName(), $fieldName),
77
                $e->getCode(),
78
                $e
79
            );
80
        }
81
    }
82
}
83