Completed
Pull Request — master (#1320)
by Maksim
14:06
created

InPlacePagerPersister::insert()   B

Complexity

Conditions 6
Paths 59

Size

Total Lines 57
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 8.7433
c 0
b 0
f 0
cc 6
eloc 36
nc 59
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace FOS\ElasticaBundle\Persister;
4
5
use FOS\ElasticaBundle\Persister\Event\Events;
6
use FOS\ElasticaBundle\Persister\Event\OnExceptionEvent;
7
use FOS\ElasticaBundle\Persister\Event\PostInsertObjectsEvent;
8
use FOS\ElasticaBundle\Persister\Event\PostPersistEvent;
9
use FOS\ElasticaBundle\Persister\Event\PreFetchObjectsEvent;
10
use FOS\ElasticaBundle\Persister\Event\PreInsertObjectsEvent;
11
use FOS\ElasticaBundle\Persister\Event\PrePersistEvent;
12
use FOS\ElasticaBundle\Provider\PagerInterface;
13
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
14
15
final class InPlacePagerPersister implements PagerPersisterInterface
16
{
17
    /**
18
     * @var PersisterRegistry
19
     */
20
    private $registry;
21
    
22
    /**
23
     * @var EventDispatcherInterface
24
     */
25
    private $dispatcher;
26
27
    /**
28
     * @param PersisterRegistry $registry
29
     * @param EventDispatcherInterface $dispatcher
30
     */
31
    public function __construct(PersisterRegistry $registry, EventDispatcherInterface $dispatcher)
32
    {
33
        $this->registry = $registry;
34
        $this->dispatcher = $dispatcher;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function insert(PagerInterface $pager, array $options = array())
41
    {
42
        $objectPersister = $this->registry->getPersister($options['indexName'], $options['typeName']);
43
44
        $event = new PrePersistEvent($pager, $objectPersister, $options);
45
        $this->dispatcher->dispatch(Events::PRE_PERSIST, $event);
46
        $pager = $event->getPager();
47
        $options = $event->getOptions();
48
49
        $pager->setMaxPerPage($options['batch_size']);
50
51
        $page = $pager->getCurrentPage();
52
        while ($page <= $pager->getNbPages()) {
53
            try {
54
                $pager->setCurrentPage($page);
55
56
                $event = new PreFetchObjectsEvent($pager, $objectPersister, $options);
57
                $this->dispatcher->dispatch(Events::PRE_FETCH_OBJECTS, $event);
58
                $pager = $event->getPager();
59
                $options = $event->getOptions();
60
61
                $objects = $pager->getCurrentPageResults();
62
63
                if ($objects instanceof \Traversable) {
64
                    $objects = iterator_to_array($objects);
65
                }
66
67
                $event = new PreInsertObjectsEvent($pager, $objectPersister, $objects, $options);
68
                $this->dispatcher->dispatch(Events::PRE_INSERT_OBJECTS, $event);
69
                $pager = $event->getPager();
70
                $options = $event->getOptions();
71
                $objects = $event->getObjects();
72
73
                if (!empty($objects)) {
74
                    $objectPersister->insertMany($objects);
75
                }
76
77
                $event = new PostInsertObjectsEvent($pager, $objectPersister, $objects, $options);
78
                $this->dispatcher->dispatch(Events::POST_INSERT_OBJECTS, $event);
79
            } catch (\Exception $e) {
80
                $event = new OnExceptionEvent($pager, $objectPersister, $e, $options);
81
                $this->dispatcher->dispatch(Events::ON_EXCEPTION, $event);
82
83
                if (false == $event->isIgnored()) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
84
                    $e = $event->getException();
85
86
                    throw $e;
87
                }
88
            }
89
90
            $pager->setCurrentPage($page++);
91
        }
92
93
        $event = new PostPersistEvent($pager, $objectPersister, $options);
94
        $this->dispatcher->dispatch(Events::POST_PERSIST, $event);
95
96
    }
97
}
98