Passed
Push — master ( 924412...d4b74b )
by Jesse
02:02
created

ConstrainedMapping   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A checkThatIt() 0 3 1
A verify() 0 6 2
A name() 0 3 1
A value() 0 3 1
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 static function checkThatIt(Satisfiable $condition, Mapping $mapping): self
23
    {
24
        return new self($condition, $mapping);
25
    }
26
27
    public function name(): string
28
    {
29
        return $this->mapping->name();
30
    }
31
32
    public function value(array $data, $owner = null)
33
    {
34
        return $this->verify($this->mapping->value($data, $owner));
35
    }
36
37
    /**
38
     * @param mixed $result
39
     * @return mixed
40
     * @throws MappingFailure
41
     */
42
    private function verify($result)
43
    {
44
        if (!$this->condition->isSatisfiedBy($result)) {
45
            throw ConstraintNotSatisfied::with($result, $this->mapping, $this->condition);
46
        }
47
        return $result;
48
    }
49
}
50