HomeController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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) {
  47. if ($hoursLeft < 1) {
  48. return;
  49. } else {
  50. return "hours";
  51. }
  52. }
  53. return "days";
  54. }
  55. /**
  56. * @description Get the Text for the Days-Left-Box in HomeView
  57. *
  58. * @param float $daysLeft
  59. * @param float $hoursLeft
  60. *
  61. * @return string
  62. */
  63. public function getTimeLeftBoxText(float $daysLeft, float $hoursLeft)
  64. {
  65. if ($daysLeft < 1) {
  66. if ($hoursLeft < 1) {
  67. return $this::TIME_LEFT_OUT_OF_CREDITS_TEXT;
  68. } else {
  69. return strval($hoursLeft);
  70. }
  71. }
  72. return strval(number_format($daysLeft, 0));
  73. }
  74. /** Show the application dashboard. */
  75. public function index(Request $request)
  76. {
  77. $usage = Auth::user()->creditUsage();
  78. $credits = Auth::user()->Credits();
  79. $bg = "";
  80. $boxText = "";
  81. $unit = "";
  82. /** Build our Time-Left-Box */
  83. if ($credits > 0.01 and $usage > 0) {
  84. $daysLeft = number_format(($credits * 30) / $usage, 2, '.', '');
  85. $hoursLeft = number_format($credits / ($usage / 30 / 24), 2, '.', '');
  86. $bg = $this->getTimeLeftBoxBackground($daysLeft);
  87. $boxText = $this->getTimeLeftBoxText($daysLeft, $hoursLeft);
  88. $unit = $daysLeft < 1 ? ($hoursLeft < 1 ? null : "hours") : "days";
  89. }
  90. // RETURN ALL VALUES
  91. return view('home')->with([
  92. 'useage' => $usage,
  93. 'credits' => $credits,
  94. 'useful_links' => UsefulLink::all()->sortBy('id'),
  95. 'bg' => $bg,
  96. 'boxText' => $boxText,
  97. 'unit' => $unit
  98. ]);
  99. }
  100. }