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 |
||
| 23 | /** |
||
| 24 | * Class SupportController |
||
| 25 | * |
||
| 26 | * @package Oc\Controller\Backend |
||
| 27 | */ |
||
| 28 | class SupportController extends AbstractController |
||
| 29 | { |
||
| 30 | /** @var Connection */ |
||
| 31 | private Connection $connection; |
||
|
|
|||
| 32 | |||
| 33 | /** @var CacheAdoptionsRepository */ |
||
| 34 | private CacheAdoptionsRepository $cacheAdoptionsRepository; |
||
| 35 | |||
| 36 | /** @var CacheCoordinatesRepository */ |
||
| 37 | private CacheCoordinatesRepository $cacheCoordinatesRepository; |
||
| 38 | |||
| 39 | /** @var CacheLogsArchivedRepository */ |
||
| 40 | private CacheLogsArchivedRepository $cacheLogsArchivedRepository; |
||
| 41 | |||
| 42 | /** @var CachesRepository */ |
||
| 43 | private CachesRepository $cachesRepository; |
||
| 44 | |||
| 45 | /** @var CacheReportsRepository */ |
||
| 46 | private CacheReportsRepository $cacheReportsRepository; |
||
| 47 | |||
| 48 | /** @var CacheStatusModifiedRepository */ |
||
| 49 | private CacheStatusModifiedRepository $cacheStatusModifiedRepository; |
||
| 50 | |||
| 51 | /** @var CacheStatusRepository */ |
||
| 52 | private CacheStatusRepository $cacheStatusRepository; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * SupportController constructor. |
||
| 56 | * |
||
| 57 | * @param Connection $connection |
||
| 58 | * @param CacheAdoptionsRepository $cacheAdoptionsRepository |
||
| 59 | * @param CacheCoordinatesRepository $cacheCoordinatesRepository |
||
| 60 | * @param CacheLogsArchivedRepository $cacheLogsArchivedRepository |
||
| 61 | * @param CachesRepository $cachesRepository |
||
| 62 | * @param CacheReportsRepository $cacheReportsRepository |
||
| 63 | * @param CacheStatusModifiedRepository $cacheStatusModifiedRepository |
||
| 64 | * @param CacheStatusRepository $cacheStatusRepository |
||
| 65 | */ |
||
| 66 | public function __construct( |
||
| 67 | Connection $connection, |
||
| 68 | CacheAdoptionsRepository $cacheAdoptionsRepository, |
||
| 69 | CacheCoordinatesRepository $cacheCoordinatesRepository, |
||
| 70 | CacheLogsArchivedRepository $cacheLogsArchivedRepository, |
||
| 71 | CachesRepository $cachesRepository, |
||
| 72 | CacheReportsRepository $cacheReportsRepository, |
||
| 73 | CacheStatusModifiedRepository $cacheStatusModifiedRepository, |
||
| 74 | CacheStatusRepository $cacheStatusRepository |
||
| 75 | ) { |
||
| 76 | $this->connection = $connection; |
||
| 77 | $this->cacheAdoptionsRepository = $cacheAdoptionsRepository; |
||
| 78 | $this->cacheCoordinatesRepository = $cacheCoordinatesRepository; |
||
| 79 | $this->cacheLogsArchivedRepository = $cacheLogsArchivedRepository; |
||
| 80 | $this->cachesRepository = $cachesRepository; |
||
| 81 | $this->cacheReportsRepository = $cacheReportsRepository; |
||
| 82 | $this->cacheStatusModifiedRepository = $cacheStatusModifiedRepository; |
||
| 83 | $this->cacheStatusRepository = $cacheStatusRepository; |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @return Response |
||
| 88 | * @Route("/support", name="support_index") |
||
| 89 | */ |
||
| 90 | public function index() |
||
| 91 | : Response |
||
| 92 | { |
||
| 93 | return $this->render('backend/support/index.html.twig'); |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param Request $request |
||
| 98 | * |
||
| 99 | * @return Response |
||
| 100 | * @Route("/supportSearch", name="support_search") |
||
| 101 | */ |
||
| 102 | public function searchCachesAndUser(Request $request) |
||
| 103 | : Response { |
||
| 104 | $fetchedCaches = ''; |
||
| 105 | $limit = false; |
||
| 106 | |||
| 107 | $formSearch = $this->createForm(SupportSearchCaches::class); |
||
| 108 | |||
| 109 | $formSearch->handleRequest($request); |
||
| 110 | if ($formSearch->isSubmitted() && $formSearch->isValid()) { |
||
| 111 | $inputData = $formSearch->getData(); |
||
| 112 | |||
| 113 | if ($formSearch->getClickedButton() === $formSearch->get('search_One')) { |
||
| 114 | $limit = true; |
||
| 115 | } |
||
| 116 | |||
| 117 | $fetchedCaches = $this->getCachesForSearchField($inputData['content_support_searchfield'], $limit); |
||
| 118 | } |
||
| 119 | |||
| 120 | return $this->render( |
||
| 121 | 'backend/support/searchedCaches.html.twig', [ |
||
| 122 | 'supportCachesForm' => $formSearch->createView(), |
||
| 123 | 'foundCaches' => $fetchedCaches |
||
| 124 | ] |
||
| 125 | ); |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @return Response |
||
| 130 | * @throws \Oc\Repository\Exception\RecordsNotFoundException |
||
| 131 | * @Route("/reportedCaches", name="support_reported_caches") |
||
| 132 | */ |
||
| 133 | public function listReportedCaches() |
||
| 134 | : Response |
||
| 135 | { |
||
| 136 | $fetchedReports = $this->getReportedCaches(); |
||
| 137 | |||
| 138 | $formSearch = $this->createForm(SupportSearchCaches::class); |
||
| 139 | |||
| 140 | return $this->render( |
||
| 141 | 'backend/support/reportedCaches.html.twig', [ |
||
| 142 | 'supportCachesForm' => $formSearch->createView(), |
||
| 143 | 'reportedCaches_by_id' => $fetchedReports |
||
| 144 | ] |
||
| 145 | ); |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param Request $request |
||
| 150 | * |
||
| 151 | * @return Response |
||
| 152 | * @Route("/dbQueries", name="support_db_queries") |
||
| 153 | */ |
||
| 154 | public function listDbQueries(Request $request) |
||
| 155 | : Response { |
||
| 156 | $fetchedInformation = []; |
||
| 157 | |||
| 158 | $formSearch = $this->createForm(SupportSearchCaches::class); |
||
| 159 | |||
| 160 | $form = $this->createForm(SupportSQLFlexForm::class); |
||
| 161 | |||
| 162 | $form->handleRequest($request); |
||
| 163 | |||
| 164 | if ($form->isSubmitted() && $form->isValid()) { |
||
| 165 | $inputData = $form->getData(); |
||
| 166 | |||
| 167 | $fetchedInformation = $this->executeSQL_flexible($inputData['content_WHAT'], $inputData['content_TABLE']); |
||
| 168 | |||
| 169 | $countFetched = count($fetchedInformation); |
||
| 170 | for ($i = 0; $i < $countFetched; $i ++) { |
||
| 171 | if (array_key_exists('password', $fetchedInformation[$i])) { |
||
| 172 | $fetchedInformation[$i]['password'] = '-'; |
||
| 173 | } |
||
| 174 | if (array_key_exists('logpw', $fetchedInformation[$i])) { |
||
| 175 | $fetchedInformation[$i]['logpw'] = '-'; |
||
| 176 | } |
||
| 177 | if (array_key_exists('admin_password', $fetchedInformation[$i])) { |
||
| 178 | $fetchedInformation[$i]['admin_password'] = '-'; |
||
| 179 | } |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | return $this->render( |
||
| 184 | 'backend/support/databaseQueries.html.twig', [ |
||
| 185 | 'supportCachesForm' => $formSearch->createView(), |
||
| 186 | 'SQLFlexForm' => $form->createView(), |
||
| 187 | 'suppSQLqueryFlex' => $fetchedInformation |
||
| 188 | ] |
||
| 189 | ); |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @param int $repID |
||
| 194 | * |
||
| 195 | * @return Response |
||
| 196 | * @throws \Oc\Repository\Exception\RecordNotFoundException |
||
| 197 | * @throws \Oc\Repository\Exception\RecordsNotFoundException |
||
| 198 | * @Route("/repCaches/{repID}", name="support_reported_cache") |
||
| 199 | */ |
||
| 200 | public function list_reported_cache_details(int $repID) |
||
| 201 | : Response { |
||
| 202 | $formSearch = $this->createForm(SupportSearchCaches::class); |
||
| 203 | $formComment = $this->createForm(SupportAdminComment::class); |
||
| 204 | |||
| 205 | $fetchedReport = $this->cacheReportsRepository->fetchOneBy(['id' => $repID]); |
||
| 206 | |||
| 207 | $fetchedStatus = $this->cacheStatusRepository->fetchAll(); |
||
| 208 | |||
| 209 | $fetchedStatusModfied = $this->cacheStatusModifiedRepository->fetchBy(['cache_id' => $fetchedReport->cacheid]); |
||
| 210 | |||
| 211 | return $this->render( |
||
| 212 | 'backend/support/reportedCacheDetails.html.twig', [ |
||
| 213 | 'supportCachesForm' => $formSearch->createView(), |
||
| 214 | 'supportAdminCommentForm' => $formComment->createView(), |
||
| 215 | 'reported_cache_by_id' => $fetchedReport, |
||
| 216 | 'cache_status' => $fetchedStatus, |
||
| 217 | 'report_status_modified' => $fetchedStatusModfied |
||
| 218 | ] |
||
| 219 | ); |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @param Request $request |
||
| 224 | * |
||
| 225 | * @return Response |
||
| 226 | * @throws \Doctrine\DBAL\DBALException |
||
| 227 | * @throws \Oc\Repository\Exception\RecordNotFoundException |
||
| 228 | * @throws \Oc\Repository\Exception\RecordNotPersistedException |
||
| 229 | * @Route("/repCachesSaveText", name="support_reported_cache_save_text") |
||
| 230 | */ |
||
| 231 | public function repCaches_saveTextArea(Request $request) |
||
| 232 | : Response { |
||
| 233 | $form = $this->createForm(SupportAdminComment::class)->handleRequest($request); |
||
| 234 | |||
| 235 | if ($form->isSubmitted() && $form->isValid()) { |
||
| 236 | $inputData = $form->getData(); |
||
| 237 | |||
| 238 | $entity = $this->cacheReportsRepository->fetchOneBy(['id' => (int) $inputData['hidden_repID']]); |
||
| 239 | $entity->comment = $inputData['support_admin_comment']; |
||
| 240 | |||
| 241 | $this->cacheReportsRepository->update($entity); |
||
| 242 | |||
| 243 | return $this->redirectToRoute('backend_support_reported_cache', ['repID' => $entity->id]); |
||
| 244 | } |
||
| 245 | |||
| 246 | return $this->redirectToRoute('backend_support_reported_caches'); |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @param string $wpID |
||
| 251 | * |
||
| 252 | * @return Response |
||
| 253 | * @throws \Oc\Repository\Exception\RecordNotFoundException |
||
| 254 | * @throws \Oc\Repository\Exception\RecordsNotFoundException |
||
| 255 | * @throws \Exception |
||
| 256 | * @Route("/cacheHistory/{wpID}", name="support_cache_history") |
||
| 257 | */ |
||
| 258 | public function list_cache_history(string $wpID) |
||
| 259 | : Response { |
||
| 260 | $formSearch = $this->createForm(SupportSearchCaches::class); |
||
| 261 | |||
| 262 | $fetchedId = $this->cachesRepository->getIdByWP($wpID); |
||
| 263 | |||
| 264 | $fetchedReports = $this->cacheReportsRepository->fetchBy(['cacheid' => $fetchedId]); |
||
| 265 | |||
| 266 | $fetchedLogDeletes = $this->cacheLogsArchivedRepository->fetchBy(['cache_id' => $fetchedId]); |
||
| 267 | |||
| 268 | $fetchedStatusModfied = $this->cacheStatusModifiedRepository->fetchBy(['cache_id' => $fetchedId]); |
||
| 269 | |||
| 270 | $fetchedCoordinates = $this->cacheCoordinatesRepository->fetchBy(['cache_id' => $fetchedId]); |
||
| 271 | |||
| 272 | $fetchedAdoptions = $this->cacheAdoptionsRepository->fetchBy(['cache_id' => $fetchedId]); |
||
| 273 | |||
| 274 | return $this->render( |
||
| 275 | 'backend/support/cacheHistory.html.twig', [ |
||
| 276 | 'supportCachesForm' => $formSearch->createView(), |
||
| 277 | 'cache_reports' => $fetchedReports, |
||
| 278 | 'deleted_logs' => $fetchedLogDeletes, |
||
| 279 | 'report_status_modified' => $fetchedStatusModfied, |
||
| 280 | 'changed_coordinates' => $fetchedCoordinates, |
||
| 281 | 'cache_adoptions' => $fetchedAdoptions, |
||
| 282 | ] |
||
| 283 | ); |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @param string $searchtext |
||
| 288 | * @param bool $limit |
||
| 289 | * |
||
| 290 | * @return array |
||
| 291 | */ |
||
| 292 | public function getCachesForSearchField(string $searchtext, bool $limit = false) |
||
| 293 | : array { |
||
| 294 | // so sieht die SQL-Vorlage aus.. |
||
| 295 | // SELECT name, wp_oc, wp_gc, wp_gc_maintained, user.username, user.email |
||
| 296 | // FROM caches |
||
| 297 | // INNER JOIN user ON caches.user_id = user.user_id |
||
| 298 | // WHERE wp_oc = "' . $searchtext . '" |
||
| 299 | // OR wp_gc = "' . $searchtext . '" |
||
| 300 | // OR wp_gc_maintained = "' . $searchtext . '" |
||
| 301 | // OR caches.name LIKE "%' . $searchtext . '%"' |
||
| 302 | // OR user.username LIKE "%' . $searchtext . '%"' |
||
| 303 | // OR user.email LIKE "%' . $searchtext . '%"' |
||
| 304 | // LIMIT $limit |
||
| 305 | $qb = $this->connection->createQueryBuilder(); |
||
| 306 | $qb->select('caches.name', 'caches.wp_oc', 'caches.wp_gc', 'caches.wp_gc_maintained', 'user.username', 'user.email') |
||
| 307 | ->from('caches') |
||
| 308 | ->innerJoin('caches', 'user', 'user', 'caches.user_id = user.user_id') |
||
| 309 | ->where('caches.wp_oc = :searchTerm') |
||
| 310 | ->orWhere('caches.wp_gc = :searchTerm') |
||
| 311 | ->orWhere('caches.wp_gc_maintained = :searchTerm') |
||
| 312 | ->orWhere('caches.name LIKE :searchTermLIKE') |
||
| 313 | ->orWhere('user.username LIKE :searchTermLIKE') |
||
| 314 | ->orWhere('user.email LIKE :searchTermLIKE') |
||
| 315 | ->setParameters(['searchTerm' => $searchtext, 'searchTermLIKE' => '%' . $searchtext . '%']) |
||
| 316 | ->orderBy('caches.wp_oc', 'ASC'); |
||
| 317 | |||
| 318 | if ($limit === true) { |
||
| 319 | $qb->setMaxResults(1); |
||
| 320 | } |
||
| 321 | |||
| 322 | return $qb->execute()->fetchAll(); |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @return array |
||
| 327 | * @throws \Oc\Repository\Exception\RecordsNotFoundException |
||
| 328 | */ |
||
| 329 | public function getReportedCaches() |
||
| 330 | : array |
||
| 331 | { |
||
| 332 | return $this->cacheReportsRepository->fetchAll(); |
||
| 333 | } |
||
| 334 | |||
| 432 |