Queue.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\Core\Queue;
  3. use ThurData\Servers\KerioEmail\Core\DependencyInjection;
  4. class Queue
  5. {
  6. /**
  7. * @var null
  8. */
  9. protected $callBefore = null;
  10. /**
  11. * @var null
  12. */
  13. protected $callAfter = null;
  14. /**
  15. *
  16. */
  17. public function process()
  18. {
  19. $queue = DependencyInjection::get(DatabaseQueue::class);
  20. while($model = $queue->pop())
  21. {
  22. if($this->callBefore)
  23. {
  24. $callback = $this->callBefore;
  25. $callback($model);
  26. }
  27. $job = new Manager($model);
  28. $job->fire();
  29. if($this->callAfter)
  30. {
  31. $callback = $this->callAfter;
  32. $callback($model);
  33. }
  34. }
  35. }
  36. /**
  37. * @param $callable
  38. * @throws \Exception
  39. */
  40. public function setCallBefore($callable)
  41. {
  42. if(!is_callable($callable))
  43. {
  44. throw new \Exception('Argument is not callable');
  45. }
  46. $this->callBefore = $callable;
  47. }
  48. /**
  49. * @param $callable
  50. * @throws \Exception
  51. */
  52. public function setCallAfter($callable)
  53. {
  54. if(!is_callable($callable))
  55. {
  56. throw new \Exception('Argument is not callable');
  57. }
  58. $this->callAfter = $callable;
  59. }
  60. }