Skip to content

Instantly share code, notes, and snippets.

@emrancu
Last active May 19, 2020 05:06
Show Gist options
  • Save emrancu/32198e0541b6bac13c68e991423a3d7c to your computer and use it in GitHub Desktop.
Save emrancu/32198e0541b6bac13c68e991423a3d7c to your computer and use it in GitHub Desktop.
<?php
namespace App\Utilities\Wizard;
use App\Models\User\User;
use App\Models\Wizard;
use Carbon\Carbon;
use Cartalyst\Sentinel\Sentinel;
use Cartalyst\Sentinel\Users\UserInterface;
class WizardController
{
/**
* Login user info
* @var UserInterface|null
*/
protected $user;
/**
* store all steps of wizard
* @var string[]
*/
protected $steps = [
[
"step" => "project_create",
"title" => "Create Project",
"description" => "Create Project",
"route" => "create-project",
"thumbnail" => "",
],
[
"step" => "bot_create",
"title" => "Create Messenger Bot",
"description" => "Create Project",
"route" => "create-project",
"thumbnail" => "",
]
];
/**
* set Login User to $user property
* WizardController constructor.
*/
public function __construct()
{
$this->user = Sentinel::getUser();
}
/**
* active wizard status to user table
* if status is 'active', it means user wizard system is active & running.
* if status is 'done', it means user Wizard is completed
* @return int
*/
public function active()
{
return User::where('id', $this->user->id)->update(['wizard_status' => 'active']);
}
/**
* make done current step
* @param array $step
* @return bool|string[]
*/
public function doneCurrentStep($step = [])
{
$wizard = Wizard::where('user_id', $this->user->id)->first();
if ($wizard) { // check user wizard exist or not
if (empty($step)) { // if provided $step empty
$step = $this->currentStep($wizard); // get current step
if (!$step) { // already done all steps
return false;
}
}
$doneSteps = json_decode($wizard->done_steps, true); // get completed steps
// current stem data for add to completed steps
$doneStep = [
"step" => $step['step'],
"done_at" => Carbon::now()
];
array_push($doneSteps, $doneStep); // push current step data to completed steps
$lastStep = $this->checkLastStep($step['step']); // check last step
if ($lastStep) { // if current step is last step
$this->makeDone(); // make wizard completed status to user table
}
// update completed steps to wizard table
// wizard status update . if if current step is last step then true or false. status true means wizard completed
Wizard::where('user_id', $this->user->id)->update([
"done_steps" => json_encode($doneSteps, true),
"status" => $lastStep,
]);
return true;
}
return false;
}
/**
* get current Step
* @param null $wizard
* @return bool|string[]
*/
public function currentStep($wizard = null)
{
if (!$wizard) { // if $wizard not passed then set user wizard
$wizard = Wizard::where('user_id', $this->user->id)->first();
}
// if wizard not created, create user wizard
if (!$wizard) {
Wizard::create([
"user_id" => $this->user->id,
"done_steps" => json_encode([], true),
"type" => '',
"status" => 0,
]);
return $this->steps[0]; // return first step from Wizard Steps
}
$doneSteps = json_decode($wizard->done_steps, true);
return $this->makeCurrentStep($doneSteps); // collect current step data
}
/**
* generating current step , comparing with completed step
* @param $doneSteps
* @return bool|string[]
*/
private function makeCurrentStep($doneSteps)
{
// if completed step is empty then return first step of wizard
if (empty($doneSteps)) {
return $this->steps[0];
}
// get last completed step
$lastDoneStep = end($doneSteps);
$key = null;
// loop with Wizard steps . When last step and wizard step match , get next step index (with adding 1 to the key)
foreach ($this->steps as $key => $step) {
if ($lastDoneStep['step'] === $step['step']) {
$key = $key + 1;
break;
}
}
// if next step exist then return the step , else return false (means all step completed;
return isset($this->steps[$key]) ? $this->steps[$key] : false;
}
/**
* check current step is last step or not
* if current step is last step then return true , otherwise return false
* @param $currentStep
* @return bool
*/
public function checkLastStep($currentStep)
{
$lastStep = end($this->steps);
if ($lastStep['step'] === $currentStep) {
return true;
}
return false;
}
/**
* make wizard status done to user table
* if status is 'done', it means user Wizard is completed
* @return int
*/
public function makeDone()
{
return User::where('id', $this->user->id)->update(['wizard_status' => 'done']);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment