-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
151 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace Ddeboer\DataImport\Writer; | ||
|
||
use Ddeboer\DataImport\Writer; | ||
|
||
/** | ||
* @author Markus Bachmann <markus.bachmann@bachi.biz> | ||
*/ | ||
class BatchWriter implements Writer | ||
{ | ||
private $delegate; | ||
|
||
private $size; | ||
|
||
private $queue; | ||
|
||
public function __construct(Writer $delegate, $size = 20) | ||
{ | ||
$this->delegate = $delegate; | ||
$this->size = $size; | ||
} | ||
|
||
public function prepare() | ||
{ | ||
$this->delegate->prepare(); | ||
|
||
$this->queue = new \SplQueue(); | ||
} | ||
|
||
public function writeItem(array $item) | ||
{ | ||
$this->queue->push($item); | ||
|
||
if (count($this->queue) >= $this->size) { | ||
$this->flush(); | ||
} | ||
} | ||
|
||
public function finish() | ||
{ | ||
$this->flush(); | ||
|
||
$this->delegate->finish(); | ||
} | ||
|
||
private function flush() | ||
{ | ||
foreach ($this->queue as $item) { | ||
$this->delegate->writeItem($item); | ||
} | ||
|
||
if ($this->delegate instanceof FlushableWriter) { | ||
$this->delegate->flush(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Ddeboer\DataImport\Writer; | ||
|
||
interface FlushableWriter | ||
{ | ||
public function flush(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Ddeboer\DataImport\Tests\Writer; | ||
|
||
use Ddeboer\DataImport\Writer\BatchWriter; | ||
|
||
class BatchWriterTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testWriteItem() | ||
{ | ||
$delegate = $this->getMock('Ddeboer\DataImport\Writer'); | ||
$writer = new BatchWriter($delegate); | ||
|
||
$delegate->expects($this->once()) | ||
->method('prepare'); | ||
|
||
$delegate->expects($this->never()) | ||
->method('writeItem'); | ||
|
||
$writer->prepare(); | ||
$writer->writeItem(['Test']); | ||
} | ||
|
||
public function testFlush() | ||
{ | ||
$delegate = $this->getMock('Ddeboer\DataImport\Writer'); | ||
$writer = new BatchWriter($delegate); | ||
|
||
$delegate->expects($this->exactly(20)) | ||
->method('writeItem'); | ||
|
||
$writer->prepare(); | ||
|
||
for ($i = 0; $i < 20; $i++) { | ||
$writer->writeItem(['Test']); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters