1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FOS\ElasticaBundle\Persister; |
4
|
|
|
|
5
|
|
|
use FOS\ElasticaBundle\Persister\Event\OnExceptionEvent; |
6
|
|
|
use FOS\ElasticaBundle\Persister\Event\PostInsertObjectsEvent; |
7
|
|
|
use FOS\ElasticaBundle\Persister\Event\PostPersistEvent; |
8
|
|
|
use FOS\ElasticaBundle\Persister\Event\PreFetchObjectsEvent; |
9
|
|
|
use FOS\ElasticaBundle\Persister\Event\PreInsertObjectsEvent; |
10
|
|
|
use FOS\ElasticaBundle\Persister\Event\PrePersistEvent; |
11
|
|
|
use FOS\ElasticaBundle\Provider\PagerInterface; |
12
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
13
|
|
|
|
14
|
|
|
final class InPlacePagerPersister implements PagerPersisterInterface |
15
|
|
|
{ |
16
|
|
|
public const NAME = 'in_place'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var PersisterRegistry |
20
|
|
|
*/ |
21
|
|
|
private $registry; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var EventDispatcherInterface |
25
|
|
|
*/ |
26
|
|
|
private $dispatcher; |
27
|
|
|
|
28
|
11 |
|
public function __construct(PersisterRegistry $registry, EventDispatcherInterface $dispatcher) |
29
|
|
|
{ |
30
|
11 |
|
$this->registry = $registry; |
31
|
11 |
|
$this->dispatcher = $dispatcher; |
32
|
11 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
10 |
|
public function insert(PagerInterface $pager, array $options = array()) |
38
|
|
|
{ |
39
|
10 |
|
$pager->setMaxPerPage(empty($options['max_per_page']) ? 100 : $options['max_per_page']); |
40
|
|
|
|
41
|
10 |
|
$options = array_replace([ |
42
|
10 |
|
'max_per_page' => $pager->getMaxPerPage(), |
43
|
10 |
|
'first_page' => $pager->getCurrentPage(), |
44
|
10 |
|
'last_page' => $pager->getNbPages(), |
45
|
|
|
], $options); |
46
|
|
|
|
47
|
10 |
|
$pager->setCurrentPage($options['first_page']); |
48
|
|
|
|
49
|
10 |
|
$objectPersister = $this->registry->getPersister($options['indexName']); |
50
|
|
|
|
51
|
|
|
try { |
52
|
10 |
|
$this->dispatcher->dispatch($event = new PrePersistEvent($pager, $objectPersister, $options)); |
|
|
|
|
53
|
10 |
|
$pager = $event->getPager(); |
54
|
10 |
|
$options = $event->getOptions(); |
55
|
|
|
|
56
|
10 |
|
$lastPage = min($options['last_page'], $pager->getNbPages()); |
57
|
10 |
|
$page = $pager->getCurrentPage(); |
58
|
|
|
do { |
59
|
10 |
|
$pager->setCurrentPage($page); |
60
|
|
|
|
61
|
10 |
|
$this->insertPage($page, $pager, $objectPersister, $options); |
62
|
|
|
|
63
|
9 |
|
$page++; |
64
|
9 |
|
} while ($page <= $lastPage); |
65
|
9 |
|
} finally { |
66
|
10 |
|
$this->dispatcher->dispatch(new PostPersistEvent($pager, $objectPersister, $options)); |
|
|
|
|
67
|
|
|
} |
68
|
9 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @throws \Exception |
72
|
|
|
*/ |
73
|
10 |
|
private function insertPage(int $page, PagerInterface $pager, ObjectPersisterInterface $objectPersister, array $options = array()): void |
74
|
|
|
{ |
75
|
10 |
|
$pager->setCurrentPage($page); |
76
|
|
|
|
77
|
10 |
|
$this->dispatcher->dispatch($event = new PreFetchObjectsEvent($pager, $objectPersister, $options)); |
|
|
|
|
78
|
10 |
|
$pager = $event->getPager(); |
79
|
10 |
|
$options = $event->getOptions(); |
80
|
|
|
|
81
|
10 |
|
$objects = $pager->getCurrentPageResults(); |
82
|
|
|
|
83
|
10 |
|
if ($objects instanceof \Traversable) { |
84
|
|
|
$objects = iterator_to_array($objects); |
85
|
|
|
} |
86
|
|
|
|
87
|
10 |
|
$this->dispatcher->dispatch($event = new PreInsertObjectsEvent($pager, $objectPersister, $objects, $options)); |
|
|
|
|
88
|
10 |
|
$pager = $event->getPager(); |
89
|
10 |
|
$options = $event->getOptions(); |
90
|
10 |
|
$objects = $event->getObjects(); |
91
|
|
|
|
92
|
|
|
try { |
93
|
10 |
|
if (!empty($objects)) { |
94
|
10 |
|
$objectPersister->insertMany($objects); |
95
|
|
|
} |
96
|
|
|
|
97
|
8 |
|
$this->dispatcher->dispatch(new PostInsertObjectsEvent($pager, $objectPersister, $objects, $options)); |
|
|
|
|
98
|
2 |
|
} catch (\Exception $e) { |
99
|
2 |
|
$this->dispatcher->dispatch($event = new OnExceptionEvent($pager, $objectPersister, $e, $objects, $options)); |
|
|
|
|
100
|
|
|
|
101
|
2 |
|
if ($event->isIgnored()) { |
102
|
1 |
|
$this->dispatcher->dispatch(new PostInsertObjectsEvent($pager, $objectPersister, $objects, $options)); |
|
|
|
|
103
|
|
|
} else { |
104
|
1 |
|
$e = $event->getException(); |
105
|
|
|
|
106
|
1 |
|
throw $e; |
107
|
|
|
} |
108
|
|
|
} |
109
|
9 |
|
} |
110
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: