Completed
Pull Request — development (#820)
by
unknown
05:16
created

CachesController::index()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 1
dl 0
loc 34
rs 9.376
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\CachesFormType;
9
use Oc\Repository\CachesRepository;
10
use Oc\Repository\Exception\RecordNotFoundException;
11
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
use Symfony\Component\Routing\Annotation\Route;
15
16
/**
17
 * Class CachesController
18
 *
19
 * @package Oc\Controller\Backend
20
 */
21
class CachesController extends AbstractController
22
{
23
    private $connection;
24
25
    private $cachesRepository;
26
27
    /**
28
     * CachesController constructor.
29
     *
30
     * @param Connection $connection
31
     * @param CachesRepository $cachesRepository
32
     */
33
    public function __construct(Connection $connection, CachesRepository $cachesRepository)
34
    {
35
        $this->connection = $connection;
36
        $this->cachesRepository = $cachesRepository;
37
    }
38
39
    /**
40
     * @param Request $request
41
     * @Route("/caches", name="caches_index")
42
     *
43
     * @return Response
44
     */
45 View Code Duplication
    public function cachesController_index(Request $request)
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...
46
    : Response {
47
        $fetchedCaches = '';
48
49
        // create input field for caches_by_searchfield
50
        $form = $this->createForm(CachesFormType::class);
51
52
        // see: https://symfonycasts.com/screencast/symfony-forms/form-submit
53
        // handles the request (submit-button of the form), but only if there is a POST request
54
        $form->handleRequest($request);
55
        // if is true only if there is a request submitted and it is valid
56
        if ($form->isSubmitted() && $form->isValid()) {
57
            // read content of form input field
58
            $inputData = $form->getData();
59
60
            // send request to DB
61
            $fetchedCaches = $this->getCachesForSearchField($inputData['content_caches_searchfield']);
62
        }
63
64
        return $this->render(
65
            'backend/caches/basicview.html.twig', [
66
                                                    'cachesForm' => $form->createView(),
67
                                                    'caches_by_searchfield' => $fetchedCaches
68
                                                ]
69
        );
70
    }
71
72
    /**
73
     * @param string $wpID
74
     *
75
     * @return Response
76
     * @Route("/cache/{wpID}", name="cache_by_wp_oc_gc")
77
     */
78
    public function search_by_cache_wp(string $wpID)
79
    : Response {
80
        $fetchedCaches = [];
81
82
        try {
83
            $fetchedCaches = $this->getCacheDetailsByWayPoint($wpID);
84
        } catch (\Exception $e) {
85
            //  tue was.. (status_not_found = true);
86
        }
87
88
        return $this->render('backend/caches/detailview.html.twig', ['cache_by_id' => $fetchedCaches]); //+ status_not_found + abfragen in twig, Z.B.
89
    }
90
91
    /**
92
     * @param string $searchtext
93
     *
94
     * @return array
95
     */
96
    public function getCachesForSearchField(string $searchtext)
97
    : array {
98
        //      so sieht die SQL-Vorlage aus..
99
        //        SELECT cache_id, name, wp_oc, user.username
100
        //        FROM caches
101
        //        INNER JOIN user ON caches.user_id = user.user_id
102
        //        WHERE wp_oc         =       "' . $searchtext . '"
103
        //        OR wp_gc            =       "' . $searchtext . '"
104
        //        OR caches.name     LIKE    "%' . $searchtext . '%"'
105
        //        OR user.username   LIKE    "%' . $searchtext . '%"'
106
        $qb = $this->connection->createQueryBuilder();
107
        $qb->select('caches.cache_id', 'caches.name', 'caches.wp_oc', 'caches.wp_gc', 'user.username')
108
            ->from('caches')
109
            ->innerJoin('caches', 'user', 'user', 'caches.user_id = user.user_id')
110
            ->where('caches.wp_oc = :searchTerm')
111
            ->orWhere('caches.wp_gc = :searchTerm')
112
            ->orWhere('caches.name LIKE :searchTermLIKE')
113
            ->orWhere('user.username LIKE :searchTermLIKE')
114
            ->setParameters(['searchTerm' => $searchtext, 'searchTermLIKE' => '%' . $searchtext . '%'])
115
            ->orderBy('caches.wp_oc', 'ASC');
116
117
        return $qb->execute()->fetchAll();
118
    }
119
120
    /**
121
     * @param int $id
122
     *
123
     * @return array
124
     * @throws RecordNotFoundException
125
     */
126
    public function getCacheDetailsById(int $id)
127
    : array {
128
        $fetchedCache = $this->cachesRepository->fetchOneBy(['cache_id' => $id]);
129
130
        return [$this->cachesRepository->getDatabaseArrayFromEntity($fetchedCache)];
131
    }
132
133
    /**
134
     * @param string $wayPoint
135
     *
136
     * @return array
137
     * @throws RecordNotFoundException
138
     */
139
    public function getCacheDetailsByWayPoint(string $wayPoint)
140
    : array {
141
        $fetchedCache = $this->cachesRepository->fetchOneBy(['wp_oc' => $wayPoint]);
142
143
        return [$this->cachesRepository->getDatabaseArrayFromEntity($fetchedCache)];
144
    }
145
}
146