|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace FOS\ElasticaBundle\Persister; |
|
4
|
|
|
|
|
5
|
|
|
use FOS\ElasticaBundle\Provider\Indexable; |
|
6
|
|
|
use Pagerfanta\Pagerfanta; |
|
7
|
|
|
use Elastica\Exception\Bulk\ResponseException as BulkResponseException; |
|
8
|
|
|
|
|
9
|
|
|
class PagerPersister |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var ObjectPersisterInterface |
|
13
|
|
|
*/ |
|
14
|
|
|
private $objectPersister; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var Indexable |
|
18
|
|
|
*/ |
|
19
|
|
|
private $indexable; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* PagerPersister constructor. |
|
23
|
|
|
* @param ObjectPersisterInterface $objectPersister |
|
24
|
|
|
* @param Indexable $indexable |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct(ObjectPersisterInterface $objectPersister, Indexable $indexable) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->objectPersister = $objectPersister; |
|
29
|
|
|
$this->indexable = $indexable; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function insert(Pagerfanta $pager, \Closure $loggerClosure = null, array $options = array()) |
|
33
|
|
|
{ |
|
34
|
|
|
$nbObjects = $pager->getNbResults(); |
|
35
|
|
|
$pager->setCurrentPage(1); |
|
36
|
|
|
while (true) { |
|
37
|
|
|
if ($pager->getNextPage()) { |
|
38
|
|
|
return; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$sliceSize = $options['batch_size']; |
|
42
|
|
|
try { |
|
43
|
|
|
$objects = $objects = $pager->getCurrentPageResults(); |
|
44
|
|
|
$sliceSize = count($objects); |
|
45
|
|
|
$objects = $this->filterObjects($options, $objects); |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
if (!empty($objects)) { |
|
48
|
|
|
$this->objectPersister->insertMany($objects); |
|
49
|
|
|
} |
|
50
|
|
|
} catch (BulkResponseException $e) { |
|
51
|
|
|
if (!$options['ignore_errors']) { |
|
52
|
|
|
throw $e; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
View Code Duplication |
if (null !== $loggerClosure) { |
|
56
|
|
|
$loggerClosure( |
|
57
|
|
|
$options['batch_size'], |
|
58
|
|
|
$nbObjects, |
|
59
|
|
|
sprintf('<error>%s</error>', $e->getMessage()) |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if (null !== $loggerClosure) { |
|
65
|
|
|
$loggerClosure($sliceSize, $nbObjects); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$pager->setCurrentPage($pager->getNextPage()); |
|
69
|
|
|
|
|
70
|
|
|
usleep($options['sleep']); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Filters objects away if they are not indexable. |
|
76
|
|
|
* |
|
77
|
|
|
* @param array $options |
|
78
|
|
|
* @param array $objects |
|
79
|
|
|
* @return array |
|
80
|
|
|
*/ |
|
81
|
|
View Code Duplication |
protected function filterObjects(array $options, array $objects) |
|
82
|
|
|
{ |
|
83
|
|
|
if ($options['skip_indexable_check']) { |
|
84
|
|
|
return $objects; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$index = $options['indexName']; |
|
88
|
|
|
$type = $options['typeName']; |
|
89
|
|
|
|
|
90
|
|
|
$return = array(); |
|
91
|
|
|
foreach ($objects as $object) { |
|
92
|
|
|
if (!$this->indexable->isObjectIndexable($index, $type, $object)) { |
|
93
|
|
|
continue; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$return[] = $object; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $return; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.