1: <?php
2:
3: namespace Guzzle\Batch;
4:
5: /**
6: * BatchInterface decorator used to add automatic flushing of the queue when the size of the queue reaches a threshold.
7: */
8: class FlushingBatch extends AbstractBatchDecorator
9: {
10: /**
11: * @var int The threshold for which to automatically flush
12: */
13: protected $threshold;
14:
15: /**
16: * @var int Current number of items known to be in the queue
17: */
18: protected $currentTotal = 0;
19:
20: /**
21: * @param BatchInterface $decoratedBatch BatchInterface that is being decorated
22: * @param int $threshold Flush when the number in queue matches the threshold
23: */
24: public function __construct(BatchInterface $decoratedBatch, $threshold)
25: {
26: $this->threshold = $threshold;
27: parent::__construct($decoratedBatch);
28: }
29:
30: /**
31: * Set the auto-flush threshold
32: *
33: * @param int $threshold The auto-flush threshold
34: *
35: * @return FlushingBatch
36: */
37: public function setThreshold($threshold)
38: {
39: $this->threshold = $threshold;
40:
41: return $this;
42: }
43:
44: /**
45: * Get the auto-flush threshold
46: *
47: * @return int
48: */
49: public function getThreshold()
50: {
51: return $this->threshold;
52: }
53:
54: /**
55: * {@inheritdoc}
56: */
57: public function add($item)
58: {
59: $this->decoratedBatch->add($item);
60: if (++$this->currentTotal >= $this->threshold) {
61: $this->currentTotal = 0;
62: $this->decoratedBatch->flush();
63: }
64:
65: return $this;
66: }
67: }
68: