ProductResolver   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 23
c 0
b 0
f 0
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 9 3
A addProduct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Damax\ChargeableApi\Bridge\Symfony\HttpFoundation;
6
7
use Damax\ChargeableApi\Product\Product;
8
use Damax\ChargeableApi\Product\ProductResolutionFailed;
9
use Damax\ChargeableApi\Product\Resolver;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\RequestMatcherInterface;
12
13
final class ProductResolver implements Resolver
14
{
15
    private $products = [];
16
17
    public function addProduct(Product $product, RequestMatcherInterface $matcher): void
18
    {
19
        $this->products[$product->name()] = [$product, $matcher];
20
    }
21
22
    /**
23
     * @param Request $request
24
     *
25
     * @throws ProductResolutionFailed
26
     */
27
    public function resolve($request): Product
28
    {
29
        foreach ($this->products as list($product, $matcher)) {
30
            if ($matcher->matches($request)) {
31
                return $product;
32
            }
33
        }
34
35
        throw ProductResolutionFailed::unresolved();
36
    }
37
}
38