HomeController.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Egg;
  4. use App\Models\Product;
  5. use App\Models\UsefulLink;
  6. use App\Models\Configuration;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\Auth;
  9. class HomeController extends Controller
  10. {
  11. const TIME_LEFT_BG_SUCCESS = "bg-success";
  12. const TIME_LEFT_BG_WARNING = "bg-warning";
  13. const TIME_LEFT_BG_DANGER = "bg-danger";
  14. const TIME_LEFT_OUT_OF_CREDITS_TEXT = "You ran out of Credits";
  15. public function __construct()
  16. {
  17. $this->middleware('auth');
  18. }
  19. /**
  20. * @description Get the Background Color for the Days-Left-Box in HomeView
  21. *
  22. * @param float $daysLeft
  23. *
  24. * @return string
  25. */
  26. public function getTimeLeftBoxBackground(float $daysLeft): string
  27. {
  28. if ($daysLeft >= 15) {
  29. return $this::TIME_LEFT_BG_SUCCESS;
  30. }
  31. if ($daysLeft <= 7) {
  32. return $this::TIME_LEFT_BG_DANGER;
  33. }
  34. return $this::TIME_LEFT_BG_WARNING;
  35. }
  36. /**
  37. * @description Set "hours", "days" or nothing behind the remaining time
  38. *
  39. * @param float $daysLeft
  40. * @param float $hoursLeft
  41. *
  42. * @return string|void
  43. */
  44. public function getTimeLeftBoxUnit(float $daysLeft, float $hoursLeft)
  45. {
  46. if ($daysLeft > 1) return 'days';
  47. return $hoursLeft < 1 ? null : "hours";
  48. }
  49. /**
  50. * @description Get the Text for the Days-Left-Box in HomeView
  51. *
  52. * @param float $daysLeft
  53. * @param float $hoursLeft
  54. *
  55. * @return string
  56. */
  57. public function getTimeLeftBoxText(float $daysLeft, float $hoursLeft)
  58. {
  59. if ($daysLeft > 1) return strval(number_format($daysLeft, 0));
  60. return ($hoursLeft < 1 ? $this::TIME_LEFT_OUT_OF_CREDITS_TEXT : strval($hoursLeft));
  61. }
  62. /** Show the application dashboard. */
  63. public function index(Request $request)
  64. {
  65. $usage = Auth::user()->creditUsage();
  66. $credits = Auth::user()->Credits();
  67. $bg = "";
  68. $boxText = "";
  69. $unit = "";
  70. /** Build our Time-Left-Box */
  71. if ($credits > 0.01 and $usage > 0) {
  72. $daysLeft = number_format(($credits * 30) / $usage, 2, '.', '');
  73. $hoursLeft = number_format($credits / ($usage / 30 / 24), 2, '.', '');
  74. $bg = $this->getTimeLeftBoxBackground($daysLeft);
  75. $boxText = $this->getTimeLeftBoxText($daysLeft, $hoursLeft);
  76. $unit = $daysLeft < 1 ? ($hoursLeft < 1 ? null : "hours") : "days";
  77. }
  78. // RETURN ALL VALUES
  79. return view('home')->with([
  80. 'useage' => $usage,
  81. 'credits' => $credits,
  82. 'useful_links' => UsefulLink::all()->sortBy('id'),
  83. 'bg' => $bg,
  84. 'boxText' => $boxText,
  85. 'unit' => $unit
  86. ]);
  87. }
  88. }