Completed
Push — master ( ee47e4...7f5a24 )
by Mark
13s queued 11s
created

QueryController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 78
Duplicated Lines 10.26 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 6
dl 8
loc 78
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 3 1
A get() 0 4 1
A clear() 0 4 1
A exec() 4 11 2
A explain() 4 10 2
A serverInfo() 0 4 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
namespace Socialblue\LaravelQueryAdviser\Http\Controllers;
4
5
use Illuminate\Routing\Controller;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Facades\Cache;
8
use Illuminate\Support\Facades\DB;
9
use Socialblue\LaravelQueryAdviser\Helper\QueryBuilderHelper;
10
11
/**
12
 * Class QueryController
13
 * @package Socialblue\LaravelQueryAdviser\Http\Controllers
14
 */
15
class QueryController extends Controller
16
{
17
    /**
18
     * Show view
19
     *
20
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
21
     */
22
    public function index() {
23
        return view('QueryAdviser::index');
24
    }
25
26
    /**
27
     * Get from cache
28
     *
29
     *
30
     * @param Request $request
31
     * @return array
32
     */
33
    public function get(Request $request): array
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
34
    {
35
        return Cache::get(config('laravel-query-adviser.cache.key'), []);
36
    }
37
38
    /**
39
     * Clears cache
40
     * @return array
41
     */
42
    public function clear():array
43
    {
44
        return ['success' => Cache::forget(config('laravel-query-adviser.cache.key'))];
45
    }
46
47
    /**
48
     * Execute query
49
     *
50
     *
51
     * @param Request $request
52
     * @return array
53
     */
54
    public function exec(Request $request): array
55
    {
56
        $data = Cache::get(config('laravel-query-adviser.cache.key'), []);
57
58 View Code Duplication
        if (isset($data[$request->get('time')][$request->get('time-key')])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
59
            $query = $data[$request->get('time')][$request->get('time-key')];
60
            return DB::connection()->select($query['sql'], $query['bindings']);
61
        }
62
63
        return [];
64
    }
65
66
    /**
67
     * Use explain for query
68
     *
69
     *
70
     * @param Request $request
71
     * @return array
72
     */
73
    public function explain(Request $request): array
74
    {
75
        $data = Cache::get(config('laravel-query-adviser.cache.key'), []);
76 View Code Duplication
        if (isset($data[$request->get('time')][$request->get('time-key')])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
77
            $query = $data[$request->get('time')][$request->get('time-key')];
78
            return QueryBuilderHelper::analyze($query['sql'], $query['bindings']);
79
        }
80
81
        return ['queryParts' => "", 'query' => "", 'optimized' => ""];
82
    }
83
84
    /**
85
     *
86
     * @return array
87
     */
88
    public function serverInfo(): array
89
    {
90
        return QueryBuilderHelper::getServerInfo();
91
    }
92
}
93