Last active
April 5, 2018 17:00
-
-
Save tschope/deb56ab640737310ced3c57cb71022ed to your computer and use it in GitHub Desktop.
Example how to use IRP class with Laravel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
This file is a command, use in cosole Laravel | |
*/ | |
namespace App\Console\Commands; | |
use Carbon\Carbon; | |
use Illuminate\Console\Command; | |
use Illuminate\Support\Facades\Mail; | |
use Illuminate\Support\Facades\Storage; | |
use Irpclass; | |
class Gnib extends Command | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'gnib:find'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'This command find appoiments in GNIB Website'; | |
/** | |
* Create a new command instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
parent::__construct(); | |
} | |
/** | |
* @param $slots | |
* @param null $until_date date until your search. If null dont have final date | |
* @param null $start_date date start your search, if null, now is your date | |
* @return bool | |
*/ | |
private function getDates($slots, $until_date = null, $start_date = null) | |
{ | |
if(empty($start_date)) | |
$start_date = Carbon::now(); | |
else | |
$start_date = new Carbon($start_date); | |
if(!empty($until_date)) | |
$until_date = new Carbon($until_date); | |
foreach ($slots as $slot) { | |
if (is_object($slot['time'])) { | |
if(!empty($until_date)){ | |
//If time slot greaterThanOrEqualTo start time search AND time slot less Than Or Equal To until date | |
if($slot['time']->gte($start_date) AND $slot['time']->lte($until_date)) | |
return true; | |
else | |
return false; | |
} else { | |
//If time slot greater Than Or Equal To start time search | |
if($slot['time']->gte($start_date)) | |
return true; | |
else | |
return false; | |
} | |
} | |
} | |
return false; | |
} | |
private function getAppoiment($params, $until_date = null, $start_date = null, $to){ | |
$irpClass = new Irpclass\Irpclass(); | |
$response = $irpClass->get($params); | |
if($response['success']) | |
{ | |
if(!empty($response['results'])) { | |
//If retrive some dates out of the dates you set, don't send e-mail to you | |
if($this->getDates($response['results'],$until_date, $start_date)){ | |
$log = date('Y-m-d H:i:s').' - '.json_encode($response); | |
Storage::disk('local')->append('find-true-date-'.$params['cat'].'-log-'.date('Y-m-d').'.txt', $log); | |
$this->send(count($response['results']), $response['results'],$to,$params['cat']); | |
} else{ | |
$log = date('Y-m-d H:i:s').' - '.json_encode($response); | |
Storage::disk('local')->append('find-false-date-'.$params['cat'].'-log-'.date('Y-m-d').'.txt', $log); | |
$this->info('Find dates, but not in your setup range.'); | |
} | |
}else{ | |
$log = date('Y-m-d H:i:s').' - '.json_encode($response); | |
Storage::disk('local')->append('empty-log-'.$params['cat'].'-'.date('Y-m-d').'.txt', $log); | |
$this->info('None dates available. Sorry :( '); | |
} | |
} | |
else | |
{ | |
$log = date('Y-m-d H:i:s').' - '.json_encode($response); | |
Storage::disk('local')->append('error-log-'.$params['cat'].'-'.date('Y-m-d').'.txt', $log); | |
$this->error('OH MY GOD!!! Something wrong on application, could you check? '); | |
} | |
} | |
private function retriveServer() | |
{ | |
$params = []; | |
$params['cat'] = 'Study'; | |
$params['sbcat'] = 'All'; | |
$params['typ'] = 'Renewal'; | |
$this->getAppoiment($params,'2018-05-10', '2018-04-11','YOUR EMAIL'); | |
} | |
private function send($count, $slots, $to, $type) | |
{ | |
$title = 'GNIB Notification'; | |
Mail::send('email', ['title' => $title, 'count' => $count, 'slots' => $slots], function ($message) use ($to, $type) | |
{ | |
$message->subject('GNIB Notification - '.$type); | |
$message->from('notification@tschope.tech', 'GNIB Notification'); | |
$message->to($to); | |
}); | |
$this->info("E-mail sent."); | |
return response()->json(['message' => 'Request completed']); | |
} | |
/** | |
* Execute the console command. | |
* | |
* @return mixed | |
*/ | |
public function handle() | |
{ | |
// | |
$this->retriveServer(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment