BatchClosureTransfer.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. namespace Guzzle\Batch;
  3. use Guzzle\Common\Exception\InvalidArgumentException;
  4. /**
  5. * Batch transfer strategy where transfer logic can be defined via a Closure.
  6. * This class is to be used with {@see Guzzle\Batch\BatchInterface}
  7. */
  8. class BatchClosureTransfer implements BatchTransferInterface
  9. {
  10. /** @var callable A closure that performs the transfer */
  11. protected $callable;
  12. /** @var mixed $context Context passed to the callable */
  13. protected $context;
  14. /**
  15. * @param mixed $callable Callable that performs the transfer. This function should accept two arguments:
  16. * (array $batch, mixed $context).
  17. * @param mixed $context Optional context to pass to the batch divisor
  18. *
  19. * @throws InvalidArgumentException
  20. */
  21. public function __construct($callable, $context = null)
  22. {
  23. if (!is_callable($callable)) {
  24. throw new InvalidArgumentException('Argument must be callable');
  25. }
  26. $this->callable = $callable;
  27. $this->context = $context;
  28. }
  29. public function transfer(array $batch)
  30. {
  31. return empty($batch) ? null : call_user_func($this->callable, $batch, $this->context);
  32. }
  33. }