Completed
Push — master ( 4e4cd8...063296 )
by Maksim
16s
created

InPlacePagerPersister::insert()   B

Complexity

Conditions 6
Paths 59

Size

Total Lines 57
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 35
CRAP Score 6.0007

Importance

Changes 0
Metric Value
dl 0
loc 57
ccs 35
cts 36
cp 0.9722
rs 8.7433
c 0
b 0
f 0
cc 6
eloc 36
nc 59
nop 2
crap 6.0007

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 9
    public function __construct(PersisterRegistry $registry, EventDispatcherInterface $dispatcher)
32
    {
33 9
        $this->registry = $registry;
34 9
        $this->dispatcher = $dispatcher;
35 9
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 8
    public function insert(PagerInterface $pager, array $options = array())
41
    {
42 8
        $objectPersister = $this->registry->getPersister($options['indexName'], $options['typeName']);
43
44 8
        $event = new PrePersistEvent($pager, $objectPersister, $options);
45 8
        $this->dispatcher->dispatch(Events::PRE_PERSIST, $event);
46 8
        $pager = $event->getPager();
47 8
        $options = $event->getOptions();
48
49 8
        $pager->setMaxPerPage($options['batch_size']);
50
51 8
        $page = $pager->getCurrentPage();
52 8
        while ($page <= $pager->getNbPages()) {
53
            try {
54 8
                $pager->setCurrentPage($page);
55
56 8
                $event = new PreFetchObjectsEvent($pager, $objectPersister, $options);
57 8
                $this->dispatcher->dispatch(Events::PRE_FETCH_OBJECTS, $event);
58 8
                $pager = $event->getPager();
59 8
                $options = $event->getOptions();
60
61 8
                $objects = $pager->getCurrentPageResults();
62
63 8
                if ($objects instanceof \Traversable) {
64
                    $objects = iterator_to_array($objects);
65
                }
66
67 8
                $event = new PreInsertObjectsEvent($pager, $objectPersister, $objects, $options);
68 8
                $this->dispatcher->dispatch(Events::PRE_INSERT_OBJECTS, $event);
69 8
                $pager = $event->getPager();
70 8
                $options = $event->getOptions();
71 8
                $objects = $event->getObjects();
72
73 8
                if (!empty($objects)) {
74 8
                    $objectPersister->insertMany($objects);
75
                }
76
77 6
                $event = new PostInsertObjectsEvent($pager, $objectPersister, $objects, $options);
78 6
                $this->dispatcher->dispatch(Events::POST_INSERT_OBJECTS, $event);
79 2
            } catch (\Exception $e) {
80 2
                $event = new OnExceptionEvent($pager, $objectPersister, $e, $options);
81 2
                $this->dispatcher->dispatch(Events::ON_EXCEPTION, $event);
82
83 2
                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 1
                    $e = $event->getException();
85
86 1
                    throw $e;
87
                }
88
            }
89
90 7
            $pager->setCurrentPage($page++);
91
        }
92
93 7
        $event = new PostPersistEvent($pager, $objectPersister, $options);
94 7
        $this->dispatcher->dispatch(Events::POST_PERSIST, $event);
95
96 7
    }
97
}
98