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

ProxyMapping::value()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 9
rs 10
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