Completed
Push — master ( 304790...f6d194 )
by
unknown
15s
created

ApiManagerController::index()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 16
rs 9.4285
cc 2
eloc 8
nc 2
nop 1
1
<?php
2
3
/**
4
 * @Author: ahmadnorin
5
 * @Date:   2017-11-28 00:12:29
6
 * @Last Modified by:   ahmadnorin
7
 * @Last Modified time: 2017-11-28 09:47:54
8
 */
9
10
namespace App\Http\Controllers;
11
12
use Illuminate\Http\Request;
13
use App\ApiManager;
14
use Validator, Session, Redirect;
15
16
class ApiManagerController extends Controller
17
{
18
    public function index(Request $request)
19
    {
20
    	if($request->get('search') != '')
21
    	{
22
    	$data['data']		= ApiManager::where('client', 'like', '%'.$request->get('search').'%')
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
23
    						->orderBy('id', 'desc')
24
    						->paginate(env('PAGINATE', 10));
25
26
    	}
27
    	else
28
    	{
29
    		$data['data']		= ApiManager::orderBy('id', 'desc')->paginate(env('PAGINATE', 10));
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
30
31
    	}
32
    	return view('api_manager.index', $data);
33
    }
34
35
    public function create()
36
    {
37
    	return view('api_manager.create');
38
    }
39
40
    public function store(Request $request)
41
    {
42
    	$validator = Validator::make($request->all(), [
43
    		'client'			=> 'required|unique:api_manager,client',
44
    		'description'		=> 'required',
45
    		]);
46
    	if($validator->fails())
47
    	{
48
    		Session::flash('message', 'Please fix the error(s) below');
49
            return redirect()->back()
50
                ->withErrors($validator)
51
                ->withInput();
52
    	}
53
54
      $token 		= $this->token();
55
    	$api = New ApiManager;
56
    	$api->client 			= str_replace(array('https://', 'http://'), array('',''),$request->input('client'));
57
      $api->api_keys 		= $token;
58
      $api->api_token 	= $token;
59
    	// $api->api_keys 			= $this->token();
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
60
    	$api->description 		= $request->input('description');
61
        $api->user_id           = 1;
62
    	$api->save();
63
    	Session::flash('message', 'Api Keys Data Saved Successfuly');
64
    	return Redirect::to('api_manager');
65
    }
66
67
    public function edit(Request $request, $id)
68
    {
69
    	$data['data']	= ApiManager::findOrFail($id);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
70
    	return view('api_manager.edit', $data);
71
    }
72
73
    public function update(Request $request, $id)
74
    {
75
    	$validator = Validator::make($request->all(), [
76
    		'client'			=> 'required|unique:api_manager,client,'.$id,
77
    		'description'		=> 'required',
78
    		]);
79
    	if($validator->fails())
80
    	{
81
    		Session::flash('message', 'Please fix the error(s) below');
82
            return redirect()->back()
83
                ->withErrors($validator)
84
                ->withInput();
85
    	}
86
87
    	$api = ApiManager::findOrFail($id);
88
        $api->client            = str_replace(array('https://', 'http://'),array('',''),$request->input('client'));
89
    	$api->description 		= $request->input('description');
90
    	$api->save();
91
    	Session::flash('message', 'Api Keys Data Update Successfuly');
92
    	return Redirect::to('api_manager');
93
    }
94
95
    public function token()
96
    {
97
    	    $length = 100;
98
		    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
99
		    $charactersLength = strlen($characters);
100
		    $randomString = '';
101
		    for ($i = 0; $i < $length; $i++) {
102
		        $randomString .= $characters[rand(0, $charactersLength - 1)];
103
		    }
104
		    return $randomString;
105
    }
106
}
107