Completed
Pull Request — master (#1240)
by Maksim
04:15
created

PagerPersister::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace FOS\ElasticaBundle\Persister;
4
5
use FOS\ElasticaBundle\Provider\Indexable;
6
use Pagerfanta\Pagerfanta;
7
use Elastica\Exception\Bulk\ResponseException as BulkResponseException;
8
9
class PagerPersister implements PagerPersisterInterface
10
{
11
    /**
12
     * @var Indexable
13
     */
14
    private $indexable;
15
16
    /**
17
     * @param Indexable $indexable
18
     */
19
    public function __construct(Indexable $indexable)
20
    {
21
        $this->indexable = $indexable;
22
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function insert(Pagerfanta $pager, ObjectPersisterInterface $objectPersister, \Closure $loggerClosure = null, array $options = array())
28
    {
29
        $nbObjects = $pager->getNbResults();
30
31
        $page = $pager->getCurrentPage();
32
        for(;$page <= $pager->getNbPages(); $page++) {
33
            $pager->setCurrentPage($page);
34
35
            $sliceSize = $options['batch_size'];
36
37
            try {
38
                $objects = $pager->getCurrentPageResults();
39
                $sliceSize = count($objects);
40
                $objects = $this->filterObjects($options, $objects);
0 ignored issues
show
Bug introduced by
It seems like $objects can also be of type object<Traversable>; however, FOS\ElasticaBundle\Persi...sister::filterObjects() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
41
42
                if (!empty($objects)) {
43
                    $objectPersister->insertMany($objects);
44
                }
45
            } catch (BulkResponseException $e) {
46
                if (!$options['ignore_errors']) {
47
                    throw $e;
48
                }
49
50 View Code Duplication
                if (null !== $loggerClosure) {
51
                    $loggerClosure(
52
                        $options['batch_size'],
53
                        $nbObjects,
54
                        sprintf('<error>%s</error>', $e->getMessage())
55
                    );
56
                }
57
            }
58
59
            if (null !== $loggerClosure) {
60
                $loggerClosure($sliceSize, $nbObjects);
61
            }
62
63
            usleep($options['sleep']);
64
        }
65
    }
66
67
    /**
68
     * Filters objects away if they are not indexable.
69
     *
70
     * @param array $options
71
     * @param array $objects
72
     * @return array
73
     */
74 View Code Duplication
    protected function filterObjects(array $options, array $objects)
75
    {
76
        if ($options['skip_indexable_check']) {
77
            return $objects;
78
        }
79
80
        $index = $options['indexName'];
81
        $type = $options['typeName'];
82
83
        $return = array();
84
        foreach ($objects as $object) {
85
            if (!$this->indexable->isObjectIndexable($index, $type, $object)) {
86
                continue;
87
            }
88
89
            $return[] = $object;
90
        }
91
92
        return $return;
93
    }
94
}
95