Conditions | 10 |
Paths | 29 |
Total Lines | 59 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
142 | protected function indexEntity(InputInterface $input, OutputInterface $output, $entity) { |
||
143 | $output->writeln(sprintf('<comment>Indexing entities of type "%s"</comment>', $entity)); |
||
144 | /** |
||
145 | * |
||
146 | * @var EntityManager $entityManager |
||
147 | */ |
||
148 | $entityManager = $this->searchService->getObjectManager(); |
||
149 | $repository = null; |
||
150 | try { |
||
151 | // Get repository for the given entity type |
||
152 | $repository = $entityManager->getRepository($entity); |
||
153 | } catch(\Exception $e) { |
||
154 | $output->writeln(sprintf('<error>No repository found for "%s", check your input</error>', $entity)); |
||
155 | return; |
||
156 | } |
||
157 | |||
158 | // Get all entities |
||
159 | $queryBuilder = $repository->createQueryBuilder('e'); |
||
160 | $countQueryBuilder = $repository->createQueryBuilder('e')->select('COUNT(e)'); |
||
161 | $entityCount = (int)$countQueryBuilder->getQuery()->getSingleScalarResult(); |
||
162 | |||
163 | $useBatch = !($entityManager->getConnection()->getDatabasePlatform() instanceof SQLServerPlatform); |
||
164 | $iterableResult = $useBatch ? $queryBuilder->getQuery()->iterate() : $queryBuilder->getQuery()->getResult(); |
||
165 | if($entityCount === 0) { |
||
166 | $output->writeln('<comment>No entities found for indexing</comment>'); |
||
167 | return; |
||
168 | } |
||
169 | $progressBar = new ProgressBar($output, $entityCount); |
||
170 | $progressBar->display(); |
||
171 | |||
172 | $entitiesIndexed = 0; |
||
173 | |||
174 | // Index each entity separate |
||
175 | foreach($iterableResult as $row) { |
||
176 | $entity = $useBatch ? $row[0] : $row; |
||
177 | $progressBar->advance(); |
||
178 | if($this->entityToDocumentMapper->isIndexable($entity)) { |
||
179 | $document = $this->entityToDocumentMapper->createDocument($entityManager, $entity); |
||
180 | if($document === null) { |
||
181 | continue; |
||
182 | } |
||
183 | try { |
||
184 | $this->searchService->saveDocument($document); |
||
185 | $entitiesIndexed++; |
||
186 | } catch(\Exception $e) { |
||
187 | $output->writeln('<error>Failed to index entity with ID ' . $document->getEntityId() . '</error>'); |
||
188 | } |
||
189 | if($entitiesIndexed % 50 === 0) { |
||
190 | $entityManager->flush(); |
||
191 | } |
||
192 | } |
||
193 | |||
194 | } |
||
195 | $entityManager->flush(); |
||
196 | $entityManager->clear(); |
||
197 | $progressBar->finish(); |
||
198 | $output->writeln(''); |
||
199 | $output->writeln('<comment>Indexed ' . $entitiesIndexed . ' entities</comment>'); |
||
200 | } |
||
201 | |||
202 | } |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.