Jake Vanderwerf
2026-02-10 c348d35c7ecb6c74f71cf90b982412f267c5d807
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
namespace JVBase\managers\queue;
if (!defined('ABSPATH')) {
    exit;
}
use JVBase\managers\queue\mergers\DefaultMerger;
 
final class TypeRegistry
{
    private array $configs = [];
 
    public function register(string $type, TypeConfig $config): void
    {
        $this->validateRegistration($type, $config);
        $this->configs[$type] = $config;
    }
 
    public function has(string $type): bool
    {
        return isset($this->configs[$type]);
    }
 
    public function getExecutor(string $type): ?Executor
    {
        return array_key_exists($type, $this->configs) ? $this->configs[$type]->executor : JVB()->queue()->executor;
    }
 
    public function getMergeable(string $type): ?Mergeable
    {
        $config = $this->configs[$type] ?? null;
        if (!$config) {
            return null;
        }
 
        // Explicit mergeable always wins
        if ($config->mergeable) {
            return $config->mergeable;
        }
 
        // Default merge based on chunkKey
        if ($config->chunkKey) {
            return new DefaultMerger($config->chunkKey);
        }
 
        return null;
    }
 
    public function getConfig(string $type): ?TypeConfig
    {
        return $this->configs[$type] ?? null;
    }
 
    public function getChunkConfig(string $type): ?array
    {
        $config = $this->configs[$type] ?? null;
        if (!$config || empty($config->chunkKey)) {
            return null;
        }
        return [
            'key'  => $config->chunkKey,
            'size' => $config->chunkSize,
        ];
    }
 
    public function getMaxRetries(string $type): int
    {
        return $this->configs[$type]?->maxRetries ?? 3;
    }
 
    public function validateRegistration(string $type, TypeConfig $config): void
    {
        if ($config->executor && $config->chunkKey) {
            // Verify executor can handle chunked operations
            if (!method_exists($config->executor, 'execute')) {
                throw new \InvalidArgumentException(
                    "Executor for '{$type}' must implement execute() method"
                );
            }
        }
    }
}