| Total Complexity | 19 |
| Total Lines | 262 |
| Duplicated Lines | 29.01 % |
| Changes | 0 | ||
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 |
||
| 41 | class SettingsController extends Controller { |
||
| 42 | |||
| 43 | /** @var string */ |
||
| 44 | private $userId; |
||
| 45 | |||
| 46 | /** @var ConfigService */ |
||
| 47 | private $configService; |
||
| 48 | |||
| 49 | /** @var TemplatesService */ |
||
| 50 | private $templatesService; |
||
| 51 | |||
| 52 | /** @var ThemesService */ |
||
| 53 | private $themesService; |
||
| 54 | |||
| 55 | /** @var WebsitesService */ |
||
| 56 | private $websitesService; |
||
| 57 | |||
| 58 | /** @var MiscService */ |
||
| 59 | private $miscService; |
||
| 60 | |||
| 61 | |||
| 62 | /** |
||
| 63 | * SettingsController constructor. |
||
| 64 | * |
||
| 65 | * @param IRequest $request |
||
| 66 | * @param string $userId |
||
| 67 | * @param ConfigService $configService |
||
| 68 | * @param TemplatesService $templatesService |
||
| 69 | * @param ThemesService $themesService |
||
| 70 | * @param WebsitesService $websitesService |
||
| 71 | * @param MiscService $miscService |
||
| 72 | */ |
||
| 73 | function __construct( |
||
| 74 | IRequest $request, $userId, ConfigService $configService, TemplatesService $templatesService, |
||
| 75 | ThemesService $themesService, WebsitesService $websitesService, |
||
| 76 | MiscService $miscService |
||
| 77 | ) { |
||
| 78 | parent::__construct(Application::APP_NAME, $request); |
||
| 79 | $this->userId = $userId; |
||
| 80 | $this->configService = $configService; |
||
| 81 | $this->templatesService = $templatesService; |
||
| 82 | $this->themesService = $themesService; |
||
| 83 | $this->websitesService = $websitesService; |
||
| 84 | $this->miscService = $miscService; |
||
| 85 | } |
||
| 86 | |||
| 87 | |||
| 88 | /** |
||
| 89 | * @NoAdminRequired |
||
| 90 | * |
||
| 91 | * @param array $data |
||
| 92 | * |
||
| 93 | * @return DataResponse |
||
| 94 | */ |
||
| 95 | public function createPersonalWebsite($data) { |
||
| 96 | |||
| 97 | try { |
||
| 98 | $this->websitesService->createWebsite( |
||
| 99 | $data['name'], $this->userId, $data['website'], $data['path'], $data['template'] |
||
| 100 | ); |
||
| 101 | |||
| 102 | return $this->miscService->success( |
||
| 103 | [ |
||
| 104 | 'name' => $data['name'], |
||
| 105 | 'websites' => $this->websitesService->getWebsitesFromUser($this->userId) |
||
| 106 | ] |
||
| 107 | ); |
||
| 108 | } catch (Exception $e) { |
||
| 109 | return $this->miscService->fail(['name' => $data['name'], 'error' => $e->getMessage()]); |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | |||
| 114 | /** |
||
| 115 | * @NoAdminRequired |
||
| 116 | * |
||
| 117 | * @param $data |
||
| 118 | * |
||
| 119 | * @return DataResponse |
||
| 120 | */ |
||
| 121 | public function removePersonalWebsite($data) { |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | |||
| 138 | /** |
||
| 139 | * @NoAdminRequired |
||
| 140 | * |
||
| 141 | * @param int $siteId |
||
| 142 | * @param string $theme |
||
| 143 | * |
||
| 144 | * @return DataResponse |
||
| 145 | */ |
||
| 146 | View Code Duplication | public function updateWebsiteTheme($siteId, $theme) { |
|
|
1 ignored issue
–
show
|
|||
| 147 | |||
| 148 | try { |
||
| 149 | $website = $this->websitesService->getWebsiteFromId($siteId); |
||
| 150 | |||
| 151 | $website->hasToBeOwnedBy($this->userId); |
||
| 152 | $website->setTheme((string)$theme); |
||
| 153 | |||
| 154 | $this->themesService->hasToBeAValidTheme($theme); |
||
| 155 | $this->websitesService->updateWebsite($website); |
||
| 156 | |||
| 157 | return $this->miscService->success( |
||
| 158 | ['websites' => $this->websitesService->getWebsitesFromUser($this->userId)] |
||
| 159 | ); |
||
| 160 | } catch (Exception $e) { |
||
| 161 | return $this->miscService->fail(['error' => $e->getMessage()]); |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | |||
| 166 | /** |
||
| 167 | * @NoAdminRequired |
||
| 168 | * |
||
| 169 | * @param int $siteId |
||
| 170 | * @param string $option |
||
| 171 | * @param string $value |
||
| 172 | * |
||
| 173 | * @return DataResponse |
||
| 174 | */ |
||
| 175 | View Code Duplication | public function editPersonalWebsiteOption($siteId, $option, $value) { |
|
|
1 ignored issue
–
show
|
|||
| 176 | |||
| 177 | try { |
||
| 178 | $website = $this->websitesService->getWebsiteFromId($siteId); |
||
| 179 | |||
| 180 | $website->hasToBeOwnedBy($this->userId); |
||
| 181 | $website->setOption((string)$option, (string)$value); |
||
| 182 | |||
| 183 | $this->websitesService->updateWebsite($website); |
||
| 184 | |||
| 185 | return $this->miscService->success( |
||
| 186 | ['websites' => $this->websitesService->getWebsitesFromUser($this->userId)] |
||
| 187 | ); |
||
| 188 | } catch (Exception $e) { |
||
| 189 | return $this->miscService->fail(['error' => $e->getMessage()]); |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | |||
| 194 | /** |
||
| 195 | * @NoAdminRequired |
||
| 196 | * |
||
| 197 | * @return DataResponse |
||
| 198 | */ |
||
| 199 | public function getPersonalWebsites() { |
||
| 200 | try { |
||
| 201 | $websites = $this->websitesService->getWebsitesFromUser($this->userId); |
||
| 202 | |||
| 203 | return $this->miscService->success( |
||
| 204 | [ |
||
| 205 | 'themes' => $this->themesService->getThemesList(), |
||
| 206 | 'websites' => $websites |
||
| 207 | ] |
||
| 208 | ); |
||
| 209 | } catch (Exception $e) { |
||
| 210 | return $this->miscService->fail(['error' => $e->getMessage()]); |
||
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | |||
| 215 | /** |
||
| 216 | * @return DataResponse |
||
| 217 | */ |
||
| 218 | public function getSettingsAdmin() { |
||
| 219 | $data = [ |
||
| 220 | 'templates' => $this->templatesService->getTemplatesList(true), |
||
| 221 | 'templates_new' => $this->templatesService->getNewTemplatesList(), |
||
| 222 | 'themes' => $this->themesService->getThemesList(true), |
||
| 223 | 'themes_new' => $this->themesService->getNewThemesList() |
||
| 224 | ]; |
||
| 225 | |||
| 226 | return new DataResponse($data, Http::STATUS_OK); |
||
| 227 | } |
||
| 228 | |||
| 229 | |||
| 230 | /** |
||
| 231 | * @return DataResponse |
||
| 232 | */ |
||
| 233 | public function setSettingsAdmin() { |
||
| 234 | return $this->getSettingsAdmin(); |
||
| 235 | } |
||
| 236 | |||
| 237 | |||
| 238 | /** |
||
| 239 | * @param string $template |
||
| 240 | * |
||
| 241 | * @return DataResponse |
||
| 242 | */ |
||
| 243 | View Code Duplication | public function addCustomTemplate($template) { |
|
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @param string $template |
||
| 254 | * |
||
| 255 | * @return DataResponse |
||
| 256 | */ |
||
| 257 | View Code Duplication | public function removeCustomTemplate($template) { |
|
| 269 | } |
||
| 270 | |||
| 271 | |||
| 272 | /** |
||
| 273 | * @param string $theme |
||
| 274 | * |
||
| 275 | * @return DataResponse |
||
| 276 | */ |
||
| 277 | View Code Duplication | public function addCustomTheme($theme) { |
|
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @param string $theme |
||
| 288 | * |
||
| 289 | * @return DataResponse |
||
| 290 | */ |
||
| 291 | View Code Duplication | public function removeCustomTheme($theme) { |
|
| 303 | } |
||
| 304 | |||
| 305 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.