Passed
Pull Request — 1.x (#163)
by
unknown
02:11
created

MarshallerProvider::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\QueryRepository;
6
7
use BEAR\RepositoryModule\Annotation\MarshallerOptions;
8
use InvalidArgumentException;
9
use Override;
10
use Ray\Di\ProviderInterface;
11
use Symfony\Component\Cache\Marshaller\DefaultMarshaller;
12
use Symfony\Component\Cache\Marshaller\DeflateMarshaller;
13
use Symfony\Component\Cache\Marshaller\MarshallerInterface;
14
15
use function is_string;
16
use function sprintf;
17
18
/**
19
 * Provider for creating marshaller instances based on configuration options
20
 *
21
 * Supports the following marshaller types:
22
 * - 'default': Basic marshaller with optional igbinary support
23
 * - 'deflate': Compression-enabled marshaller (requires zlib extension)
24
 *
25
 * @implements ProviderInterface<MarshallerInterface|null>
26
 */
27
final class MarshallerProvider implements ProviderInterface
28
{
29
    /** @param array<string, mixed> $options Marshalling options */
30
    public function __construct(
31
        #[MarshallerOptions]
32
        private readonly array $options = [],
33
    ) {
34
    }
35
36
    #[Override]
37
    public function get(): MarshallerInterface|null
38
    {
39
        return $this->createMarshaller($this->options);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->createMarshaller($this->options) targeting BEAR\QueryRepository\Mar...der::createMarshaller() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
40
    }
41
42
    /**
43
     * Create marshaller instance based on options
44
     *
45
     * @param array<string, mixed> $options
46
     */
47
    private function createMarshaller(array $options): MarshallerInterface|null
48
    {
49
        if (empty($options) || ($options['enabled'] ?? false) !== true) {
50
            return null;
51
        }
52
53
        /** @var string $type */
54
        $type = is_string($options['type'] ?? null) ? $options['type'] : 'default';
55
        $useIgbinary = (bool) ($options['use_igbinary'] ?? false);
56
57
        return match ($type) {
58
            'default' => $this->createDefaultMarshaller($useIgbinary),
59
            'deflate' => $this->createDeflateMarshaller($useIgbinary),
60
            default => throw new InvalidArgumentException(sprintf('Invalid marshaller type: %s', $type)),
61
        };
62
    }
63
64
    private function createDefaultMarshaller(bool $useIgbinary): DefaultMarshaller
65
    {
66
        return new DefaultMarshaller($useIgbinary);
67
    }
68
69
    private function createDeflateMarshaller(bool $useIgbinary): DeflateMarshaller
70
    {
71
        $defaultMarshaller = $this->createDefaultMarshaller($useIgbinary);
72
73
        return new DeflateMarshaller($defaultMarshaller);
74
    }
75
}
76