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 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( |
|
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 | 8 | $this->objectPersister->insertMany($objects); |
|
106 | 7 | } |
|
107 | 9 | } 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 | 7 | } |
|
124 | |||
125 | 8 | usleep($options['sleep']); |
|
126 | |||
127 | 8 | if (null !== $loggerClosure) { |
|
128 | 1 | $loggerClosure($sliceSize, $nbObjects); |
|
129 | 1 | } |
|
130 | 8 | } |
|
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 | 9 | 'debug_logging' => false, |
|
143 | 9 | 'ignore_errors' => false, |
|
144 | 9 | 'offset' => 0, |
|
145 | 9 | 'query_builder_method' => 'createQueryBuilder', |
|
146 | 9 | 'sleep' => 0, |
|
147 | 9 | ]); |
|
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) |
|
180 | } |
||
181 |