Completed
Pull Request — master (#1569)
by
unknown
03:33
created

InPlacePagerPersister   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 97.83%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 10
dl 0
loc 98
ccs 45
cts 46
cp 0.9783
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A insert() 0 32 3
A insertPage() 0 37 5
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));
0 ignored issues
show
Documentation introduced by
$event = new \FOS\Elasti...ectPersister, $options) is of type object<FOS\ElasticaBundl...\Event\PrePersistEvent>, but the function expects a object<Symfony\Contracts\EventDispatcher\object>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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));
0 ignored issues
show
Documentation introduced by
new \FOS\ElasticaBundle\...ectPersister, $options) is of type object<FOS\ElasticaBundl...Event\PostPersistEvent>, but the function expects a object<Symfony\Contracts\EventDispatcher\object>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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));
0 ignored issues
show
Documentation introduced by
$event = new \FOS\Elasti...ectPersister, $options) is of type object<FOS\ElasticaBundl...t\PreFetchObjectsEvent>, but the function expects a object<Symfony\Contracts\EventDispatcher\object>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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));
0 ignored issues
show
Documentation introduced by
$event = new \FOS\Elasti...er, $objects, $options) is of type object<FOS\ElasticaBundl...\PreInsertObjectsEvent>, but the function expects a object<Symfony\Contracts\EventDispatcher\object>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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));
0 ignored issues
show
Documentation introduced by
new \FOS\ElasticaBundle\...er, $objects, $options) is of type object<FOS\ElasticaBundl...PostInsertObjectsEvent>, but the function expects a object<Symfony\Contracts\EventDispatcher\object>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
98 2
        } catch (\Exception $e) {
99 2
            $this->dispatcher->dispatch($event = new OnExceptionEvent($pager, $objectPersister, $e, $objects, $options));
0 ignored issues
show
Documentation introduced by
$event = new \FOS\Elasti...$e, $objects, $options) is of type object<FOS\ElasticaBundl...Event\OnExceptionEvent>, but the function expects a object<Symfony\Contracts\EventDispatcher\object>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
100
101 2
            if ($event->isIgnored()) {
102 1
                $this->dispatcher->dispatch(new PostInsertObjectsEvent($pager, $objectPersister, $objects, $options));
0 ignored issues
show
Documentation introduced by
new \FOS\ElasticaBundle\...er, $objects, $options) is of type object<FOS\ElasticaBundl...PostInsertObjectsEvent>, but the function expects a object<Symfony\Contracts\EventDispatcher\object>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
103
            } else {
104 1
                $e = $event->getException();
105
106 1
                throw $e;
107
            }
108
        }
109 9
    }
110
111
}
112