-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathBulk.php
More file actions
77 lines (64 loc) · 1.63 KB
/
Bulk.php
File metadata and controls
77 lines (64 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
namespace G4\DataMapper\Common;
use G4\DataMapper\Common\MappingInterface;
use G4\DataMapper\Exception\BulkOperationException;
class Bulk implements \Countable
{
/**
* @var AdapterInterface
*/
private $adapter;
/**
* @var \ArrayIterator
*/
private $data;
/**
* @var CollectionNameInterface
*/
private $collectionName;
public function __construct(AdapterInterface $adapter, CollectionNameInterface $collectionName)
{
$this->adapter = $adapter;
$this->collectionName = $collectionName;
$this->data = new \ArrayIterator([]);
}
/**
* @param MappingInterface $mapping
* @return Bulk
*/
public function add(MappingInterface $mapping)
{
$this->data->append($mapping);
return $this;
}
public function insert()
{
try {
$this->adapter->insertBulk($this->collectionName, $this->getData());
} catch (\Exception $exception) {
throw new BulkOperationException($exception->getCode() . ': ' . $exception->getMessage());
}
}
public function upsert()
{
try {
$this->adapter->upsertBulk($this->collectionName, $this->getData());
} catch (\Exception $exception) {
throw new BulkOperationException($exception->getCode() . ': ' . $exception->getMessage());
}
}
/**
* @return \ArrayIterator
*/
public function getData()
{
return $this->data;
}
/**
* @return int
*/
public function count(): int
{
return count($this->data);
}
}