InPlacePagerPersister::insert()   A
last analyzed

Complexity

Conditions 3
Paths 8

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 21
cts 21
cp 1
rs 9.408
c 0
b 0
f 0
cc 3
nc 8
nop 2
crap 3
1
<?php
2
3
/*
4
 * This file is part of the FOSElasticaBundle package.
5
 *
6
 * (c) FriendsOfSymfony <https://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\ElasticaBundle\Persister;
13
14
use FOS\ElasticaBundle\Persister\Event\OnExceptionEvent;
15
use FOS\ElasticaBundle\Persister\Event\PostInsertObjectsEvent;
16
use FOS\ElasticaBundle\Persister\Event\PostPersistEvent;
17
use FOS\ElasticaBundle\Persister\Event\PreFetchObjectsEvent;
18
use FOS\ElasticaBundle\Persister\Event\PreInsertObjectsEvent;
19
use FOS\ElasticaBundle\Persister\Event\PrePersistEvent;
20
use FOS\ElasticaBundle\Provider\PagerInterface;
21
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
22
23
final class InPlacePagerPersister implements PagerPersisterInterface
24
{
25
    public const NAME = 'in_place';
26
27
    /**
28
     * @var PersisterRegistry
29
     */
30
    private $registry;
31
32
    /**
33
     * @var EventDispatcherInterface
34
     */
35
    private $dispatcher;
36
37 12
    public function __construct(PersisterRegistry $registry, EventDispatcherInterface $dispatcher)
38
    {
39 12
        $this->registry = $registry;
40 12
        $this->dispatcher = $dispatcher;
41 12
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 11
    public function insert(PagerInterface $pager, array $options = [])
47
    {
48 11
        $pager->setMaxPerPage(empty($options['max_per_page']) ? 100 : $options['max_per_page']);
49
50 11
        $options = \array_replace([
51 11
            'max_per_page' => $pager->getMaxPerPage(),
52 11
            'first_page' => $pager->getCurrentPage(),
53 11
            'last_page' => $pager->getNbPages(),
54 11
        ], $options);
55
56 11
        $pager->setCurrentPage($options['first_page']);
57
58 11
        $objectPersister = $this->registry->getPersister($options['indexName']);
59
60
        try {
61 11
            $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...
62 11
            $pager = $event->getPager();
63 11
            $options = $event->getOptions();
64
65 11
            $lastPage = \min($options['last_page'], $pager->getNbPages());
66 11
            $page = $pager->getCurrentPage();
67
            do {
68 11
                $pager->setCurrentPage($page);
69
70 11
                $this->insertPage($page, $pager, $objectPersister, $options);
71
72 10
                ++$page;
73 10
            } while ($page <= $lastPage);
74 10
        } finally {
75 11
            $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...
76
        }
77 10
    }
78
79
    /**
80
     * @throws \Exception
81
     */
82 11
    private function insertPage(int $page, PagerInterface $pager, ObjectPersisterInterface $objectPersister, array $options = []): void
83
    {
84 11
        $pager->setCurrentPage($page);
85
86 11
        $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...
87 11
        $pager = $event->getPager();
88 11
        $options = $event->getOptions();
89
90 11
        $objects = $pager->getCurrentPageResults();
91
92 11
        if ($objects instanceof \Traversable) {
93
            $objects = \iterator_to_array($objects);
94
        }
95
96 11
        $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...
97 11
        $pager = $event->getPager();
98 11
        $options = $event->getOptions();
99 11
        $objects = $event->getObjects();
100
101
        try {
102 11
            if (!empty($objects)) {
103 10
                $objectPersister->insertMany($objects);
104
            }
105
106 9
            $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...
107 2
        } catch (\Exception $e) {
108 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...
109
110 2
            if ($event->isIgnored()) {
111 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...
112
            } else {
113 1
                $e = $event->getException();
114
115 1
                throw $e;
116
            }
117
        }
118 10
    }
119
}
120