dpd2349 2016-11-25 16:13
浏览 36
已采纳

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

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?

  • 写回答

1条回答 默认 最新

  • dongzou7134 2016-11-26 20:05
    关注

    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 :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R