walletservice/app/Http/Controllers/UserController.php

186 lines
6.7 KiB
PHP
Raw Normal View History

2020-06-11 13:42:35 +00:00
<?php
namespace App\Http\Controllers;
use App\Models\Identification;
use App\Models\User;
use App\Traits\ApiResponser;
use App\Traits\Helper;
2020-06-11 13:42:35 +00:00
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\Mail;
class UserController extends Controller
{
use ApiResponser;
use Helper;
2020-06-11 13:42:35 +00:00
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
//
}
public function identification(Request $request)
{
$identification = new Identification();
$this->validate($request, $identification->rules());
$dbIdentification = Identification::where('id_user', $request->id_user)->first();
if($dbIdentification)
return $this->errorResponse(trans('errors.identification_carried_out'));
if($request->has('id_user'))
$user = User::findOrFail($request->id_user);
elseif ($request->has('phone_number'))
$user = User::where('phone',$request->phone_number)->firstOrFail();
2020-06-11 13:42:35 +00:00
$identification->fill($request->all());
$identification->id_user = $user->id;
2020-06-11 13:42:35 +00:00
$identification->status = 0;
$identification->save();
$this->sendMail($user->email,trans('messages.successful_identification'),
2020-06-14 07:37:30 +00:00
trans('messages.successful_identification_message',['name'=>$identification->lastname.' '.$identification->firstname]));
return $this->successResponse(trans('messages.successful_identification',['name'=>$identification->lastname.' '.$identification->firstname]));
2020-06-11 13:42:35 +00:00
}
public function validateIdentification(Request $request, $id_identification)
{
$identification = Identification::findOrFail($id_identification);
if ($identification->status == 1) {
return $this->errorResponse(trans('messages.identification_already_validated'));
}
// dd($request->allFiles());
$this->validate($request, [
'id_network' => 'required|integer|min:0|not_in:0',
2020-06-12 06:11:11 +00:00
'id_country' => 'required|integer|min:0|not_in:0',
'document_image_front' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:6048',
'document_image_back' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:6048',
2020-06-11 13:42:35 +00:00
'user_image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:6048',
]);
$identification->idNetwork = $request->id_network;
2020-06-12 06:11:11 +00:00
$identification->id_country = $request->id_country;
$identification->document_image_front = $this->uploadImage($request,'document_image_front','D-F',"documents");
$identification->document_image_back = $this->uploadImage($request,'document_image_back','D-B',"documents");
2020-06-11 13:42:35 +00:00
$identification->user_image = $this->uploadImage($request,'user_image','U',"photos");
2020-06-11 13:42:35 +00:00
$identification->status = 1;
$identification->save();
$this->sendMail($identification->user->email,trans('messages.validated_identification'),trans('messages.validated_identification_message'));
2020-06-11 13:42:35 +00:00
return $this->successResponse(trans('messages.validated_identification'));
}
2020-06-16 11:23:09 +00:00
public function fetchIdentification($id_user)
2020-06-12 06:11:11 +00:00
{
2020-06-16 11:23:09 +00:00
// return Identification::where('id_user', $id_user)->firstOrFail();
$identification = Identification::where('id_user', $id_user)->first();
$data['isIdentified'] = false;
$data['isIdentifiedValidated'] = false;
$data['data'] = $identification;
if($identification){
$data['isIdentified'] = true;
$data['isIdentifiedValidated'] = $identification->status == 1 ;
}
return $this->successResponse($data);
2020-06-12 06:11:11 +00:00
}
2020-06-14 07:37:30 +00:00
public function verifyIdentification($user_phone)
2020-06-12 06:11:11 +00:00
{
2020-06-14 07:37:30 +00:00
$user = User::where('phone', $user_phone)->firstOrFail();
$identification = Identification::where('id_user', $user->id)->first();
if($identification){
return $this->errorResponse(trans('messages.user_identificated'));
}else
return $this->successResponse($user);
2020-06-12 06:11:11 +00:00
}
2020-06-11 13:42:35 +00:00
private function generateRandomString($length = 10)
{
$characters = '23456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
public function uploadImage(Request $request , $key , $imageCode, $folderName)
{
// if ($request->hasFile('image')) {
$original_filename = $request->file($key)->getClientOriginalName();
$original_filename_arr = explode('.', $original_filename);
$file_ext = end($original_filename_arr);
$image = $imageCode.'-' . time() . '.' . $file_ext;
//Check if the directory already exists.
$directoryName = './'.$folderName;
if(!is_dir($directoryName)){
//Directory does not exist, so lets create it.
mkdir($directoryName, 0755);
}
// Allow certain file formats
// $allowTypes = array('jpg', 'png', 'jpeg', 'gif');
// if (in_array(strtolower($file_ext), $allowTypes)) {
$compressedImage = $this->compressImage($request->file($key), './'.$folderName.'/' . $image, 70);
if ($compressedImage) {
return $image;
} else {
return $this->errorResponse(trans('errors.compression_failed'));
}
// } else {
// return $this->errorResponse('Sorry, only JPG, JPEG, PNG, & GIF files are allowed to upload.');
// }
// } else {
// return $this->errorResponse('File not found');
// }
}
/*
* Custom function to compress image size and
* upload to the server using PHP
*/
private function compressImage($source, $destination, $quality)
{
// Get image info
$imgInfo = getimagesize($source);
$mime = $imgInfo['mime'];
// Create a new image from file
switch ($mime) {
case 'image/jpeg':
$image = imagecreatefromjpeg($source);
break;
case 'image/png':
$image = imagecreatefrompng($source);
break;
case 'image/gif':
$image = imagecreatefromgif($source);
break;
default:
$image = imagecreatefromjpeg($source);
}
// Save image
imagejpeg($image, $destination, $quality);
// Return compressed image
return $destination;
}
}