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 | 9 | 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 | 9 | protected function doPopulate($options, \Closure $loggerClosure = null) |
|
90 | { |
||
91 | 9 | $manager = $this->managerRegistry->getManagerForClass($this->objectClass); |
|
92 | |||
93 | 9 | $queryBuilder = $this->createQueryBuilder($options['query_builder_method']); |
|
94 | 9 | $nbObjects = $this->countObjects($queryBuilder); |
|
95 | 9 | $offset = $options['offset']; |
|
96 | |||
97 | 9 | $objects = []; |
|
98 | 9 | for (; $offset < $nbObjects; $offset += $options['batch_size']) { |
|
99 | 9 | $sliceSize = $options['batch_size']; |
|
100 | try { |
||
101 | 9 | $objects = $this->getSlice($queryBuilder, $options['batch_size'], $offset, $objects); |
|
102 | 9 | $sliceSize = count($objects); |
|
103 | 9 | $objects = $this->filterObjects($options, $objects); |
|
104 | |||
105 | 9 | if (!empty($objects)) { |
|
106 | 8 | $this->objectPersister->insertMany($objects); |
|
107 | } |
||
108 | 1 | } catch (BulkResponseException $e) { |
|
109 | 1 | if (!$options['ignore_errors']) { |
|
110 | 1 | throw $e; |
|
111 | } |
||
112 | |||
113 | if (null !== $loggerClosure) { |
||
114 | $loggerClosure( |
||
115 | $options['batch_size'], |
||
116 | $nbObjects, |
||
117 | sprintf('<error>%s</error>', $e->getMessage()) |
||
118 | ); |
||
119 | } |
||
120 | } |
||
121 | |||
122 | 8 | if ($options['clear_object_manager']) { |
|
123 | 7 | $manager->clear(); |
|
124 | } |
||
125 | |||
126 | 8 | usleep($options['sleep']); |
|
127 | |||
128 | 8 | if (null !== $loggerClosure) { |
|
129 | 1 | $loggerClosure($sliceSize, $nbObjects); |
|
130 | } |
||
131 | } |
||
132 | 8 | } |
|
133 | |||
134 | /** |
||
135 | * {@inheritdoc} |
||
136 | */ |
||
137 | 9 | View Code Duplication | protected function configureOptions() |
150 | |||
151 | /** |
||
152 | * If this Provider has a SliceFetcher defined, we use it instead of falling back to |
||
153 | * the fetchSlice methods defined in the ORM/MongoDB subclasses. |
||
154 | * |
||
155 | * @param $queryBuilder |
||
156 | * @param int $limit |
||
157 | * @param int $offset |
||
158 | * @param array $lastSlice |
||
159 | * |
||
160 | * @return array |
||
161 | */ |
||
162 | 9 | private function getSlice($queryBuilder, $limit, $offset, $lastSlice) |
|
181 | } |
||
182 |