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

TransportFactory::default()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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