Passed
Push — master ( 01c6a8...5a4258 )
by Jesse
09:00
created

ObservedRelationMapping   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 33
rs 10
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A hydrating() 0 3 1
A name() 0 3 1
A inProperty() 0 3 1
A value() 0 6 2
A __construct() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\Hydration\Mapping\Relation;
4
5
use Stratadox\HydrationMapping\Mapping;
6
use Stratadox\Hydrator\HydrationObserver;
7
8
final class ObservedRelationMapping implements Mapping, HydrationObserver
9
{
10
    /** @var string */
11
    private $name;
12
    /** @var null|object */
13
    private $referenceTo;
14
15
    private function __construct(string $name)
16
    {
17
        $this->name = $name;
18
    }
19
20
    public static function inProperty(string $name): self
21
    {
22
        return new self($name);
23
    }
24
25
    public function name(): string
26
    {
27
        return $this->name;
28
    }
29
30
    public function hydrating(object $theInstance, array $theData): void
31
    {
32
        $this->referenceTo = $theInstance;
33
    }
34
35
    public function value(array $data, $owner = null)
36
    {
37
        if (null === $this->referenceTo) {
38
            throw NoRelationObservationMade::forIn($this->name);
39
        }
40
        return $this->referenceTo;
41
    }
42
}