Smaller, Better, PHPstorm. Refactored some Variabled

This commit is contained in:
WBLKLeipe 2021-11-11 16:19:16 +01:00
parent 14bceeb311
commit c981add07c

View file

@ -1,4 +1,5 @@
<?php <?php
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Models\Egg; use App\Models\Egg;
@ -8,11 +9,14 @@ use App\Models\Configuration;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
/**
*
*/
class HomeController extends Controller class HomeController extends Controller
{ {
const TIME_LEFT_BG_SUCCESS = "bg-success"; const TIME_LEFT_BG_SUCCESS = "bg-success";
const TIME_LEFT_BG_WARNING = "bg-warning"; const TIME_LEFT_BG_WARNING = "bg-warning";
const TIME_LEFT_BG_DANGER = "bg-danger"; const TIME_LEFT_BG_DANGER = "bg-danger";
const TIME_LEFT_OUT_OF_CREDITS_TEXT = "You ran out of Credits"; const TIME_LEFT_OUT_OF_CREDITS_TEXT = "You ran out of Credits";
public function __construct() public function __construct()
@ -21,51 +25,38 @@ class HomeController extends Controller
} }
/** /**
* @description Get the Background Color for the Days-Left-Box in HomeView * @description Get the Background Color for the Days-Left-Box in HomeView
* *
* @param float $days * @param float $days
* *
* @return string * @return string
*/ */
public function getTimeLeftBoxBackground(float $days) public function getTimeLeftBoxBackground(float $days): string
{ {
switch ($days) if ($days >= 15) {
{ return $this::TIME_LEFT_BG_SUCCESS;
case ($days >= 15):
return $this::TIME_LEFT_BG_SUCCESS;
break;
case ($days >= 8 && $days <= 14):
return $this::TIME_LEFT_BG_WARNING;
break;
case ($days <= 7):
return $this::TIME_LEFT_BG_DANGER;
break;
default:
return $this::TIME_LEFT_BG_WARNING;
} }
if ($days <= 7) {
return $this::TIME_LEFT_BG_DANGER;
}
return $this::TIME_LEFT_BG_WARNING;
} }
/** /**
* @description Set "hours", "days" or nothing behind the remaining time * @description Set "hours", "days" or nothing behind the remaining time
* *
* @param float $days * @param float $days
* @param float $hours * @param float $hours
* *
* @return string * @return string|void
*/ */
public function getTimeLeftBoxUnit(float $days, float $hours) public function getTimeLeftBoxUnit(float $days, float $hours)
{ {
if ($days < 1) if ($days < 1) {
{ if ($hours < 1) {
if ($hours < 1)
{
return; return;
} } else {
else
{
return "hours"; return "hours";
} }
} }
@ -73,23 +64,19 @@ class HomeController extends Controller
} }
/** /**
* @description Get the Text for the Days-Left-Box in HomeView * @description Get the Text for the Days-Left-Box in HomeView
* *
* @param float $days * @param float $days
* @param float $hours * @param float $hours
* *
* @return string * @return string
*/ */
public function getTimeLeftBoxText(float $days, float $hours) public function getTimeLeftBoxText(float $days, float $hours)
{ {
if ($days < 1) if ($days < 1) {
{ if ($hours < 1) {
if ($hours < 1)
{
return $this::TIME_LEFT_OUT_OF_CREDITS_TEXT; return $this::TIME_LEFT_OUT_OF_CREDITS_TEXT;
} } else {
else
{
return strval($hours); return strval($hours);
} }
} }
@ -106,19 +93,17 @@ class HomeController extends Controller
$unit = ""; $unit = "";
/** Build our Time-Left-Box */ /** Build our Time-Left-Box */
if ($credits > 0.01 and $usage > 0) if ($credits > 0.01 and $usage > 0) {
{ $daysLeft = number_format(($credits * 30) / $usage, 2, '.', '');
$days = number_format(($credits * 30) / $usage, 2, '.', ''); $hoursLeft = number_format($credits / ($usage / 30 / 24), 2, '.', '');
$hours = number_format($credits / ($usage / 30 / 24) , 2, '.', '');
$bg = $this->getTimeLeftBoxBackground($days); $bg = $this->getTimeLeftBoxBackground($daysLeft);
$boxText = $this->getTimeLeftBoxText($days, $hours); $boxText = $this->getTimeLeftBoxText($daysLeft, $hoursLeft);
$unit = $this->getTimeLeftBoxUnit($days, $hours); $unit = $daysLeft < 1 ? ($hoursLeft < 1 ? null : "hours") : "daysLeft";
} }
// RETURN ALL VALUES // RETURN ALL VALUES
return view('home')->with([ return view('home')->with([
'useage' => $usage, 'useage' => $usage,