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 |
||
20 | abstract class AbstractProvider extends BaseAbstractProvider |
||
21 | { |
||
22 | /** |
||
23 | * @var SliceFetcherInterface |
||
24 | */ |
||
25 | private $sliceFetcher; |
||
26 | |||
27 | /** |
||
28 | * @var ManagerRegistry |
||
29 | */ |
||
30 | protected $managerRegistry; |
||
31 | |||
32 | /** |
||
33 | * Constructor. |
||
34 | * |
||
35 | * @param ObjectPersisterInterface $objectPersister |
||
36 | * @param IndexableInterface $indexable |
||
37 | * @param string $objectClass |
||
38 | * @param array $baseOptions |
||
39 | * @param ManagerRegistry $managerRegistry |
||
40 | * @param SliceFetcherInterface $sliceFetcher |
||
41 | */ |
||
42 | 13 | public function __construct( |
|
55 | |||
56 | /** |
||
57 | * Counts objects that would be indexed using the query builder. |
||
58 | * |
||
59 | * @param object $queryBuilder |
||
60 | * |
||
61 | * @return int |
||
62 | */ |
||
63 | abstract protected function countObjects($queryBuilder); |
||
64 | |||
65 | /** |
||
66 | * Creates the query builder, which will be used to fetch objects to index. |
||
67 | * |
||
68 | * @param string $method |
||
69 | * @param array $arguments |
||
70 | * |
||
71 | * @return object |
||
72 | */ |
||
73 | abstract protected function createQueryBuilder($method, array $arguments = []); |
||
74 | |||
75 | /** |
||
76 | * Fetches a slice of objects using the query builder. |
||
77 | * |
||
78 | * @param object $queryBuilder |
||
79 | * @param int $limit |
||
80 | * @param int $offset |
||
81 | * |
||
82 | * @return array |
||
83 | */ |
||
84 | abstract protected function fetchSlice($queryBuilder, $limit, $offset); |
||
85 | |||
86 | /** |
||
87 | * {@inheritdoc} |
||
88 | */ |
||
89 | 13 | protected function doPopulate($options, \Closure $loggerClosure = null) |
|
90 | { |
||
91 | 13 | $manager = $this->managerRegistry->getManagerForClass($this->objectClass); |
|
92 | |||
93 | 13 | $queryBuilder = $this->createQueryBuilder($options['query_builder_method']); |
|
94 | 13 | $limit = $nbObjects = $this->countObjects($queryBuilder); |
|
95 | 13 | if ($options['limit'] && ($nbObjects - $options['offset']) > $options['limit']) { |
|
96 | 2 | $limit = $options['limit'] + $options['offset']; |
|
97 | } |
||
98 | 13 | $offset = $options['offset']; |
|
99 | 13 | if ($options['limit'] && $options['limit'] < $options['batch_size']) { |
|
100 | $options['batch_size'] = $options['limit']; |
||
101 | } |
||
102 | |||
103 | 13 | $objects = []; |
|
104 | 13 | for (; $offset < $limit; $offset += $options['batch_size']) { |
|
105 | 13 | $sliceSize = $options['batch_size']; |
|
106 | try { |
||
107 | 13 | $objects = $this->getSlice($queryBuilder, $options['batch_size'], $offset, $objects); |
|
108 | 13 | $sliceSize = count($objects); |
|
109 | 13 | $objects = $this->filterObjects($options, $objects); |
|
110 | |||
111 | 13 | if (!empty($objects)) { |
|
112 | 13 | $this->objectPersister->insertMany($objects); |
|
113 | } |
||
114 | 1 | } catch (BulkResponseException $e) { |
|
115 | 1 | if (!$options['ignore_errors']) { |
|
116 | 1 | throw $e; |
|
117 | } |
||
118 | |||
119 | if (null !== $loggerClosure) { |
||
120 | $loggerClosure( |
||
121 | $options['batch_size'], |
||
122 | $nbObjects, |
||
123 | sprintf('<error>%s</error>', $e->getMessage()) |
||
124 | ); |
||
125 | } |
||
126 | } |
||
127 | |||
128 | 12 | if ($options['clear_object_manager']) { |
|
129 | 11 | $manager->clear(); |
|
130 | } |
||
131 | |||
132 | 12 | usleep($options['sleep']); |
|
133 | |||
134 | 12 | if (null !== $loggerClosure) { |
|
135 | 1 | $loggerClosure($sliceSize, $nbObjects); |
|
136 | } |
||
137 | } |
||
138 | 12 | } |
|
139 | |||
140 | /** |
||
141 | * {@inheritdoc} |
||
142 | */ |
||
143 | 13 | View Code Duplication | protected function configureOptions() |
156 | |||
157 | /** |
||
158 | * If this Provider has a SliceFetcher defined, we use it instead of falling back to |
||
159 | * the fetchSlice methods defined in the ORM/MongoDB subclasses. |
||
160 | * |
||
161 | * @param $queryBuilder |
||
162 | * @param int $limit |
||
163 | * @param int $offset |
||
164 | * @param array $lastSlice |
||
165 | * |
||
166 | * @return array |
||
167 | */ |
||
168 | 13 | private function getSlice($queryBuilder, $limit, $offset, $lastSlice) |
|
187 | } |
||
188 |