ChainResolver   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 30
c 0
b 0
f 0
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A resolve() 0 11 3
A add() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Damax\ChargeableApi\Product;
6
7
final class ChainResolver implements Resolver
8
{
9
    /**
10
     * @var Resolver[]
11
     */
12
    private $resolvers;
13
14
    public function __construct(iterable $resolvers = [])
15
    {
16
        foreach ($resolvers as $resolver) {
17
            $this->add($resolver);
18
        }
19
    }
20
21
    public function add(Resolver $resolver): void
22
    {
23
        $this->resolvers[] = $resolver;
24
    }
25
26
    public function resolve($request): Product
27
    {
28
        foreach ($this->resolvers as $resolver) {
29
            try {
30
                return $resolver->resolve($request);
31
            } catch (ProductResolutionFailed $e) {
32
                // Try next resolver.
33
            }
34
        }
35
36
        throw ProductResolutionFailed::unresolved();
37
    }
38
}
39