Completed
Pull Request — development (#829)
by
unknown
05:04
created

SupportController   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 374
Duplicated Lines 11.76 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 0
Metric Value
dl 44
loc 374
rs 10
c 0
b 0
f 0
wmc 23
lcom 1
cbo 12

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 1
A index() 0 5 1
A searchCachesAndUser() 0 25 4
A listReportedCaches() 0 14 1
B listDbQueries() 0 37 7
A list_reported_cache_details() 0 19 1
A list_cache_history() 0 27 1
A getCachesForSearchField() 0 32 2
A getReportedCaches() 0 5 1
A executeSQL_caches_old_reg_date() 21 21 1
A executeSQL_old_reg_date() 0 19 1
A executeSQL_caches_old_login_date() 23 23 1
A executeSQL_flexible() 0 8 1

How to fix   Duplicated Code   

Duplicated Code

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

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.

Loading history...
311
    : Response
312
    {
313
        $formSearch = $this->createForm(SupportSearchCaches::class);
314
315
        $qb = $this->connection->createQueryBuilder();
316
        $qb->select('caches.name', 'user.username', 'user.date_created', 'user.last_login')
317
            ->from('caches')
318
            ->innerJoin('caches', 'user', 'user', 'caches.user_id = user.user_id')
319
            ->where('user.date_created > now() - interval :searchTerm DAY')
320
            ->andWhere('caches.user_id = user.user_id')
321
            ->setParameters(['searchTerm' => $days])
322
            ->orderBy('date_created', 'DESC');
323
324
        return $this->render(
325
            'backend/support/databaseQueries.html.twig', [
326
                                                           'supportCachesForm' => $formSearch->createView(),
327
                                                           'suppSQLquery1' => $qb->execute()->fetchAll()
328
                                                       ]
329
        );
330
    }
331
332
    /**
333
     * @param int $days
334
     *
335
     * @return Response
336
     * @Route("/dbQueries2/{days}", name="support_db_queries_2")
337
     */
338
    public function executeSQL_old_reg_date(int $days) // List user whose registration date is no older than x days.
339
    : Response
340
    {
341
        $formSearch = $this->createForm(SupportSearchCaches::class);
342
343
        $qb = $this->connection->createQueryBuilder();
344
        $qb->select('username', 'date_created', 'last_login')
345
            ->from('user')
346
            ->where('date_created > now() - interval :searchTerm DAY')
347
            ->setParameters(['searchTerm' => $days])
348
            ->orderBy('date_created', 'DESC');
349
350
        return $this->render(
351
            'backend/support/databaseQueries.html.twig', [
352
                                                           'supportCachesForm' => $formSearch->createView(),
353
                                                           'suppSQLquery2' => $qb->execute()->fetchAll()
354
                                                       ]
355
        );
356
    }
357
358
    /**
359
     * @return Response
360
     * @Route("/dbQueries4", name="support_db_queries_4")
361
     */
362 View Code Duplication
    public function executeSQL_caches_old_login_date(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
363
    ) // List (non-archived, non-locked) caches from users whose last login date is older than one year.
364
    : Response
365
    {
366
        $formSearch = $this->createForm(SupportSearchCaches::class);
367
368
        $qb = $this->connection->createQueryBuilder();
369
        $qb->select('caches.name', 'caches.cache_id', 'caches.status', 'user.username', 'user.last_login')
370
            ->from('caches')
371
            ->innerJoin('caches', 'user', 'user', 'caches.user_id = user.user_id')
372
            ->where('user.last_login < now() - interval :searchTerm YEAR')
373
            ->andWhere('caches.status <= 2')
374
            ->andWhere(('caches.user_id = user.user_id'))
375
            ->setParameters(['searchTerm' => 1])
376
            ->orderBy('user.last_login', 'ASC');
377
378
        return $this->render(
379
            'backend/support/databaseQueries.html.twig', [
380
                                                           'supportCachesForm' => $formSearch->createView(),
381
                                                           'suppSQLquery4' => $qb->execute()->fetchAll()
382
                                                       ]
383
        );
384
    }
385
386
    /**
387
     * @param string $what
388
     * @param string $table
389
     *
390
     * @return array
391
     */
392
    public function executeSQL_flexible(string $what, string $table)
393
    : array {
394
        $qb = $this->connection->createQueryBuilder();
395
        $qb->select($what)
396
            ->from($table);
397
398
        return ($qb->execute()->fetchAll());
399
    }
400
}
401