<?php
|
namespace JVBase\managers\queue\mergers;
|
|
use JVBase\managers\queue\Mergeable;
|
use JVBase\managers\queue\Operation;
|
|
/**
|
* Default merger, uses the chunkKey as the mergeable array
|
*/
|
final class DefaultMerger implements Mergeable
|
{
|
public function __construct(
|
private string|array $chunkKey
|
) {}
|
|
public function canMerge(Operation $existing, Operation $incoming): bool
|
{
|
foreach ((array) $this->chunkKey as $key) {
|
if (!isset($incoming->requestData[$key])) {
|
return false;
|
}
|
}
|
return true;
|
}
|
|
public function merge(Operation $existing, Operation $incoming): Operation
|
{
|
foreach ((array) $this->chunkKey as $key) {
|
$existing->requestData[$key] = array_replace(
|
$existing->requestData[$key] ?? [],
|
$incoming->requestData[$key] ?? []
|
);
|
}
|
|
$existing->totalItems += $incoming->totalItems;
|
return $existing;
|
}
|
}
|