Open Journal Systems  3.3.0
ResourceIteratorApplyBatched.php
1 <?php
2 
4 
10 
17 {
19  protected $callback;
20 
22  protected $iterator;
23 
25  protected $batches = 0;
26 
28  protected $iterated = 0;
29 
30  public static function getAllEvents()
31  {
32  return array(
33  // About to send a batch of requests to the callback
34  'iterator_batch.before_batch',
35  // Finished sending a batch of requests to the callback
36  'iterator_batch.after_batch',
37  // Created the batch object
38  'iterator_batch.created_batch'
39  );
40  }
41 
48  {
49  $this->iterator = $iterator;
50  $this->callback = $callback;
51  Version::warn(__CLASS__ . ' is deprecated');
52  }
53 
61  public function apply($perBatch = 50)
62  {
63  $this->iterated = $this->batches = $batches = 0;
64  $that = $this;
65  $it = $this->iterator;
67 
68  $batch = BatchBuilder::factory()
69  ->createBatchesWith(new BatchSizeDivisor($perBatch))
70  ->transferWith(new BatchClosureTransfer(function (array $batch) use ($that, $callback, &$batches, $it) {
71  $batches++;
72  $that->dispatch('iterator_batch.before_batch', array('iterator' => $it, 'batch' => $batch));
73  call_user_func_array($callback, array($it, $batch));
74  $that->dispatch('iterator_batch.after_batch', array('iterator' => $it, 'batch' => $batch));
75  }))
76  ->autoFlushAt($perBatch)
77  ->build();
78 
79  $this->dispatch('iterator_batch.created_batch', array('batch' => $batch));
80 
81  foreach ($this->iterator as $resource) {
82  $this->iterated++;
83  $batch->add($resource);
84  }
85 
86  $batch->flush();
87  $this->batches = $batches;
88 
89  return $this->iterated;
90  }
91 
97  public function getBatchCount()
98  {
99  return $this->batches;
100  }
101 
107  public function getIteratedCount()
108  {
110  }
111 }
Guzzle\Service\Resource\ResourceIteratorApplyBatched\$iterated
$iterated
Definition: ResourceIteratorApplyBatched.php:40
Guzzle\Service\Resource\ResourceIteratorApplyBatched\getBatchCount
getBatchCount()
Definition: ResourceIteratorApplyBatched.php:109
Guzzle\Batch\BatchClosureTransfer
Definition: BatchClosureTransfer.php:11
Guzzle\Common\Version\warn
static warn($message)
Definition: Version.php:23
Guzzle\Batch\BatchSizeDivisor
Definition: BatchSizeDivisor.php:8
Guzzle\Service\Resource\ResourceIteratorInterface
Definition: ResourceIteratorInterface.php:11
Guzzle\Common\Version
Definition: Version.php:8
Guzzle\Batch\BatchBuilder\factory
static factory()
Definition: BatchBuilder.php:60
Guzzle\Service\Resource\ResourceIteratorApplyBatched\getAllEvents
static getAllEvents()
Definition: ResourceIteratorApplyBatched.php:42
Guzzle\Batch\BatchBuilder
Definition: BatchBuilder.php:11
Guzzle\Service\Resource\ResourceIteratorApplyBatched\getIteratedCount
getIteratedCount()
Definition: ResourceIteratorApplyBatched.php:119
Guzzle\Service\Resource
Definition: AbstractResourceIteratorFactory.php:3
Guzzle\Service\Resource\ResourceIteratorApplyBatched
Definition: ResourceIteratorApplyBatched.php:16
Guzzle\Common\AbstractHasDispatcher\dispatch
dispatch($eventName, array $context=array())
Definition: AbstractHasDispatcher.php:41
Guzzle\Service\Resource\ResourceIteratorApplyBatched\$callback
$callback
Definition: ResourceIteratorApplyBatched.php:22
Guzzle\Common\AbstractHasDispatcher
Definition: AbstractHasDispatcher.php:12
Guzzle\Service\Resource\ResourceIteratorApplyBatched\apply
apply($perBatch=50)
Definition: ResourceIteratorApplyBatched.php:73
Guzzle\Service\Resource\ResourceIteratorApplyBatched\$batches
$batches
Definition: ResourceIteratorApplyBatched.php:34
Guzzle\Service\Resource\ResourceIteratorApplyBatched\$iterator
$iterator
Definition: ResourceIteratorApplyBatched.php:28
Guzzle\Service\Resource\ResourceIteratorApplyBatched\__construct
__construct(ResourceIteratorInterface $iterator, $callback)
Definition: ResourceIteratorApplyBatched.php:59