QueueSystem.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. /** @var \Base $f3 */
  13. private $f3;
  14. private $serviceName;
  15. // Used internally
  16. private $initLoad;
  17. public function __construct(string $serviceName)
  18. {
  19. global $f3;
  20. $this->f3 = $f3;
  21. parent::__construct($f3->get('DB'), 'queuesystem');
  22. $this->serviceName = $serviceName;
  23. }
  24. /**
  25. * Send object to <$serviceName>.
  26. * send() $obj will be serialized before send to queue
  27. *
  28. * @param $obj
  29. */
  30. public function send($obj)
  31. {
  32. $serializedObject = serialize($obj);
  33. $this->reset();
  34. $this->servicename = $this->serviceName;
  35. $this->command = base64_encode($serializedObject);
  36. $this->save();
  37. }
  38. public function getCommandObject() {
  39. $decodedObj = base64_decode($this->command);
  40. $obj = unserialize($decodedObj);
  41. return $obj;
  42. }
  43. /**
  44. * Retrieve messages
  45. *
  46. * @return array|FALSE
  47. */
  48. public function check()
  49. {
  50. if (!$this->initLoad) {
  51. $this->initLoad = true;
  52. $this->load(['servicename=?', $this->serviceName]);
  53. }
  54. return $this->dry();
  55. }
  56. /**
  57. * Delete all messages of <$serviceName>
  58. */
  59. public function deleteAll()
  60. {
  61. $this->reset();
  62. $this->erase(['servicename=?', $this->serviceName]);
  63. }
  64. }