php:松弛或电子邮件通知发送

时间:2016-11-25 16:13:49

标签: php laravel

我有一个名为Guzzle controller的控制器。目前,我通过构造函数将松弛类作为依赖项传递,但是我没有使用它。相反,我使用Slack类作为静态(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;
}

} 现在我想完成以下任务 1,但GuzzleController类不应该依赖Slack。但它应该依赖于另一个类。我们称之为Reporter,它的工作是在需要时向某些渠道报告?

1 个答案:

答案 0 :(得分:0)

请按照以下方法发送Slack / Email通知..

Utilities内创建名称为app的文件夹。现在创建一个Reporter.php的类,如下所示

命名空间App \ Utilities;

使用Slack; 使用邮件;

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....');
    });
  }
}

现在不使用名称GuzzleController,而是使用类似NotificationController的内容,因为控制器主要用于发送通知,而不仅仅是通过 Guzzle 检查网站状态...

因此,请将GuzzleController.php重命名为NotificationController.php并将其内容替换为

  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;
      }
    }

基本上,正如您所问,您有一个单独的类(Reporter)发送Slack / Mail通知单独的类来更新状态和发送通知基于频率等...

希望我能理解你的问题,请通过以下评论告诉我你是否面临任何其他问题:)