ChainResolver::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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