| Conditions | 9 |
| Paths | 36 |
| Total Lines | 121 |
| Lines | 37 |
| Ratio | 30.58 % |
| 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 |
||
| 36 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 37 | { |
||
| 38 | @trigger_error( |
||
|
|
|||
| 39 | sprintf('%s is deprecated since 6.7. Use ezplatform:reindex command instead', $this->getName()), |
||
| 40 | E_USER_DEPRECATED |
||
| 41 | ); |
||
| 42 | |||
| 43 | $bulkCount = $input->getArgument('bulk_count'); |
||
| 44 | |||
| 45 | /** @var \eZ\Publish\SPI\Persistence\Handler $persistenceHandler */ |
||
| 46 | $persistenceHandler = $this->getContainer()->get('ezpublish.api.persistence_handler'); |
||
| 47 | /** @var \eZ\Publish\SPI\Search\Handler $searchHandler */ |
||
| 48 | $searchHandler = $this->getContainer()->get('ezpublish.spi.search'); |
||
| 49 | /** @var \eZ\Publish\Core\Persistence\Database\DatabaseHandler $databaseHandler */ |
||
| 50 | $databaseHandler = $this->getContainer()->get('ezpublish.connection'); |
||
| 51 | |||
| 52 | // Indexing Content |
||
| 53 | $query = $databaseHandler->createSelectQuery(); |
||
| 54 | $query->select('count(id)') |
||
| 55 | ->from('ezcontentobject') |
||
| 56 | ->where($query->expr->eq('status', ContentInfo::STATUS_PUBLISHED)); |
||
| 57 | $stmt = $query->prepare(); |
||
| 58 | $stmt->execute(); |
||
| 59 | $totalCount = $stmt->fetchColumn(); |
||
| 60 | |||
| 61 | $query = $databaseHandler->createSelectQuery(); |
||
| 62 | $query->select('id', 'current_version') |
||
| 63 | ->from('ezcontentobject') |
||
| 64 | ->where($query->expr->eq('status', ContentInfo::STATUS_PUBLISHED)); |
||
| 65 | |||
| 66 | $stmt = $query->prepare(); |
||
| 67 | $stmt->execute(); |
||
| 68 | |||
| 69 | /** @var \eZ\Publish\Core\Search\Elasticsearch\Content\Handler $searchHandler */ |
||
| 70 | $searchHandler->setCommit(true); |
||
| 71 | $searchHandler->purgeIndex(); |
||
| 72 | |||
| 73 | $output->writeln('Indexing Content...'); |
||
| 74 | |||
| 75 | /** @var \Symfony\Component\Console\Helper\ProgressHelper $progress */ |
||
| 76 | $progress = $this->getHelperSet()->get('progress'); |
||
| 77 | $progress->start($output, $totalCount); |
||
| 78 | $i = 0; |
||
| 79 | View Code Duplication | do { |
|
| 80 | $contentObjects = []; |
||
| 81 | |||
| 82 | for ($k = 0; $k <= $bulkCount; ++$k) { |
||
| 83 | if (!$row = $stmt->fetch(PDO::FETCH_ASSOC)) { |
||
| 84 | break; |
||
| 85 | } |
||
| 86 | |||
| 87 | $contentObjects[] = $persistenceHandler->contentHandler()->load( |
||
| 88 | $row['id'], |
||
| 89 | $row['current_version'] |
||
| 90 | ); |
||
| 91 | } |
||
| 92 | |||
| 93 | if (!empty($contentObjects)) { |
||
| 94 | $searchHandler->bulkIndexContent($contentObjects); |
||
| 95 | } |
||
| 96 | |||
| 97 | $progress->advance($k); |
||
| 98 | } while (($i += $bulkCount) < $totalCount); |
||
| 99 | |||
| 100 | $progress->finish(); |
||
| 101 | |||
| 102 | // Indexing Locations |
||
| 103 | $query = $databaseHandler->createSelectQuery(); |
||
| 104 | $query |
||
| 105 | ->select('count(node_id)') |
||
| 106 | ->from('ezcontentobject_tree') |
||
| 107 | ->where( |
||
| 108 | $query->expr->neq( |
||
| 109 | $databaseHandler->quoteColumn('contentobject_id'), |
||
| 110 | $query->bindValue(0, null, PDO::PARAM_INT) |
||
| 111 | ) |
||
| 112 | ); |
||
| 113 | $stmt = $query->prepare(); |
||
| 114 | $stmt->execute(); |
||
| 115 | $totalCount = $stmt->fetchColumn(); |
||
| 116 | |||
| 117 | $query = $databaseHandler->createSelectQuery(); |
||
| 118 | $query |
||
| 119 | ->select('node_id') |
||
| 120 | ->from('ezcontentobject_tree') |
||
| 121 | ->where( |
||
| 122 | $query->expr->neq( |
||
| 123 | $databaseHandler->quoteColumn('contentobject_id'), |
||
| 124 | $query->bindValue(0, null, PDO::PARAM_INT) |
||
| 125 | ) |
||
| 126 | ); |
||
| 127 | |||
| 128 | $stmt = $query->prepare(); |
||
| 129 | $stmt->execute(); |
||
| 130 | |||
| 131 | $output->writeln('Indexing Locations...'); |
||
| 132 | |||
| 133 | /** @var \Symfony\Component\Console\Helper\ProgressHelper $progress */ |
||
| 134 | $progress = $this->getHelperSet()->get('progress'); |
||
| 135 | $progress->start($output, $totalCount); |
||
| 136 | $i = 0; |
||
| 137 | View Code Duplication | do { |
|
| 138 | $locations = []; |
||
| 139 | |||
| 140 | for ($k = 0; $k <= $bulkCount; ++$k) { |
||
| 141 | if (!$row = $stmt->fetch(PDO::FETCH_ASSOC)) { |
||
| 142 | break; |
||
| 143 | } |
||
| 144 | |||
| 145 | $locations[] = $persistenceHandler->locationHandler()->load($row['node_id']); |
||
| 146 | } |
||
| 147 | |||
| 148 | if (!empty($locations)) { |
||
| 149 | $searchHandler->bulkIndexLocations($locations); |
||
| 150 | } |
||
| 151 | |||
| 152 | $progress->advance($k); |
||
| 153 | } while (($i += $bulkCount) < $totalCount); |
||
| 154 | |||
| 155 | $progress->finish(); |
||
| 156 | } |
||
| 157 | } |
||
| 158 |
If you suppress an error, we recommend checking for the error condition explicitly: