File: D:/HostingSpaces/SBogers10/honger.komma.pro/app/KommaApp/Forms/IpController.php
<?php
/**
* Created by PhpStorm.
* User: mike
* Date: 26/09/17
* Time: 15:24
*/
namespace App\KommaApp\Forms;
use App\Http\Controllers\Controller;
use App\Http\Requests\IpRequest;
use App\Mail\IpMail;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\View;
class IpController extends Controller
{
/**
* @var FormService
*/
private $formService;
/**
* ContactController constructor.
* @param FormService $formService
*/
public function __construct(FormService $formService)
{
parent::__construct();
$this->formService = $formService;
$this->formService->setOrigin('ip');
}
/**
* Store the request and send it by e-mail
* Note: Validation is done in the request itself
*
* @param IpRequest $request
* @return
*/
public function process(IpRequest $request)
{
// Extra validation: check if ip has already been send in past half hour
// Todo: put this in the IpRequest
if($result = DB::table('requests')
->where('origin','ip')
->where('ip',Request::ip())
->orderBy('id','desc')
->first()) {
$createdAt = Carbon::createFromFormat('Y-m-d H:i:s', $result->created_at);
if ($createdAt > Carbon::now()->subMinutes(30))
return Redirect::back()->withErrors(
['name' => 'Je hebt zojuist je IP al doorgegeven, probeer het later opnieuw.']
);
}
// Put ip in request
$request['ip'] = Request::ip();
// Store request in the database
$this->formService->storeRequest($request);
// Remove token from request (this creates and array)
Mail::send(new IpMail($request->except(['_token','honing'])));
// Flash success session for the next request only
Session::flash('success', 1);
// Send to the success route
return Redirect::route('ip.success');
}
/**
* Show page where we thank the user
*
* @return mixed
*/
public function success()
{
// Make sure a request is successful
if( ! Session::has('success')) return Redirect::to('/ip');
return View::make('site.pages.ipSuccess',[
'namespace' => 'ip'
]);
}
}