Test Setup Failed
Pull Request — master (#4)
by Alex
06:07
created

Metadata::getAssociationFiledMapping()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
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
/**
12
 * @author  Alex Patterson <[email protected]>
13
 * @package Arp\DoctrineQueryFilter\Metadata
14
 */
15
final class Metadata implements MetadataInterface
16
{
17
    /**
18
     * @var ClassMetadata
19
     */
20
    private ClassMetadata $metadata;
21
22
    /**
23
     * @param ClassMetadata $metadata
24
     */
25
    public function __construct(ClassMetadata $metadata)
26
    {
27
        $this->metadata = $metadata;
28
    }
29
30
    /**
31
     * @return string
32
     */
33
    public function getName(): string
34
    {
35
        return $this->metadata->getName();
36
    }
37
38
    /**
39
     * @param string $fieldName
40
     *
41
     * @return bool
42
     */
43
    public function hasField(string $fieldName): bool
44
    {
45
        return $this->metadata->hasField($fieldName);
46
    }
47
48
    /**
49
     * @param string $fieldName
50
     *
51
     * @return array
52
     *
53
     * @throws MetadataException
54
     */
55
    public function getFieldMapping(string $fieldName): array
56
    {
57
        try {
58
            return $this->metadata->getFieldMapping($fieldName);
59
        } catch (MappingException $e) {
60
            throw new MetadataException(
61
                sprintf(
62
                    'Unable to find field mapping for field \'%s::%s\': %s',
63
                    $this->getName(),
64
                    $fieldName,
65
                    $e->getMessage()
66
                ),
67
                $e->getCode(),
68
                $e
69
            );
70
        }
71
    }
72
73
    /**
74
     * @param string $fieldName
75
     *
76
     * @return string
77
     *
78
     * @throws MetadataException
79
     */
80
    public function getFieldType(string $fieldName): string
81
    {
82
        $type = $this->getFieldMapping($fieldName)['type'] ?? '';
83
84
        if (empty($type)) {
85
            throw new MetadataException(
86
                sprintf('Unable to resolve field data type for \'%s::%s\'', $this->getName(), $fieldName)
87
            );
88
        }
89
90
        return $type;
91
    }
92
93
    /**
94
     * @param string $fieldName
95
     *
96
     * @return bool
97
     */
98
    public function hasAssociation(string $fieldName): bool
99
    {
100
        return $this->metadata->hasAssociation($fieldName);
101
    }
102
103
    /**
104
     * @param string $fieldName
105
     *
106
     * @return array
107
     *
108
     * @throws MetadataException
109
     */
110
    public function getAssociationMapping(string $fieldName): array
111
    {
112
        try {
113
            return $this->metadata->getAssociationMapping($fieldName);
114
        } catch (MappingException $e) {
115
            throw new MetadataException(
116
                sprintf(
117
                    'Unable to find association mapping for field \'%s::%s\': %s',
118
                    $this->getName(),
119
                    $fieldName,
120
                    $e->getMessage()
121
                ),
122
                $e->getCode(),
123
                $e
124
            );
125
        }
126
    }
127
}
128