简体   繁体   中英

php: slack or email notifications sending

I have a controller called Guzzle controller.Yurrently, I am passing the slack class as a dependency through the constructor, but iam not using it. Instead i am using the Slack class as static ( Slack::send(...blablabla ).

const DEFAULT_FREQUENCY = 1;

private $client;
private $Slack;

public function __construct(Client $clinet,Slack $slack)
{
    $this->client = $clinet;
    $this->slack = $slack;
}

public function status()
{
    $notifications = Notification::where('active', 1)->get();
    $status = Status::where('name', 'health')->first();

    foreach ($notifications as $notification) {
        $this->updateStatus($notification, $status);
    }
}

private function updateStatus(Notification $notification, Status $status)
{
    $status_health = $notification->status('health');

    $frequency = $this->getFrequency($notification);

    $elapsed_time = \Carbon\Carbon::parse($status_health['timestamp'])->diffInMinutes();

    if ($elapsed_time >= $frequency) {
        $response = $this->client->get($notification->website_url, ['http_errors' => false]);   
        $notification->statuses()->attach($status, [
            'values'=> $response->getStatusCode() === 200 ? 'up' : 'down'
            ]);
        $resCode = $response->getStatusCode();
        if($resCode != 200){
            Slack::send('the site is dying help!!');
        }
    }

}

private function getFrequency(Notification $notification)
{
    return isset($notification->check_frequency) 
    ? intval($notification->check_frequency) 
    : self::DEFAULT_FREQUENCY;
}

} now iam trying to accomplish the following task 1, But GuzzleController class shouldn't be depending on Slack. But it should depend on another class. Let's call it Reporter and its job would be to report to certain channels when needed?

Please follow the below approach to send Slack/Email notifications..

Create a folder with the name Utilities inside app . Now create a class with Reporter.php as follows

namespace App\\Utilities;

use Slack; use Mail;

class Reporter
{
  public function slack($message,$slack_channel)
  {
     /*  Slack::to($slackChannel)->send($message);*/
    Slack::to($slack_channel)->send($message);


  }

  public function mail($emails, $message)
  {

    Mail::queue('emails.notification', ['message' => $message], function($m) use ($emails) {
      $m->to($emails);
      $m->subject('Notification....');
    });
  }
}

Now instead of using the name GuzzleController , use something like NotificationController because the controller is majorly used for sending notifications and not just checking site status via Guzzle ...

So, rename GuzzleController.php to NotificationController.php and replace its contents with

  namespace App\Http\Controllers;

    use GuzzleHttp\Client;
    use App\Utilities\Reporter;
    use GuzzleHttp\Exception\ClientException;

    class NotificationController
    {
      const DEFAULT_FREQUENCY = 1;

      protected $client;
      protected $reporter;

      public function __construct()
      {
        $this->client = new Client;

        $this->reporter = new Reporter;
      }

      public function status()
      {
        $notifications = Notification::where('active', 1)->get();

        $status = Status::where('name', 'health')->first();

        foreach ($notifications as $notification) {
          $this->updateStatus($notification, $status);
        }
      }

      private function updateStatus(Notification $notification, Status $status)
      {
        $status_health = $notification->status('health');

        $frequency = $this->getFrequency($notification);

        $elapsed_time = \Carbon\Carbon::parse($status_health['timestamp'])->diffInMinutes();

        if($elapsed_time >= $frequency) {
          $response = $this->client->get($notification->website_url, [
            'http_errors' => false
          ]);   

          $resCode = $response->getStatusCode();

          $notification->statuses()->attach($status, [
            'values' => $resCode === 200 ? 'up' : 'down'
          ]);

          if($resCode != 200){
            $this->reporter->slack('the site is dying help!!');

            // And if you also want to send email
            // $this->reporter->mail($userEmail, 'the site is dying help!!');
          }
        }

      }

      private function getFrequency(Notification $notification)
      {
        return isset($notification->check_frequency) 
        ? intval($notification->check_frequency) 
        : self::DEFAULT_FREQUENCY;
      }
    }

Basically, as asked by you, you have a separate class (Reporter) to send Slack/Mail notifications and separate class to for updating Statuses and sending Notifications based on frequency etc...

Hope I understood your question well, let me know if you face any other issues by commenting below :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM