Passed
Pull Request — master (#8)
by BENOIT
01:53
created

MetricsHandlerFactory::default()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BenTools\MercurePHP\Metrics;
4
5
use React\Promise\PromiseInterface;
6
7
final class MetricsHandlerFactory implements MetricsHandlerFactoryInterface
8
{
9
    /**
10
     * @var MetricsHandlerFactoryInterface[]
11
     */
12
    private array $factories;
13
14
    public function __construct(array $factories)
15
    {
16
        $this->factories = (fn(MetricsHandlerFactoryInterface ...$factories) => $factories)(...$factories);
17
    }
18 2
19
    public function supports(string $dsn): bool
20 2
    {
21 2
        foreach ($this->factories as $factory) {
22
            if ($factory->supports($dsn)) {
23 1
                return true;
24
            }
25 1
        }
26 1
27 1
        return false;
28
    }
29
30
    public function create(string $dsn): PromiseInterface
31
    {
32
        foreach ($this->factories as $factory) {
33
            if (!$factory->supports($dsn)) {
34 1
                continue;
35
            }
36 1
37 1
            return $factory->create($dsn);
38
        }
39
40
        throw new \RuntimeException(\sprintf('Invalid metrics handler DSN %s', $dsn));
41 1
    }
42
}
43