HomeController.php 2.9 KB

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