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

FailerFactory::createFromDsn()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 2
rs 10
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