Przeglądaj źródła

added suspended server notification

AVMG20 4 lat temu
rodzic
commit
5171f01852

+ 44 - 8
app/Console/Commands/ChargeCreditsCommand.php

@@ -2,13 +2,11 @@
 
 
 namespace App\Console\Commands;
 namespace App\Console\Commands;
 
 
-use App\Classes\Pterodactyl;
 use App\Models\Product;
 use App\Models\Product;
 use App\Models\Server;
 use App\Models\Server;
 use App\Models\User;
 use App\Models\User;
-use Carbon\Carbon;
+use App\Notifications\ServersSuspendedNotification;
 use Illuminate\Console\Command;
 use Illuminate\Console\Command;
-use Illuminate\Support\Collection;
 
 
 class ChargeCreditsCommand extends Command
 class ChargeCreditsCommand extends Command
 {
 {
@@ -26,6 +24,13 @@ class ChargeCreditsCommand extends Command
      */
      */
     protected $description = 'Charge all users with active servers';
     protected $description = 'Charge all users with active servers';
 
 
+
+    /**
+     * A list of users that have to be notified
+     * @var array
+     */
+    protected $usersToNotify = [];
+
     /**
     /**
      * Create a new command instance.
      * Create a new command instance.
      *
      *
@@ -43,7 +48,7 @@ class ChargeCreditsCommand extends Command
      */
      */
     public function handle()
     public function handle()
     {
     {
-        return Server::whereNull('suspended')->chunk(10, function ($servers) {
+        Server::whereNull('suspended')->chunk(10, function ($servers) {
             /** @var Server $server */
             /** @var Server $server */
             foreach ($servers as $server) {
             foreach ($servers as $server) {
                 /** @var Product $product */
                 /** @var Product $product */
@@ -51,15 +56,46 @@ class ChargeCreditsCommand extends Command
                 /** @var User $user */
                 /** @var User $user */
                 $user = $server->user;
                 $user = $server->user;
 
 
-               #charge credits / suspend server
-                if ($user->credits >= $product->getHourlyPrice()){
+                #charge credits / suspend server
+                if ($user->credits >= $product->getHourlyPrice()) {
                     $this->line("<fg=blue>{$user->name}</> Current credits: <fg=green>{$user->credits}</> Credits to be removed: <fg=red>{$product->getHourlyPrice()}</>");
                     $this->line("<fg=blue>{$user->name}</> Current credits: <fg=green>{$user->credits}</> Credits to be removed: <fg=red>{$product->getHourlyPrice()}</>");
                     $user->decrement('credits', $product->getHourlyPrice());
                     $user->decrement('credits', $product->getHourlyPrice());
                 } else {
                 } else {
-                    $this->line("server <fg=blue>{$server->name}</> <fg=red>has been suspended! </>");
-                    $server->suspend();
+                    try {
+                        #suspend server
+                        $this->line("<fg=yellow>{$server->name}</> from user: <fg=blue>{$user->name}</> has been <fg=red>suspended!</>");
+                        #$server->suspend();
+
+                        #add user to notify list
+                        if (!in_array($user, $this->usersToNotify)) {
+                            array_push($this->usersToNotify, $user);
+                        }
+                    } catch (\Exception $exception) {
+                        $this->error($exception->getMessage());
+                    }
+
                 }
                 }
             }
             }
         });
         });
+
+        return $this->notifyUsers();
+    }
+
+    /**
+     * @return bool
+     */
+    public function notifyUsers()
+    {
+        if (!empty($this->usersToNotify)) {
+            /** @var User $user */
+            foreach ($this->usersToNotify as $user) {
+                $this->line("<fg=yellow>Notified user:</> <fg=blue>{$user->name}</>");
+                $user->notify(new ServersSuspendedNotification());
+            }
+        }
+
+        #reset array
+        $this->usersToNotify = array();
+        return true;
     }
     }
 }
 }

+ 63 - 0
app/Notifications/ServersSuspendedNotification.php

@@ -0,0 +1,63 @@
+<?php
+
+namespace App\Notifications;
+
+use Illuminate\Bus\Queueable;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Notifications\Messages\MailMessage;
+use Illuminate\Notifications\Notification;
+
+class ServersSuspendedNotification extends Notification implements ShouldQueue
+{
+    use Queueable;
+
+    /**
+     * Create a new notification instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        //
+    }
+
+    /**
+     * Get the notification's delivery channels.
+     *
+     * @param  mixed  $notifiable
+     * @return array
+     */
+    public function via($notifiable)
+    {
+        return ['mail'];
+    }
+
+    /**
+     * Get the mail representation of the notification.
+     *
+     * @param  mixed  $notifiable
+     * @return \Illuminate\Notifications\Messages\MailMessage
+     */
+    public function toMail($notifiable)
+    {
+        return (new MailMessage)
+                    ->subject('Your servers have been suspended!')
+                    ->greeting('Your servers have been suspended!')
+                    ->line("To unsuspend your server/s you need to purchase more credits.")
+                    ->action('Purchase credits', route('store.index'))
+                    ->line('If you have any questions please let us know.');
+    }
+
+    /**
+     * Get the array representation of the notification.
+     *
+     * @param  mixed  $notifiable
+     * @return array
+     */
+    public function toArray($notifiable)
+    {
+        return [
+            //
+        ];
+    }
+}