Clean code and fetch servers with product in unSuspend method

This commit is contained in:
Tobiletsmc 2022-06-05 12:22:26 +02:00
parent f6eab834fc
commit 204629539f

View file

@ -179,7 +179,7 @@ class User extends Authenticatable implements MustVerifyEmail
*/ */
public function suspend() public function suspend()
{ {
foreach ($this->servers as $server) { foreach ($this->servers() as $server) {
$server->suspend(); $server->suspend();
} }
@ -195,7 +195,7 @@ class User extends Authenticatable implements MustVerifyEmail
*/ */
public function unSuspend() public function unSuspend()
{ {
foreach ($this->servers as $server) { foreach ($this->getServersWithProduct() as $server) {
if ($this->credits >= $server->product->getHourlyPrice()) { if ($this->credits >= $server->product->getHourlyPrice()) {
$server->unSuspend(); $server->unSuspend();
} }
@ -232,20 +232,21 @@ class User extends Authenticatable implements MustVerifyEmail
* @return string * @return string
*/ */
public function creditUsage() public function creditUsage()
{ {
$servers = Server::query()
->where('user_id', '=', $this->id)
->with('product')
->get();
$usage = 0; $usage = 0;
foreach ($servers as $server) { foreach ($this->getServersWithProduct() as $server) {
$usage += $server->product->price; $usage += $server->product->price;
} }
return number_format($usage, 2, '.', ''); return number_format($usage, 2, '.', '');
} }
private function getServersWithProduct() {
return $this->servers()
->with('product')
->get();
}
/** /**
* @return array|string|string[] * @return array|string|string[]
*/ */