BatchBuilder.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace Guzzle\Batch;
  3. use Guzzle\Common\Exception\InvalidArgumentException;
  4. use Guzzle\Common\Exception\RuntimeException;
  5. /**
  6. * Builder used to create custom batch objects
  7. */
  8. class BatchBuilder
  9. {
  10. /** @var bool Whether or not the batch should automatically flush*/
  11. protected $autoFlush = false;
  12. /** @var bool Whether or not to maintain a batch history */
  13. protected $history = false;
  14. /** @var bool Whether or not to buffer exceptions encountered in transfer */
  15. protected $exceptionBuffering = false;
  16. /** @var mixed Callable to invoke each time a flush completes */
  17. protected $afterFlush;
  18. /** @var BatchTransferInterface Object used to transfer items in the queue */
  19. protected $transferStrategy;
  20. /** @var BatchDivisorInterface Object used to divide the queue into batches */
  21. protected $divisorStrategy;
  22. /** @var array of Mapped transfer strategies by handle name */
  23. protected static $mapping = array(
  24. 'request' => 'Guzzle\Batch\BatchRequestTransfer',
  25. 'command' => 'Guzzle\Batch\BatchCommandTransfer'
  26. );
  27. /**
  28. * Create a new instance of the BatchBuilder
  29. *
  30. * @return BatchBuilder
  31. */
  32. public static function factory()
  33. {
  34. return new self();
  35. }
  36. /**
  37. * Automatically flush the batch when the size of the queue reaches a certain threshold. Adds {@see FlushingBatch}.
  38. *
  39. * @param $threshold Number of items to allow in the queue before a flush
  40. *
  41. * @return BatchBuilder
  42. */
  43. public function autoFlushAt($threshold)
  44. {
  45. $this->autoFlush = $threshold;
  46. return $this;
  47. }
  48. /**
  49. * Maintain a history of all items that have been transferred using the batch. Adds {@see HistoryBatch}.
  50. *
  51. * @return BatchBuilder
  52. */
  53. public function keepHistory()
  54. {
  55. $this->history = true;
  56. return $this;
  57. }
  58. /**
  59. * Buffer exceptions thrown during transfer so that you can transfer as much as possible, and after a transfer
  60. * completes, inspect each exception that was thrown. Enables the {@see ExceptionBufferingBatch} decorator.
  61. *
  62. * @return BatchBuilder
  63. */
  64. public function bufferExceptions()
  65. {
  66. $this->exceptionBuffering = true;
  67. return $this;
  68. }
  69. /**
  70. * Notify a callable each time a batch flush completes. Enables the {@see NotifyingBatch} decorator.
  71. *
  72. * @param mixed $callable Callable function to notify
  73. *
  74. * @return BatchBuilder
  75. * @throws InvalidArgumentException if the argument is not callable
  76. */
  77. public function notify($callable)
  78. {
  79. $this->afterFlush = $callable;
  80. return $this;
  81. }
  82. /**
  83. * Configures the batch to transfer batches of requests. Associates a {@see \Guzzle\Http\BatchRequestTransfer}
  84. * object as both the transfer and divisor strategy.
  85. *
  86. * @param int $batchSize Batch size for each batch of requests
  87. *
  88. * @return BatchBuilder
  89. */
  90. public function transferRequests($batchSize = 50)
  91. {
  92. $className = self::$mapping['request'];
  93. $this->transferStrategy = new $className($batchSize);
  94. $this->divisorStrategy = $this->transferStrategy;
  95. return $this;
  96. }
  97. /**
  98. * Configures the batch to transfer batches commands. Associates as
  99. * {@see \Guzzle\Service\Command\BatchCommandTransfer} as both the transfer and divisor strategy.
  100. *
  101. * @param int $batchSize Batch size for each batch of commands
  102. *
  103. * @return BatchBuilder
  104. */
  105. public function transferCommands($batchSize = 50)
  106. {
  107. $className = self::$mapping['command'];
  108. $this->transferStrategy = new $className($batchSize);
  109. $this->divisorStrategy = $this->transferStrategy;
  110. return $this;
  111. }
  112. /**
  113. * Specify the strategy used to divide the queue into an array of batches
  114. *
  115. * @param BatchDivisorInterface $divisorStrategy Strategy used to divide a batch queue into batches
  116. *
  117. * @return BatchBuilder
  118. */
  119. public function createBatchesWith(BatchDivisorInterface $divisorStrategy)
  120. {
  121. $this->divisorStrategy = $divisorStrategy;
  122. return $this;
  123. }
  124. /**
  125. * Specify the strategy used to transport the items when flush is called
  126. *
  127. * @param BatchTransferInterface $transferStrategy How items are transferred
  128. *
  129. * @return BatchBuilder
  130. */
  131. public function transferWith(BatchTransferInterface $transferStrategy)
  132. {
  133. $this->transferStrategy = $transferStrategy;
  134. return $this;
  135. }
  136. /**
  137. * Create and return the instantiated batch
  138. *
  139. * @return BatchInterface
  140. * @throws RuntimeException if no transfer strategy has been specified
  141. */
  142. public function build()
  143. {
  144. if (!$this->transferStrategy) {
  145. throw new RuntimeException('No transfer strategy has been specified');
  146. }
  147. if (!$this->divisorStrategy) {
  148. throw new RuntimeException('No divisor strategy has been specified');
  149. }
  150. $batch = new Batch($this->transferStrategy, $this->divisorStrategy);
  151. if ($this->exceptionBuffering) {
  152. $batch = new ExceptionBufferingBatch($batch);
  153. }
  154. if ($this->afterFlush) {
  155. $batch = new NotifyingBatch($batch, $this->afterFlush);
  156. }
  157. if ($this->autoFlush) {
  158. $batch = new FlushingBatch($batch, $this->autoFlush);
  159. }
  160. if ($this->history) {
  161. $batch = new HistoryBatch($batch);
  162. }
  163. return $batch;
  164. }
  165. }