zalazdi

Handler.php

Mar 25th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.11 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Listeners;
  4.  
  5. use Cartalyst\Sentinel\Sentinel as Sentinel;
  6. use App\Services\ActionlogService;
  7. use Illuminate\Mail\Mailer;
  8. use App;
  9.  
  10. class Handler
  11. {
  12.     /**
  13.      * Sentinel Instance
  14.      * @var int
  15.      */
  16.     protected $sentinel;
  17.  
  18.     /**
  19.      * Actionlog Service Instance
  20.      * @var
  21.      */
  22.     protected $log;
  23.  
  24.     /**
  25.      * Illuminate\Mail\Mailer Instance
  26.      * @var
  27.      */
  28.     protected $mail;
  29.  
  30.     public function __construct()
  31.     {
  32.         $this->sentinel = App::make(Sentinel::class);
  33.         $this->log = App::make(ActionlogService::class);
  34.         $this->mail = App::make(Mailer::class);
  35.     }
  36.  
  37.     protected function compileDetails($event)
  38.     {
  39.         $details['note'] = $event->note;
  40.         if (empty(array_filter($event->entities))) {
  41.             $details['entities'] = [
  42.                 'actual' => null,
  43.                 'before' => null,
  44.                 'related' => null,
  45.             ];
  46.         } else {
  47.             $details['entities'] = $event->entities;
  48.         }
  49.  
  50.         return json_encode($details);
  51.     }
  52.  
  53.     public function prepareSpamFilter($event)
  54.     {
  55.         $entity = new \stdClass();
  56.         if (! empty($event->entities['actual'])) {
  57.             $entity = json_decode($event->entities['actual']);
  58.         } else {
  59.             $entity->id = null;
  60.         }
  61.  
  62.         return $entity;
  63.     }
  64.  
  65.     protected function createSpamFilter($event)
  66.     {
  67.         $entity = $this->prepareSpamFilter($event);
  68.         $logId = is_array($entity) ? json_encode($entity) : $entity->id;
  69.         session(["log.{$event->name}.{$logId}" => \Carbon\Carbon::now()]);
  70.     }
  71.  
  72.     protected function checkSpamFilter($event)
  73.     {
  74.         $entity = $this->prepareSpamFilter($event);
  75.  
  76.         $logId = is_array($entity) ? json_encode($entity) : $entity->id;
  77.         $logInSession = session("log.{$event->name}.{$logId}");
  78.         if (is_null($logInSession) || $logInSession <= \Carbon\Carbon::now()->subMinutes(1)) {
  79.             \Session::forget("log.{$event->name}.{$logId}");
  80.             return true;
  81.         }
  82.  
  83.         return false;
  84.     }
  85. }
Add Comment
Please, Sign In to add comment