Passed
Push — master ( 819f30...8ceff2 )
by Thorsten
02:22
created

Attribute   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 4
dl 0
loc 67
ccs 18
cts 24
cp 0.75
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A define() 0 13 3
A makeValue() 0 8 2
A getValueType() 0 4 1
A getName() 0 4 1
A getEntityType() 0 4 1
A getParent() 0 4 1
A __construct() 0 6 1
1
<?php
2
3
namespace Daikon\Entity\EntityType;
4
5
use Daikon\Entity\Assert\Assertion;
6
use Daikon\Entity\Entity\EntityInterface;
7
use Daikon\Entity\Error\InvalidType;
8
use Daikon\Entity\Error\MissingImplementation;
9
use Daikon\Entity\ValueObject\ValueObjectInterface;
10
11
class Attribute implements AttributeInterface
12
{
13
    /**
14
     * @var string
15
     */
16
    private $name;
17
18
    /**
19
     * @var EntityTypeInterface
20
     */
21
    private $entityType;
22
23
    /**
24
     * @var string
25
     */
26
    private $valueImplementor;
27
28 58
    public static function define(
29
        string $name,
30
        $valueImplementor,
31
        EntityTypeInterface $entityType
32
    ): AttributeInterface {
33 58
        if (!class_exists($valueImplementor)) {
34
            throw new MissingImplementation("Unable to load VO class $valueImplementor");
35
        }
36 58
        if (!is_subclass_of($valueImplementor, ValueObjectInterface::class)) {
37
            throw new InvalidType("Given VO class $valueImplementor does not implement ".ValueObjectInterface::class);
38
        }
39 58
        return new static($name, $entityType, $valueImplementor);
40
    }
41
42 32
    public function makeValue($value = null, EntityInterface $parent = null): ValueObjectInterface
43
    {
44 32
        if (is_object($value)) {
45
            Assertion::isInstanceOf($value, $this->valueImplementor);
46
            return $value;
47
        }
48 32
        return call_user_func([$this->valueImplementor, 'fromNative'], $value);
49
    }
50
51
    public function getValueType(): string
52
    {
53
        return $this->valueImplementor;
54
    }
55
56 58
    public function getName(): string
57
    {
58 58
        return $this->name;
59
    }
60
61 3
    public function getEntityType(): EntityTypeInterface
62
    {
63 3
        return $this->entityType;
64
    }
65
66 2
    public function getParent(): ?AttributeInterface
67
    {
68 2
        return $this->getEntityType()->getParentAttribute();
69
    }
70
71 58
    protected function __construct(string $name, EntityTypeInterface $entityType, string $valueImplementor)
72
    {
73 58
        $this->name = $name;
74 58
        $this->valueImplementor = $valueImplementor;
75 58
        $this->entityType = $entityType;
76 58
    }
77
}
78