QueueSystem.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Giancarlo
  5. * Date: 09/08/2018
  6. * Time: 11:27
  7. */
  8. namespace OpenMind\System;
  9. use DB\SQL\Mapper;
  10. class QueueSystem extends Mapper
  11. {
  12. private $f3;
  13. private $serviceName;
  14. // Used internally
  15. private $initLoad;
  16. public function __construct(string $serviceName)
  17. {
  18. global $f3;
  19. $this->f3 = $f3;
  20. parent::__construct($f3->get('DB'), 'queuesystem');
  21. $this->serviceName = $serviceName;
  22. }
  23. /**
  24. * Send object to <$serviceName>.
  25. * send() serialize $obj before send to queue
  26. *
  27. * @param $obj
  28. */
  29. public function send($obj)
  30. {
  31. $serializedObject = serialize($obj);
  32. $this->reset();
  33. $this->servicename = $this->serviceName;
  34. $this->command = base64_encode($serializedObject);
  35. $this->save();
  36. }
  37. /**
  38. * Retrieve and unserialize current object
  39. *
  40. * @return mixed
  41. */
  42. public function getCommandObject() {
  43. $decodedObj = base64_decode($this->command);
  44. $obj = unserialize($decodedObj);
  45. return $obj;
  46. }
  47. /**
  48. * Retrieve messages
  49. *
  50. * @return array|FALSE
  51. */
  52. public function check()
  53. {
  54. if (!$this->initLoad) {
  55. $this->initLoad = true;
  56. $this->load(['servicename=?', $this->serviceName]);
  57. }
  58. return $this->dry();
  59. }
  60. /**
  61. * Delete all messages of <$serviceName>
  62. */
  63. public function deleteAll()
  64. {
  65. $this->reset();
  66. $this->erase(['servicename=?', $this->serviceName]);
  67. }
  68. }