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

ConstrainedMapping::verify()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 6
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\Hydration\Mapping\Composite;
4
5
use Stratadox\HydrationMapping\Mapping;
6
use Stratadox\HydrationMapping\MappingFailure;
7
use Stratadox\Specification\Contract\Satisfiable;
8
9
final class ConstrainedMapping implements Mapping
10
{
11
    /** @var Satisfiable */
12
    private $condition;
13
    /** @var Mapping */
14
    private $mapping;
15
16
    public function __construct(Satisfiable $condition, Mapping $mapping)
17
    {
18
        $this->condition = $condition;
19
        $this->mapping = $mapping;
20
    }
21
22
    public function name(): string
23
    {
24
        return $this->mapping->name();
25
    }
26
27
    public function value(array $data, $owner = null)
28
    {
29
        return $this->verify($this->mapping->value($data, $owner));
30
    }
31
32
    /**
33
     * @param mixed $result
34
     * @return mixed
35
     * @throws MappingFailure
36
     */
37
    private function verify($result)
38
    {
39
        if (!$this->condition->isSatisfiedBy($result)) {
40
            throw ConstraintNotSatisfied::with($result, $this->mapping, $this->condition);
41
        }
42
        return $result;
43
    }
44
}
45