| 1 |  |  | <?php
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace App\Http\Controllers;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use App\Http\Controllers\Controller;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Http\Request;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use App\Hostkeys;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use That0n3guy\Transliteration;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use GuzzleHttp\Exception\GuzzleException;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use GuzzleHttp\Client;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use GuzzleHttp\Pool;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use GuzzleHttp\Psr7;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Auth;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Validator, Image, Session, File, Response, Redirect, Exception;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | class HostkeysController extends Controller
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |   /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |    * Display a listing of the resource.
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |    *
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |    * @return Response
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |    */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |   public function index(Request $request)
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |   {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |       try {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         if($request->get('search') != ''){
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |           $data['data']	= Hostkeys::with('getState')
 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |                                  ->with('getTransition')
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |                                  ->with('getUserName')
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |                                  ->where('hostname', 'like', '%'.$request->get('search').'%')
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |                                  ->orderBy('id', 'desc')
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |                                  ->paginate(env('PAGINATE', 10));
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         } else{
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |           $data['data']	= Hostkeys::with('getState')
 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                                  ->with('getTransition')
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                                  ->with('getUserName')
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                                  ->orderBy('id', 'desc')
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                                  ->paginate(env('PAGINATE', 10));
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |       } catch (Exception $e) {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |           $data['data']	= [];
 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |       }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |       return view('host_keys.index', $data);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |   }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |   /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |    * Show the form for creating a new resource.
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |    *
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |    * @return Response
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |    */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |   public function create()
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |   {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     return view('host_keys.create');
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |   }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |   /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |    * Store a newly created resource in storage.
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |    *
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |    * @return Response
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |    */
 | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 64 |  | View Code Duplication |   public function store(Request $request)
 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |   {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |       try {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |           $token 		= $request->keys;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |           $hostkey = New Hostkeys;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |           $hostkey->hostname 			= str_replace(array('https://', 'http://'), array('',''),$request->hostname);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |           $hostkey->keys 			= $token;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |           $hostkey->state 		= $request->state;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |           $hostkey->transition 		= $request->transition;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |           $hostkey->user_id           = $request->user_id;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |           if($request->state == 'Approved' || $request->state == 'approved' || $request->state == 'Rejected' || $request->state == 'rejected'){
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             $hostkey->save();
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             $error = false;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             $statusCode = 200;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             $title = 'Success';
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             $type = 'success';
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             $message = 'Data Saved Successfuly.';
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             $result = $hostkey;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |           }else {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |               $error = true;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |               $statusCode = 404;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |               $title = 'Error';
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |               $type = 'error';
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |               $message = 'Error';
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |               $result = 'Not Found';
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |           }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |       } catch (Exception $e) {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |           $error = true;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |           $statusCode = 404;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |           $title = 'Error';
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |           $type = 'error';
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |           $message = 'Error';
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |           $result = 'Not Found';
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |       } finally {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |           return Response::json(array(
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             'error' => $error,
 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             'status' => $statusCode,
 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             'title' => $title,
 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             'type' => $type,
 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             'message' => $message,
 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             'result' => $result
 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |           ));
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |       }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |   }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |   /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |    * Display the specified resource.
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |    *
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |    * @param  int  $id
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |    * @return Response
 | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 115 |  |  |    */
 | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |   public function show($id)
 | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |   {
 | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  | 
 | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |   }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |   public function get($hostname)
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |   {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |       try {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |           $data	= Hostkeys::with('getState')
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |                                  ->with('getTransition')
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                                  ->with('getUserName')
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                                  ->where('hostname', 'like', '%'.$hostname.'%')
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                                  ->orderBy('id', 'desc')
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                                  ->first();
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |            $error = false;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |            $statusCode = 200;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |            $title = 'Success';
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |            $type = 'success';
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |            $message = 'Success';
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |            $result = $data->hostname;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |            $resultid = $data->id;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |       } catch (Exception $e) {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |           $error = true;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |           $statusCode = 404;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |           $title = 'Error';
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |           $type = 'error';
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |           $message = 'Error';
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |           $result = 'Not Found';
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |           $resultid = 'Not Found';
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |       } finally {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |           return Response::json(array(
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             'error' => $error,
 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             'status' => $statusCode,
 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |             'title' => $title,
 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |             'type' => $type,
 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |             'message' => $message,
 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |             'result' => $result,
 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |             'id' => $resultid
 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |           ));
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |       }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |   }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |   public function request(Request $request){
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |       $validator = Validator::make($request->all(), [
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         'host'			=> 'required|unique:host_keys,hostname',
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         'description'		=> 'required',
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         ]);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |       if($validator->fails())
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |       {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         Session::flash('message', 'Please fix the error(s) below');
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |             return redirect()->back()
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |                 ->withErrors($validator)
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |                 ->withInput();
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |       }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |       if(Auth::guest()){ $current_user = 1; }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |       else{ $current_user = Auth::user()->id; }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |       $headers = ['Content-Type' => 'application/json'];
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |       $data = [
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |         'host' => $request->host,
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |         'client' => $request->client,
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |         'request' => 'Request',
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |         'deskripsi' => $request->description,
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |         'user_id' => $current_user
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |       ];
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |       $body = json_encode($data);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |       //kalo udah rilis
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |       $host 			= str_replace(array('https://', 'http://'), array('',''),$request->host);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |       $url = $host."/api/v1/api-manager/request";
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |       //untuk local
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |       // $url = "dashboard.local/api/v1/api-manager/receive";
 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |       $client = new \GuzzleHttp\Client();
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |       $res = $client->request('POST', $url,['headers'=>$headers,'body'=>$body]);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |       $response = $res->getBody();
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |       $responses = json_decode($response);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |       $hostkey = New Hostkeys;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |       $hostkey->hostname 			= $host;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |       $hostkey->keys 			= "";
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |       $hostkey->state 		= $responses->result->request;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |       $hostkey->transition 		= "Propose To ".$responses->result->request;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |       $hostkey->user_id           = $responses->result->user_id;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |       if($responses->status == 200){
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |         $hostkey->save();
 | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 203 |  | View Code Duplication |         if(env('URL_APIMANAGER') != NULL){
 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |           $url_apimanager = str_replace('"', '',env('URL_APIMANAGER'));
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |           if($url_apimanager != "" || $url_apimanager != NULL || $url_apimanager != false || !empty($url_apimanager)){
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |             $this->send_apimanager($url_apimanager,$request,$current_user,$hostkey->transition);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |           }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |         }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |         Session::flash('message', 'Send Request Api Keys Successfuly');
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |       }else {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |         Session::flash('message', $responses->message);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |       }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |       return Redirect::to('host-keys');
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |   }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |   private function send_apimanager($url_apimanager,$request,$current_user,$keterangan){
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |       $headers = ['Content-Type' => 'application/json'];
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  |       $host 			= str_replace(array('https://', 'http://'), array('',''),$request->host);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  |       $client 			= str_replace(array('https://', 'http://'), array('',''),$request->client);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  |       $data = [
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |         'host' => $host,
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |         'client' => $client,
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  |         'keterangan' => $keterangan,
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |         'user_id' => $current_user
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |       ];
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  |       $body = json_encode($data);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  |       //kalo udah rilis
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  |       $url = $url_apimanager."/api/store";
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  |       //untuk local
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  |       // $url = "bloger.local/api/v1/host-keys";
 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  |       $client = new \GuzzleHttp\Client();
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  |       $res = $client->request('POST', $url,['headers'=>$headers,'body'=>$body]);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  |       $response = $res->getBody();
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |       $responses = json_decode($response);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  |       return $responses;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  |   }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 |  |  |   /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  |    * Show the form for editing the specified resource.
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  |    *
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 |  |  |    * @param  int  $id
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  |    * @return Response
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 |  |  |    */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 |  |  |   public function edit($id)
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 |  |  |   {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  |   }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  |   /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  |    * Update the specified resource in storage.
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 |  |  |    *
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 |  |  |    * @param  int  $id
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 |  |  |    * @return Response
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 |  |  |    */
 | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 259 |  | View Code Duplication |    public function update(Request $request, $id)
 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  |    {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  |        try {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 |  |  |            $token 		= $request->keys;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  |            $hostkey = Hostkeys::findOrFail($id);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 |  |  |            $hostkey->hostname 			= str_replace(array('https://', 'http://'), array('',''),$request->hostname);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 |  |  |            $hostkey->keys 			= $token;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 |  |  |            $hostkey->state 		= $request->state;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 |  |  |            $hostkey->transition 		= $request->transition;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 |  |  |            $hostkey->user_id           = $request->user_id;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 269 |  |  |            if($request->state == 'Approved' || $request->state == 'approved' || $request->state == 'Rejected' || $request->state == 'rejected'){
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 |  |  |              $hostkey->save();
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 |  |  |              $error = false;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 273 |  |  |              $statusCode = 200;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 |  |  |              $title = 'Success';
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 |  |  |              $type = 'success';
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 |  |  |              $message = 'Data Saved Successfuly.';
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 |  |  |              $result = $hostkey;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 278 |  |  |            }else {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 279 |  |  |                $error = true;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 280 |  |  |                $statusCode = 404;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 281 |  |  |                $title = 'Error';
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 |  |  |                $type = 'error';
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 |  |  |                $message = 'Error';
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 |  |  |                $result = 'Not Found';
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 |  |  |            }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 |  |  |        } catch (Exception $e) {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 |  |  |            $error = true;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 |  |  |            $statusCode = 404;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 |  |  |            $title = 'Error';
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 290 |  |  |            $type = 'error';
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 291 |  |  |            $message = 'Error';
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 |  |  |            $result = 'Not Found';
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 293 |  |  |        } finally {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 294 |  |  |            return Response::json(array(
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 295 |  |  |              'error' => $error,
 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 296 |  |  |              'status' => $statusCode,
 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 297 |  |  |              'title' => $title,
 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 298 |  |  |              'type' => $type,
 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 299 |  |  |              'message' => $message,
 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 300 |  |  |              'result' => $result
 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 301 |  |  |            ));
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 302 |  |  |        }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 303 |  |  |    }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 304 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 305 |  |  |   /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 306 |  |  |    * Remove the specified resource from storage.
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 307 |  |  |    *
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 308 |  |  |    * @param  int  $id
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 309 |  |  |    * @return Response
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 310 |  |  |    */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 311 |  |  |   public function destroy($id)
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 312 |  |  |   {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 313 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 314 |  |  |   }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 315 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 316 |  |  | }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 317 |  |  | 
 | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 318 |  |  | ?>
 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 319 |  |  |  | 
            
                        
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:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.