Passed
Pull Request — 1.x (#163)
by Akihito
01:58
created

MarshallerProvider   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 13
c 2
b 0
f 0
dl 0
loc 57
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createDefaultMarshaller() 0 3 1
A get() 0 4 1
A createDeflateMarshaller() 0 5 1
A __construct() 0 4 1
A createMarshaller() 0 14 4
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
    /**
30
     * @param array{
31
     *     enabled?: bool,
32
     *     type?: 'default'|'deflate',
33
     *     use_igbinary?: bool
34
     * } $options Marshalling options
35
     */
36
    public function __construct(
37
        #[MarshallerOptions]
38
        private readonly array $options = [],
39
    ) {
40
    }
41
42
    #[Override]
43
    public function get(): MarshallerInterface|null
44
    {
45
        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...
46
    }
47
48
    /**
49
     * Create marshaller instance based on options
50
     *
51
     * @param array{
52
     *     enabled?: bool,
53
     *     type?: 'default'|'deflate',
54
     *     use_igbinary?: bool
55
     * } $options
56
     */
57
    private function createMarshaller(array $options): MarshallerInterface|null
58
    {
59
        if (empty($options) || ($options['enabled'] ?? false) !== true) {
60
            return null;
61
        }
62
63
        /** @var 'default'|'deflate' $type */
64
        $type = is_string($options['type'] ?? null) ? $options['type'] : 'default';
65
        $useIgbinary = (bool) ($options['use_igbinary'] ?? false);
66
67
        return match ($type) {
68
            'default' => $this->createDefaultMarshaller($useIgbinary),
69
            'deflate' => $this->createDeflateMarshaller($useIgbinary),
70
            default => throw new InvalidArgumentException(sprintf('Invalid marshaller type: %s', $type)),
71
        };
72
    }
73
74
    private function createDefaultMarshaller(bool $useIgbinary): DefaultMarshaller
75
    {
76
        return new DefaultMarshaller($useIgbinary);
77
    }
78
79
    private function createDeflateMarshaller(bool $useIgbinary): DeflateMarshaller
80
    {
81
        $defaultMarshaller = $this->createDefaultMarshaller($useIgbinary);
82
83
        return new DeflateMarshaller($defaultMarshaller);
84
    }
85
}
86