Passed
Pull Request — 1.x (#163)
by
unknown
02:01
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 RuntimeException;
12
use Symfony\Component\Cache\Marshaller\DefaultMarshaller;
13
use Symfony\Component\Cache\Marshaller\DeflateMarshaller;
14
use Symfony\Component\Cache\Marshaller\MarshallerInterface;
15
16
use function extension_loaded;
17
use function is_string;
18
use function sprintf;
19
20
/**
21
 * Provider for creating marshaller instances based on configuration options
22
 *
23
 * Supports the following marshaller types:
24
 * - 'default': Basic marshaller with optional igbinary support
25
 * - 'deflate': Compression-enabled marshaller (requires zlib extension)
26
 *
27
 * @implements ProviderInterface<MarshallerInterface|null>
28
 */
29
final class MarshallerProvider implements ProviderInterface
30
{
31
    /** @param array<string, mixed> $options Marshalling options */
32
    public function __construct(
33
        #[MarshallerOptions]
34
        private readonly array $options = [],
35
    ) {
36
    }
37
38
    #[Override]
39
    public function get(): MarshallerInterface|null
40
    {
41
        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...
42
    }
43
44
    /**
45
     * Create marshaller instance based on options
46
     *
47
     * @param array<string, mixed> $options
48
     */
49
    private function createMarshaller(array $options): MarshallerInterface|null
50
    {
51
        if (empty($options) || ($options['enabled'] ?? false) !== true) {
52
            return null;
53
        }
54
55
        /** @var string $type */
56
        $type = is_string($options['type'] ?? null) ? $options['type'] : 'default';
57
        $useIgbinary = (bool) ($options['use_igbinary'] ?? false);
58
59
        return match ($type) {
60
            'default' => $this->createDefaultMarshaller($useIgbinary),
61
            'deflate' => $this->createDeflateMarshaller($useIgbinary),
62
            default => throw new InvalidArgumentException(sprintf('Invalid marshaller type: %s', $type)),
63
        };
64
    }
65
66
    private function createDefaultMarshaller(bool $useIgbinary): DefaultMarshaller
67
    {
68
        if ($useIgbinary && ! extension_loaded('igbinary')) {
69
            throw new RuntimeException('igbinary extension is required for igbinary marshaller');
70
        }
71
72
        return new DefaultMarshaller($useIgbinary);
73
    }
74
75
    private function createDeflateMarshaller(bool $useIgbinary): DeflateMarshaller
76
    {
77
        if (! extension_loaded('zlib')) {
78
            throw new RuntimeException('zlib extension is required for deflate marshaller');
79
        }
80
81
        $defaultMarshaller = $this->createDefaultMarshaller($useIgbinary);
82
83
        return new DeflateMarshaller($defaultMarshaller);
84
    }
85
}
86