Passed
Pull Request — master (#4)
by Pieter
01:45
created

DiscriminatorColumn   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 47
rs 10
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getPriority() 0 3 1
A __construct() 0 4 1
A toType() 0 3 1
A getName() 0 3 1
A getValue() 0 14 5
1
<?php
2
3
namespace W2w\Lib\ApieObjectAccessNormalizer\ObjectAccess\Getters;
4
5
use Symfony\Component\PropertyInfo\Type;
6
use W2w\Lib\ApieObjectAccessNormalizer\Getters\GetterInterface;
7
8
class DiscriminatorColumn implements GetterInterface
9
{
10
    /**
11
     * @var string
12
     */
13
    private $name;
14
15
    /**
16
     * @var array
17
     */
18
    private $discriminatorMapping;
19
20
    public function __construct(string $name, array $discriminatorMapping)
21
    {
22
        $this->name = $name;
23
        $this->discriminatorMapping = $discriminatorMapping;
24
    }
25
26
    public function getName(): string
27
    {
28
        return $this->name;
29
    }
30
31
    public function getValue($object)
32
    {
33
        $objectClassName = get_class($object);
34
        foreach ($this->discriminatorMapping as $value => $className) {
35
            if ($objectClassName === $className) {
36
                return $value;
37
            }
38
        }
39
        foreach ($this->discriminatorMapping as $value => $className) {
40
            if (is_a($objectClassName, $className)) {
41
                return $value;
42
            }
43
        }
44
        return null;
45
    }
46
47
    public function toType(): ?Type
48
    {
49
        return new Type(Type::BUILTIN_TYPE_STRING);
50
    }
51
52
    public function getPriority(): int
53
    {
54
        return 15;
55
    }
56
}
57