Completed
Pull Request — development (#827)
by
unknown
05:10
created

SupportController::executeSQL_old_reg_date()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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