Completed
Push — master ( 6eb08a...7e2c22 )
by Karel
02:55
created

AbstractProvider   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 161
Duplicated Lines 8.07 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 89.8%

Importance

Changes 0
Metric Value
wmc 12
lcom 2
cbo 6
dl 13
loc 161
ccs 44
cts 49
cp 0.898
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
countObjects() 0 1 ?
createQueryBuilder() 0 1 ?
fetchSlice() 0 1 ?
C doPopulate() 0 44 8
A configureOptions() 13 13 1
A getSlice() 0 19 2

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
/*
4
 * This file is part of the FOSElasticaBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://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\Doctrine;
13
14
use Doctrine\Common\Persistence\ManagerRegistry;
15
use Elastica\Exception\Bulk\ResponseException as BulkResponseException;
16
use FOS\ElasticaBundle\Persister\ObjectPersisterInterface;
17
use FOS\ElasticaBundle\Provider\AbstractProvider as BaseAbstractProvider;
18
use FOS\ElasticaBundle\Provider\IndexableInterface;
19
20
abstract class AbstractProvider extends BaseAbstractProvider
21
{
22
    /**
23
     * @var ManagerRegistry
24
     */
25
    protected $managerRegistry;
26
    /**
27
     * @var SliceFetcherInterface
28
     */
29
    private $sliceFetcher;
30
31
    /**
32
     * Constructor.
33
     *
34
     * @param ObjectPersisterInterface $objectPersister
35
     * @param IndexableInterface       $indexable
36
     * @param string                   $objectClass
37
     * @param array                    $baseOptions
38
     * @param ManagerRegistry          $managerRegistry
39
     * @param SliceFetcherInterface    $sliceFetcher
40
     */
41 9
    public function __construct(
42
        ObjectPersisterInterface $objectPersister,
43
        IndexableInterface $indexable,
44
        $objectClass,
45
        array $baseOptions,
46
        ManagerRegistry $managerRegistry,
47
        SliceFetcherInterface $sliceFetcher = null
48
    ) {
49 9
        parent::__construct($objectPersister, $indexable, $objectClass, $baseOptions);
50
51 9
        $this->managerRegistry = $managerRegistry;
52 9
        $this->sliceFetcher = $sliceFetcher;
53 9
    }
54
55
    /**
56
     * Counts objects that would be indexed using the query builder.
57
     *
58
     * @param object $queryBuilder
59
     *
60
     * @return int
61
     */
62
    abstract protected function countObjects($queryBuilder);
63
64
    /**
65
     * Creates the query builder, which will be used to fetch objects to index.
66
     *
67
     * @param string $method
68
     * @param array  $arguments
69
     *
70
     * @return object
71
     */
72
    abstract protected function createQueryBuilder($method, array $arguments = []);
73
74
    /**
75
     * Fetches a slice of objects using the query builder.
76
     *
77
     * @param object $queryBuilder
78
     * @param int    $limit
79
     * @param int    $offset
80
     *
81
     * @return array
82
     */
83
    abstract protected function fetchSlice($queryBuilder, $limit, $offset);
84
85
    /**
86
     * {@inheritdoc}
87
     */
88 9
    protected function doPopulate($options, \Closure $loggerClosure = null)
89
    {
90 9
        $manager = $this->managerRegistry->getManagerForClass($this->objectClass);
91
92 9
        $queryBuilder = $this->createQueryBuilder($options['query_builder_method']);
93 9
        $nbObjects = $this->countObjects($queryBuilder);
94 9
        $offset = $options['offset'];
95
96 9
        $objects = [];
97 9
        for (; $offset < $nbObjects; $offset += $options['batch_size']) {
98 9
            $sliceSize = $options['batch_size'];
99
            try {
100 9
                $objects = $this->getSlice($queryBuilder, $options['batch_size'], $offset, $objects);
101 9
                $sliceSize = count($objects);
102 9
                $objects = $this->filterObjects($options, $objects);
103
104 9
                if (!empty($objects)) {
105 9
                    $this->objectPersister->insertMany($objects);
106
                }
107 1
            } catch (BulkResponseException $e) {
108 1
                if (!$options['ignore_errors']) {
109 1
                    throw $e;
110
                }
111
112
                if (null !== $loggerClosure) {
113
                    $loggerClosure(
114
                        $options['batch_size'],
115
                        $nbObjects,
116
                        sprintf('<error>%s</error>', $e->getMessage())
117
                    );
118
                }
119
            }
120
121 8
            if ($options['clear_object_manager']) {
122 7
                $manager->clear();
123
            }
124
125 8
            usleep($options['sleep']);
126
127 8
            if (null !== $loggerClosure) {
128 1
                $loggerClosure($sliceSize, $nbObjects);
129
            }
130
        }
131 8
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136 9 View Code Duplication
    protected function configureOptions()
137
    {
138 9
        parent::configureOptions();
139
140 9
        $this->resolver->setDefaults([
141 9
            'clear_object_manager' => true,
142
            'debug_logging' => false,
143
            'ignore_errors' => false,
144
            'offset' => 0,
145
            'query_builder_method' => 'createQueryBuilder',
146
            'sleep' => 0,
147
        ]);
148 9
    }
149
150
    /**
151
     * If this Provider has a SliceFetcher defined, we use it instead of falling back to
152
     * the fetchSlice methods defined in the ORM/MongoDB subclasses.
153
     *
154
     * @param $queryBuilder
155
     * @param int   $limit
156
     * @param int   $offset
157
     * @param array $lastSlice
158
     *
159
     * @return array
160
     */
161 9
    private function getSlice($queryBuilder, $limit, $offset, $lastSlice)
162
    {
163 9
        if (!$this->sliceFetcher) {
164 2
            return $this->fetchSlice($queryBuilder, $limit, $offset);
165
        }
166
167 7
        $manager = $this->managerRegistry->getManagerForClass($this->objectClass);
168
        $identifierFieldNames = $manager
169 7
            ->getClassMetadata($this->objectClass)
170 7
            ->getIdentifierFieldNames();
171
172 7
        return $this->sliceFetcher->fetch(
173 7
            $queryBuilder,
174 7
            $limit,
175 7
            $offset,
176 7
            $lastSlice,
177 7
            $identifierFieldNames
178
        );
179
    }
180
}
181