Completed
Pull Request — master (#1240)
by Maksim
03:38
created

PagerPersister::filterObjects()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 20
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 20
loc 20
ccs 0
cts 16
cp 0
rs 9.2
cc 4
eloc 11
nc 4
nop 2
crap 20
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
     * @var PersisterRegistry
18
     */
19
    private $persisterRegistry;
20
21
    /**
22
     * @param Indexable $indexable
23
     * @param PersisterRegistry $persisterRegistry
24
     */
25
    public function __construct(Indexable $indexable, PersisterRegistry $persisterRegistry)
26
    {
27
        $this->indexable = $indexable;
28
        $this->persisterRegistry = $persisterRegistry;
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function insert(Pagerfanta $pager, \Closure $loggerClosure = null, array $options = array())
35
    {
36
        $nbObjects = $pager->getNbResults();
37
38
39
        $objectPersister = $this->persisterRegistry->getPersister($options['indexName'], $options['typeName']);
40
41
        $page = $pager->getCurrentPage();
42
        for(;$page <= $pager->getNbPages(); $page++) {
43
            $pager->setCurrentPage($page);
44
45
            $sliceSize = $options['batch_size'];
46
47
            try {
48
                $objects = $pager->getCurrentPageResults();
49
                $sliceSize = count($objects);
50
                $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...
51
52
                if (!empty($objects)) {
53
                    $objectPersister->insertMany($objects);
54
                }
55
            } catch (BulkResponseException $e) {
56
                if (!$options['ignore_errors']) {
57
                    throw $e;
58
                }
59
60 View Code Duplication
                if (null !== $loggerClosure) {
61
                    $loggerClosure(
62
                        $options['batch_size'],
63
                        $nbObjects,
64
                        sprintf('<error>%s</error>', $e->getMessage())
65
                    );
66
                }
67
            }
68
69
            if (null !== $loggerClosure) {
70
                $loggerClosure($sliceSize, $nbObjects);
71
            }
72
73
            usleep($options['sleep']);
74
        }
75
    }
76
77
    /**
78
     * Filters objects away if they are not indexable.
79
     *
80
     * @param array $options
81
     * @param array $objects
82
     * @return array
83
     */
84 View Code Duplication
    protected function filterObjects(array $options, array $objects)
85
    {
86
        if ($options['skip_indexable_check']) {
87
            return $objects;
88
        }
89
90
        $index = $options['indexName'];
91
        $type = $options['typeName'];
92
93
        $return = array();
94
        foreach ($objects as $object) {
95
            if (!$this->indexable->isObjectIndexable($index, $type, $object)) {
96
                continue;
97
            }
98
99
            $return[] = $object;
100
        }
101
102
        return $return;
103
    }
104
}
105