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 |
||
| 25 | class Resetter |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @var AliasProcessor |
||
| 29 | */ |
||
| 30 | private $aliasProcessor; |
||
| 31 | |||
| 32 | /*** |
||
| 33 | * @var ManagerInterface |
||
| 34 | */ |
||
| 35 | private $configManager; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var EventDispatcherInterface |
||
| 39 | */ |
||
| 40 | private $dispatcher; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var IndexManager |
||
| 44 | */ |
||
| 45 | private $indexManager; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var MappingBuilder |
||
| 49 | */ |
||
| 50 | private $mappingBuilder; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @param ManagerInterface $configManager |
||
| 54 | * @param IndexManager $indexManager |
||
| 55 | * @param AliasProcessor $aliasProcessor |
||
| 56 | * @param MappingBuilder $mappingBuilder |
||
| 57 | * @param EventDispatcherInterface $eventDispatcher |
||
| 58 | */ |
||
| 59 | 15 | View Code Duplication | public function __construct( |
| 72 | |||
| 73 | /** |
||
| 74 | * Deletes and recreates all indexes. |
||
| 75 | * |
||
| 76 | * @param bool $populating |
||
| 77 | * @param bool $force |
||
| 78 | */ |
||
| 79 | 1 | public function resetAllIndexes($populating = false, $force = false) |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Deletes and recreates the named index. If populating, creates a new index |
||
| 88 | * with a randomised name for an alias to be set after population. |
||
| 89 | * |
||
| 90 | * @param string $indexName |
||
| 91 | * @param bool $populating |
||
| 92 | * @param bool $force If index exists with same name as alias, remove it |
||
| 93 | * |
||
| 94 | * @throws \InvalidArgumentException if no index exists for the given name |
||
| 95 | */ |
||
| 96 | 12 | public function resetIndex($indexName, $populating = false, $force = false) |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Deletes and recreates a mapping type for the named index. |
||
| 120 | * |
||
| 121 | * @param string $indexName |
||
| 122 | * @param string $typeName |
||
| 123 | * |
||
| 124 | * @throws \InvalidArgumentException if no index or type mapping exists for the given names |
||
| 125 | * @throws ResponseException |
||
| 126 | */ |
||
| 127 | 5 | public function resetIndexType($indexName, $typeName) |
|
| 128 | { |
||
| 129 | 5 | $typeConfig = $this->configManager->getTypeConfiguration($indexName, $typeName); |
|
| 130 | |||
| 131 | 4 | $this->resetIndex($indexName, true); |
|
| 132 | |||
| 133 | 2 | $index = $this->indexManager->getIndex($indexName); |
|
| 134 | 2 | $type = $index->getType($typeName); |
|
| 135 | |||
| 136 | 2 | $event = new TypeResetEvent($indexName, $typeName); |
|
| 137 | 2 | $this->dispatcher->dispatch(TypeResetEvent::PRE_TYPE_RESET, $event); |
|
| 138 | |||
| 139 | 2 | $mapping = new Mapping(); |
|
| 140 | 2 | foreach ($this->mappingBuilder->buildTypeMapping($typeConfig) as $name => $field) { |
|
| 141 | $mapping->setParam($name, $field); |
||
| 142 | } |
||
| 143 | |||
| 144 | 2 | $type->setMapping($mapping); |
|
| 145 | |||
| 146 | 2 | $this->dispatcher->dispatch(TypeResetEvent::POST_TYPE_RESET, $event); |
|
| 147 | 2 | } |
|
| 148 | |||
| 149 | /** |
||
| 150 | * A command run when a population has finished. |
||
| 151 | * |
||
| 152 | * @param string $indexName |
||
| 153 | * |
||
| 154 | * @deprecated |
||
| 155 | */ |
||
| 156 | public function postPopulate($indexName) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Switching aliases. |
||
| 163 | * |
||
| 164 | * @param string $indexName |
||
| 165 | * @param bool $delete Delete or close index |
||
| 166 | * |
||
| 167 | * @throws \FOS\ElasticaBundle\Exception\AliasIsIndexException |
||
| 168 | */ |
||
| 169 | 2 | public function switchIndexAlias($indexName, $delete = true) |
|
| 178 | } |
||
| 179 |