AddBusNameStampMiddlewareFactory::__callStatic()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 10
rs 10
ccs 1
cts 1
cp 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TMV\Laminas\Messenger\Factory\Middleware;
6
7
use Psr\Container\ContainerInterface;
8
use Symfony\Component\Messenger\Middleware\AddBusNameStampMiddleware;
9
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
10
use TMV\Laminas\Messenger\Exception\InvalidArgumentException;
11
12
use function array_key_exists;
13
use function sprintf;
14
15
/**
16
 * @psalm-api
17
 */
18 1
final class AddBusNameStampMiddlewareFactory
19
{
20 1
    private string $busName;
21 1
22
    public function __construct(string $busName = 'messenger.bus.default')
23 1
    {
24
        $this->busName = $busName;
25 1
    }
26
27
    public function __invoke(ContainerInterface $container): AddBusNameStampMiddleware
28
    {
29
        return new AddBusNameStampMiddleware($this->busName);
30
    }
31
32
    public static function __set_state(array $data): self
33
    {
34 2
        return new self($data['busName'] ?? null);
0 ignored issues
show
Bug introduced by
It seems like $data['busName'] ?? null can also be of type null; however, parameter $busName of TMV\Laminas\Messenger\Fa...eFactory::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

34
        return new self(/** @scrutinizer ignore-type */ $data['busName'] ?? null);
Loading history...
35
    }
36 2
37 1
    /**
38 1
     * @psalm-api
39 1
     *
40
     * @param array<int, mixed> $arguments
41
     */
42
    public static function __callStatic(string $name, array $arguments): MiddlewareInterface
43 1
    {
44
        if (! array_key_exists(0, $arguments) || ! $arguments[0] instanceof ContainerInterface) {
45
            throw new InvalidArgumentException(sprintf(
46
                'The first argument must be of type %s',
47
                ContainerInterface::class
48
            ));
49
        }
50
51
        return (new self($name))($arguments[0]);
52
    }
53
}
54