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