Passed
Push — master ( 109e7c...79a79c )
by Thomas Mauro
02:15
created

__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TMV\Laminas\Messenger\Factory\Middleware;
6
7
use Doctrine\Persistence\ManagerRegistry;
8
use Psr\Container\ContainerInterface;
9
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
10
use TMV\Laminas\Messenger\Exception\InvalidArgumentException;
11
use TMV\Laminas\Messenger\Middleware\DoctrinePingConnectionMiddleware;
12
13
final class DoctrinePingConnectionMiddlewareFactory extends AbstractDoctrineMiddlewareFactory
14 1
{
15
    private string $query;
16 1
17 1
    public function __construct(string $connectionName = 'orm_default', string $query = 'SELECT 1;')
18 1
    {
19
        parent::__construct($connectionName);
20
        $this->query = $query;
21
    }
22
23
    public function __invoke(ContainerInterface $container): MiddlewareInterface
24
    {
25
        /** @var ManagerRegistry $manager */
26
        $manager = $container->get(ManagerRegistry::class);
27
28
        return new DoctrinePingConnectionMiddleware(
29
            $manager,
30
            $this->connectionName ?? $manager->getDefaultConnectionName(),
31
            $this->query
32
        );
33
    }
34
35
    /**
36
     * @psalm-api
37
     *
38
     * @param array<int, mixed> $arguments
39
     */
40
    public static function __callStatic(string $name, array $arguments): MiddlewareInterface
41
    {
42
        if (! array_key_exists(0, $arguments) || ! $arguments[0] instanceof ContainerInterface) {
43
            throw new InvalidArgumentException(sprintf(
44
                'The first argument must be of type %s',
45
                ContainerInterface::class
46
            ));
47
        }
48
49
        return (new self($name))($arguments[0]);
50
    }
51
52
    /**
53
     * @param array{connectionName: string, query: string} $data
54
     */
55
    public static function __set_state(array $data): self
56
    {
57
        return new self(
58
            $data['connectionName'] ?? 'orm_default',
59
            $data['query'] ?? 'SELECT 1;',
60
        );
61
    }
62
}
63