Passed
Pull Request — master (#2)
by Vincent
04:15 queued 01:21
created

FailerFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 42
ccs 9
cts 9
cp 1
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createFromDsn() 0 11 2
A register() 0 3 1
1
<?php
2
3
namespace Bdf\QueueBundle\FailerFactory;
4
5
use Bdf\Dsn\Dsn;
6
use Bdf\Queue\Failer\FailedJobRepositoryInterface;
7
use InvalidArgumentException;
8
9
/**
10
 * Factory for failer storage
11
 */
12
final class FailerFactory
13
{
14
    /**
15
     * Factories indexed by the scheme
16
     *
17
     * @var array<string, FailerDsnFactoryInterface>
18
     * @see FailerDsnFactoryInterface::scheme() For the key
19
     */
20
    private $factories = [];
21
22
    /**
23
     * Register a new factory
24
     *
25
     * @param FailerDsnFactoryInterface $factory
26
     *
27
     * @return void
28
     */
29 5
    public function register(FailerDsnFactoryInterface $factory): void
30
    {
31 5
        $this->factories[$factory->scheme()] = $factory;
32 5
    }
33
34
    /**
35
     * Try to create the repository using a DSN string
36
     *
37
     * @param string $dsn The repository DSN
38
     *
39
     * @return FailedJobRepositoryInterface
40
     *
41
     * @throws InvalidArgumentException When the repository cannot be created
42
     */
43 5
    public function createFromDsn(string $dsn): FailedJobRepositoryInterface
44
    {
45 5
        $parsedDsn = Dsn::parse($dsn);
46
47 5
        $factory = $this->factories[$parsedDsn->getScheme()] ?? null;
48
49 5
        if (!$factory) {
50 1
            throw new InvalidArgumentException('Unsupported failer scheme "' . $parsedDsn->getScheme() . '"');
51
        }
52
53 4
        return $factory->create($parsedDsn);
54
    }
55
}
56