Completed
Pull Request — master (#1569)
by
unknown
08:21 queued 08:21
created

InPlacePagerPersister::insertPage()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 5.0023

Importance

Changes 0
Metric Value
dl 0
loc 37
ccs 21
cts 22
cp 0.9545
rs 9.0168
c 0
b 0
f 0
cc 5
nc 16
nop 4
crap 5.0023
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 9
    public function __construct(PersisterRegistry $registry, EventDispatcherInterface $dispatcher)
29
    {
30 9
        $this->registry = $registry;
31 9
        $this->dispatcher = $dispatcher;
32 9
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 8
    public function insert(PagerInterface $pager, array $options = array())
38
    {
39 8
        $pager->setMaxPerPage(empty($options['max_per_page']) ? 100 : $options['max_per_page']);
40
41 8
        $options = array_replace([
42 8
            'max_per_page' => $pager->getMaxPerPage(),
43 8
            'first_page' => $pager->getCurrentPage(),
44 8
            'last_page' => $pager->getNbPages(),
45
        ], $options);
46
47 8
        $pager->setCurrentPage($options['first_page']);
48
49 8
        $objectPersister = $this->registry->getPersister($options['indexName']);
50
51
        try {
52 8
            $this->dispatcher->dispatch($event = new PrePersistEvent($pager, $objectPersister, $options));
53 8
            $pager = $event->getPager();
54 8
            $options = $event->getOptions();
55
56 8
            $lastPage = min($options['last_page'], $pager->getNbPages());
57 8
            $page = $pager->getCurrentPage();
58
            do {
59 8
                $pager->setCurrentPage($page);
60
61 8
                $this->insertPage($page, $pager, $objectPersister, $options);
62
63 7
                $page++;
64 7
            } while ($page <= $lastPage);
65 7
        } finally {
66 8
            $this->dispatcher->dispatch(new PostPersistEvent($pager, $objectPersister, $options));
67
        }
68 7
    }
69
70
    /**
71
     * @throws \Exception
72
     */
73 8
    private function insertPage(int $page, PagerInterface $pager, ObjectPersisterInterface $objectPersister, array $options = array()): void
74
    {
75 8
        $pager->setCurrentPage($page);
76
77 8
        $this->dispatcher->dispatch($event = new PreFetchObjectsEvent($pager, $objectPersister, $options));
78 8
        $pager = $event->getPager();
79 8
        $options = $event->getOptions();
80
81 8
        $objects = $pager->getCurrentPageResults();
82
83 8
        if ($objects instanceof \Traversable) {
84
            $objects = iterator_to_array($objects);
85
        }
86
87 8
        $this->dispatcher->dispatch($event = new PreInsertObjectsEvent($pager, $objectPersister, $objects, $options));
88 8
        $pager = $event->getPager();
89 8
        $options = $event->getOptions();
90 8
        $objects = $event->getObjects();
91
92
        try {
93 8
            if (!empty($objects)) {
94 8
                $objectPersister->insertMany($objects);
95
            }
96
97 6
            $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 7
    }
110
111
}
112