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

ProxyMapping   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 34
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A value() 0 9 2
A __construct() 0 4 1
A name() 0 3 1
A inProperty() 0 5 1
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\Hydration\Mapping\Relation;
4
5
use Stratadox\HydrationMapping\Mapping;
6
use Stratadox\Proxy\ProxyFactory;
7
use Throwable;
8
9
final class ProxyMapping implements Mapping
10
{
11
    /** @var string */
12
    private $name;
13
    /** @var ProxyFactory */
14
    private $proxyFactory;
15
16
    private function __construct(string $name, ProxyFactory $proxyFactory)
17
    {
18
        $this->name = $name;
19
        $this->proxyFactory = $proxyFactory;
20
    }
21
22
    public static function inProperty(
23
        string $name,
24
        ProxyFactory $proxyFactory
25
    ): Mapping {
26
        return new self($name, $proxyFactory);
27
    }
28
29
    public function name(): string
30
    {
31
        return $this->name;
32
    }
33
34
    public function value(array $data, $owner = null)
35
    {
36
        try {
37
            return $this->proxyFactory->create([
38
                'owner' => $owner,
39
                'property' => $this->name
40
            ]);
41
        } catch (Throwable $exception) {
42
            throw ProxyMappingFailure::encountered($this, $exception);
43
        }
44
    }
45
}
46