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 |
||
| 45 | class InstitutionConfigurationCommandHandler extends CommandHandler |
||
| 46 | { |
||
| 47 | /** |
||
| 48 | * @var RepositoryInterface |
||
| 49 | */ |
||
| 50 | private $repository; |
||
| 51 | |||
| 52 | public function __construct(RepositoryInterface $repository) |
||
| 56 | |||
| 57 | View Code Duplication | public function handleCreateInstitutionConfigurationCommand(CreateInstitutionConfigurationCommand $command) |
|
| 58 | { |
||
| 59 | $institution = new Institution($command->institution); |
||
| 60 | $institutionConfigurationId = InstitutionConfigurationId::normalizedFrom($institution); |
||
| 61 | |||
| 62 | try { |
||
| 63 | /** @var InstitutionConfiguration $institutionConfiguration */ |
||
| 64 | $institutionConfiguration = $this->repository->load( |
||
| 65 | $institutionConfigurationId->getInstitutionConfigurationId() |
||
| 66 | ); |
||
| 67 | |||
| 68 | $institutionConfiguration->rebuild(); |
||
| 69 | |||
| 70 | } catch (AggregateNotFoundException $exception) { |
||
| 71 | $institutionConfiguration = InstitutionConfiguration::create($institutionConfigurationId, $institution); |
||
| 72 | } |
||
| 73 | |||
| 74 | $this->repository->save($institutionConfiguration); |
||
| 75 | } |
||
| 76 | |||
| 77 | public function handleReconfigureInstitutionConfigurationOptionsCommand( |
||
| 78 | ReconfigureInstitutionConfigurationOptionsCommand $command |
||
| 79 | ) { |
||
| 80 | $institution = new Institution($command->institution); |
||
| 81 | |||
| 82 | $allowedSecondFactors = array_map(function ($allowedSecondFactor) { |
||
| 83 | return new SecondFactorType($allowedSecondFactor); |
||
| 84 | }, $command->allowedSecondFactors); |
||
| 85 | |||
| 86 | |||
| 87 | $institutionConfiguration = $this->loadInstitutionConfigurationFor($institution); |
||
| 88 | $institutionConfiguration->configureUseRaLocationsOption( |
||
| 89 | new UseRaLocationsOption($command->useRaLocationsOption) |
||
| 90 | ); |
||
| 91 | $institutionConfiguration->configureShowRaaContactInformationOption( |
||
| 92 | new ShowRaaContactInformationOption($command->showRaaContactInformationOption) |
||
| 93 | ); |
||
| 94 | $institutionConfiguration->updateAllowedSecondFactorList( |
||
| 95 | AllowedSecondFactorList::ofTypes($allowedSecondFactors) |
||
| 96 | ); |
||
| 97 | |||
| 98 | $this->repository->save($institutionConfiguration); |
||
| 99 | } |
||
| 100 | |||
| 101 | View Code Duplication | public function handleAddRaLocationCommand(AddRaLocationCommand $command) |
|
| 115 | |||
| 116 | View Code Duplication | public function handleChangeRaLocationCommand(ChangeRaLocationCommand $command) |
|
| 130 | |||
| 131 | public function handleRemoveRaLocationCommand(RemoveRaLocationCommand $command) |
||
| 140 | |||
| 141 | public function handleRemoveInstitutionConfigurationByUnnormalizedIdCommand( |
||
| 142 | RemoveInstitutionConfigurationByUnnormalizedIdCommand $command |
||
| 143 | ) { |
||
| 144 | $institution = new Institution($command->institution); |
||
| 145 | |||
| 146 | $institutionConfigurationId = InstitutionConfigurationId::from($institution); |
||
| 147 | $institutionConfiguration = $this->repository->load( |
||
| 148 | $institutionConfigurationId->getInstitutionConfigurationId() |
||
| 149 | ); |
||
| 150 | $institutionConfiguration->destroy(); |
||
| 151 | |||
| 152 | $this->repository->save($institutionConfiguration); |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @deprecated Should be used until existing institution configurations have been migrated to using normalized ids |
||
| 157 | * |
||
| 158 | * @param Institution $institution |
||
| 159 | * @return InstitutionConfiguration |
||
| 160 | */ |
||
| 161 | View Code Duplication | private function loadInstitutionConfigurationFor(Institution $institution) |
|
| 177 | } |
||
| 178 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.