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

CachesController::index()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 2
dl 0
loc 30
rs 9.44
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 Doctrine\ORM\EntityManagerInterface;
9
use Form\CachesFormType;
10
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpFoundation\Response;
13
use Symfony\Component\Routing\Annotation\Route;
14
15
class CachesController extends AbstractController
16
{
17
    /**
18
     * @Route("/caches", name="caches_index")
19
     */
20
    public function index(Connection $connection, Request $request): Response
21
    {
22
        // declare variable to avoid render-error when $request is empty // there is surely a better method..
23
        $fetched_caches = '0';
24
25
        // create input field for caches_by_searchfield
26
        $form = $this->createForm(CachesFormType::class);
27
28
        // see: https://symfonycasts.com/screencast/symfony-forms/form-submit
29
        // handles the request (submit-button of the form), but only if there is a POST request
30
        $form->handleRequest($request);
31
        // if is true only if there is a request submitted and it is valid
32
        if ($form->isSubmitted() && $form->isValid()) {
33
            // read content of form input field
34
            $input_data = $form->getData();
35
36
            // send request to DB
37
            $fetched_caches = $this->search_by_searchfield($connection, $input_data["content_caches_searchfield"]);
38
39
//            dd($input_data["content_caches_searchfield"]);
40
//            dd($fetched_caches);
41
//            die();
42
        }
43
44
        // load all caches from database and hand over to twig page
45
        // just for initial test to learn how it works.. leave it here for later check up
46
        // $caches = $connection->fetchAll('SELECT * FROM caches');
47
48
        return $this->render('backend/caches/index.html.twig', ['cachesForm' => $form->createView(), 'caches_by_searchfield' => $fetched_caches]);
49
    }
50
51
    /**
52
     * @Route("/caches/caches_by_searchfield", name="create_form_caches_by_searchfield")
53
     */
54
    public function create_form_caches_by_searchfield(EntityManagerInterface $em)
0 ignored issues
show
Unused Code introduced by
The parameter $em is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
55
    {
56
        $form = $this->createForm(CachesFormType::class);
57
58
        return $this->render('backend/caches/index.html.twig', ['cachesForm' => $form->createView()]);
59
    }
60
61
    /**
62
     * @Route("/cache/{wp_oc}", name="cache_by_wp_oc")
63
     */
64
    public function search_by_cache_id(Connection $connection, string $wp_oc): Response
65
    {
66
        $wp_oc = strtoupper($wp_oc);
67
68
        // search in database for wp_oc with the given {wp_oc}
69
        $fetched_cache = $connection->fetchAll('SELECT * FROM caches WHERE wp_oc = "' . $wp_oc . '"');
70
71
        // if search for wp_oc gave no result, search for wp_gc
72
        if (!$fetched_cache) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $fetched_cache of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
73
            $fetched_cache = $connection->fetchAll('SELECT * FROM caches WHERE wp_gc = "' . $wp_oc . '"');
74
75
            // if search for wp_gc also gave no result, search for wp_nc
76
            if (!$fetched_cache)
0 ignored issues
show
Bug Best Practice introduced by
The expression $fetched_cache of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
77
                $fetched_cache = $connection->fetchAll('SELECT * FROM caches WHERE wp_nc = "' . $wp_oc . '"');
78
        }
79
80
//        if (!$fetched_cache)
81
//            print_r("nope, nix da");
82
//        else
83
//            print_r($fetched_cache);
84
//        die();
85
86
        return $this->render('backend/caches/index.html.twig', ['cache_by_id' => $fetched_cache]);
87
    }
88
89
//    /**
90
//     * @Route("/caches/list", name="caches_by_searchfield", methods="POST")
91
//     */
92
    function search_by_searchfield(Connection $connection, string $searchtext)
93
    {
94
95
        if ($searchtext != "") {
96
            $sql_string = '
97
            SELECT * FROM caches
98
            WHERE wp_oc = "' . $searchtext . '"
99
            OR wp_gc = "' . $searchtext . '"
100
            OR wp_nc = "' . $searchtext . '"
101
            OR name LIKE "%' . $searchtext . '%"
102
            ';
103
104
            $fetched_caches = $connection->fetchAll($sql_string);
105
        }
106
107
//        print_r("searchtext: " . $searchtext . " /// ");
108
//        print_r("sql_string: " . $sql_string . " /// ");
109
//        print_r("fetched_caches: ");
110
//        dd($fetched_caches);
111
//        die();
112
113
        return $fetched_caches;
0 ignored issues
show
Bug introduced by
The variable $fetched_caches does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
114
115
    }
116
}
117