Completed
Pull Request — master (#1240)
by Maksim
10:45
created

PagerPersister   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 93
Duplicated Lines 29.03 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 13
c 0
b 0
f 0
lcom 1
cbo 4
dl 27
loc 93
ccs 0
cts 56
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C insert() 7 41 8
A filterObjects() 20 20 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
10
{
11
    /**
12
     * @var ObjectPersisterInterface
13
     */
14
    private $objectPersister;
15
16
    /**
17
     * @var Indexable
18
     */
19
    private $indexable;
20
21
    /**
22
     * PagerPersister constructor.
23
     * @param ObjectPersisterInterface $objectPersister
24
     * @param Indexable $indexable
25
     */
26
    public function __construct(ObjectPersisterInterface $objectPersister, Indexable $indexable)
27
    {
28
        $this->objectPersister = $objectPersister;
29
        $this->indexable = $indexable;
30
    }
31
32
    public function insert(Pagerfanta $pager, \Closure $loggerClosure = null, array $options = array())
33
    {
34
        $nbObjects = $pager->getNbResults();
35
        $pager->setCurrentPage(1);
36
        while (true) {
37
            if ($pager->getNextPage()) {
38
                return;
39
            }
40
41
            $sliceSize = $options['batch_size'];
42
            try {
43
                $objects = $objects = $pager->getCurrentPageResults();
44
                $sliceSize = count($objects);
45
                $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...
46
47
                if (!empty($objects)) {
48
                    $this->objectPersister->insertMany($objects);
49
                }
50
            } catch (BulkResponseException $e) {
51
                if (!$options['ignore_errors']) {
52
                    throw $e;
53
                }
54
55 View Code Duplication
                if (null !== $loggerClosure) {
56
                    $loggerClosure(
57
                        $options['batch_size'],
58
                        $nbObjects,
59
                        sprintf('<error>%s</error>', $e->getMessage())
60
                    );
61
                }
62
            }
63
64
            if (null !== $loggerClosure) {
65
                $loggerClosure($sliceSize, $nbObjects);
66
            }
67
68
            $pager->setCurrentPage($pager->getNextPage());
69
70
            usleep($options['sleep']);
71
        }
72
    }
73
74
    /**
75
     * Filters objects away if they are not indexable.
76
     *
77
     * @param array $options
78
     * @param array $objects
79
     * @return array
80
     */
81 View Code Duplication
    protected function filterObjects(array $options, array $objects)
82
    {
83
        if ($options['skip_indexable_check']) {
84
            return $objects;
85
        }
86
87
        $index = $options['indexName'];
88
        $type = $options['typeName'];
89
90
        $return = array();
91
        foreach ($objects as $object) {
92
            if (!$this->indexable->isObjectIndexable($index, $type, $object)) {
93
                continue;
94
            }
95
96
            $return[] = $object;
97
        }
98
99
        return $return;
100
    }
101
}
102