Passed
Push — master ( 6f5173...a1a76b )
by BENOIT
01:53
created

StorageFactory::supports()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BenTools\MercurePHP\Storage;
4
5
use React\Promise\PromiseInterface;
6
7
final class StorageFactory implements StorageFactoryInterface
8
{
9
    /**
10
     * @var StorageFactoryInterface[]
11
     */
12
    private iterable $factories;
13
14
    public function __construct(iterable $factories)
15
    {
16
        $this->factories = $factories;
0 ignored issues
show
Documentation Bug introduced by
It seems like $factories of type iterable is incompatible with the declared type BenTools\MercurePHP\Stor...orageFactoryInterface[] of property $factories.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
17
    }
18
19 2
    public function supports(string $dsn): bool
20
    {
21 2
        foreach ($this->getFactories() as $factory) {
22 2
            if ($factory->supports($dsn)) {
23
                return true;
24 1
            }
25
        }
26 1
27 1
        return false;
28 1
    }
29
30
    public function create(string $dsn): PromiseInterface
31
    {
32
        foreach ($this->getFactories() as $factory) {
33
            if (!$factory->supports($dsn)) {
34
                continue;
35 1
            }
36
37 1
            return $factory->create($dsn);
38 1
        }
39 1
40
        throw new \RuntimeException(\sprintf('Invalid storage DSN %s', $dsn));
41
    }
42 1
43
    private function getFactories(): array
44
    {
45
        if (\is_array($this->factories)) {
46
            return $this->factories;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->factories returns the type iterable which is incompatible with the type-hinted return array.
Loading history...
47
        }
48 2
49
        $factories = [];
50 2
        foreach ($this->factories as $factory) {
51
            if ($factory === $this) {
52 2
                continue;
53 2
            }
54 2
            $factories[] = $factory;
55
        }
56
        return $this->factories = (fn(StorageFactoryInterface ...$factories) => $factories)(...$factories);
57
    }
58
}
59