MarshallerProvider::get()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 2
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 Override;
9
use Ray\Di\ProviderInterface;
10
use Symfony\Component\Cache\Marshaller\DefaultMarshaller;
11
use Symfony\Component\Cache\Marshaller\DeflateMarshaller;
12
use Symfony\Component\Cache\Marshaller\MarshallerInterface;
13
14
/**
15
 * Provider for creating marshaller instances based on configuration options
16
 *
17
 * Supports the following marshaller types:
18
 * - 'default': Basic marshaller with optional igbinary support
19
 * - 'deflate': Compression-enabled marshaller (requires zlib extension)
20
 *
21
 * @implements ProviderInterface<MarshallerInterface|null>
22
 */
23
final class MarshallerProvider implements ProviderInterface
24
{
25
    /** @param array{enabled?: bool, type?: string, use_igbinary?: bool} $options Marshalling options */
26
    public function __construct(
27
        #[MarshallerOptions]
28
        private readonly array $options = [],
29
    ) {
30
    }
31
32
    #[Override]
33
    public function get(): MarshallerInterface|null
34
    {
35
        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...
36
    }
37
38
    /**
39
     * Create marshaller instance based on options
40
     *
41
     * @param array{enabled?: bool, type?: string, use_igbinary?: bool} $options
42
     */
43
    private function createMarshaller(array $options): MarshallerInterface|null
44
    {
45
        if (empty($options) || ($options['enabled'] ?? false) !== true) {
46
            return null;
47
        }
48
49
        $typeString = $options['type'] ?? 'default';
50
        $type = MarshallerType::tryFrom($typeString) ?? MarshallerType::DEFAULT;
51
        $useIgbinary = $options['use_igbinary'] ?? false;
52
53
        return match ($type) {
54
            MarshallerType::DEFAULT => $this->createDefaultMarshaller($useIgbinary),
55
            MarshallerType::DEFLATE => $this->createDeflateMarshaller($useIgbinary),
56
        };
57
    }
58
59
    private function createDefaultMarshaller(bool $useIgbinary): DefaultMarshaller
60
    {
61
        return new DefaultMarshaller($useIgbinary);
62
    }
63
64
    private function createDeflateMarshaller(bool $useIgbinary): DeflateMarshaller
65
    {
66
        $defaultMarshaller = $this->createDefaultMarshaller($useIgbinary);
67
68
        return new DeflateMarshaller($defaultMarshaller);
69
    }
70
}
71