Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. <?php
  2. /**
  3. * FireSync Observer
  4. *
  5. * Watch for changes and replicate them to the Firebase database
  6. *
  7. * PHP Version 7.1
  8. *
  9. * @category Observer
  10. * @package Firebase
  11. * @license GPLv3.0 GNU Public License v3.0
  12. */
  13. namespace App\Observers;
  14.  
  15. use App\FireSync;
  16. use Kreait\Firebase\Factory;
  17. use Illuminate\Support\Facades\Log;
  18.  
  19. /**
  20. * FireSync Observer
  21. *
  22. * Watch for changes and replicate them to the Firebase database
  23. *
  24. * @category Observer
  25. * @package Firebase
  26. * @license GPLv3.0 GNU Public License v3.0
  27. */
  28. class FireSyncObserver
  29. {
  30. private $_database;
  31. private $_baseReference = 'recordset/records';
  32. private $_reference;
  33.  
  34. /**
  35. * Constructor
  36. */
  37. public function __construct()
  38. {
  39. $this->_database = (new Factory)->createDatabase();
  40. $this->_reference = $this->_database->getReference($this->_baseReference);
  41. }
  42.  
  43. /**
  44. * Created event
  45. *
  46. * Takes the newly created row and adds it to the firebase record set
  47. *
  48. * @param FireSync $fireSync The Database model
  49. *
  50. * @return void
  51. */
  52. public function created(FireSync $fireSync)
  53. {
  54. $row = $fireSync->toArray();
  55. $pushKey = $this->_database->getReference(
  56. $this->_baseReference
  57. )
  58. ->push($row)->getKey();
  59.  
  60. Log::debug('Create called for ' . $pushKey);
  61. $fireSync['push_key'] = $pushKey;
  62. // Call an update of the local table to add the push_key without
  63. // triggering any updated event.
  64. FireSync::withoutEvents(
  65. function () use ($fireSync) {
  66. $fireSync->save();
  67. }
  68. );
  69. }
  70.  
  71. /**
  72. * Updated Event
  73. *
  74. * When a row is updated update the firebase record set to match.
  75. * Fairly lazy as it updates ALL columns, not just those that have changed.
  76. *
  77. * @param FireSync $fireSync Database model
  78. *
  79. * @return void
  80. */
  81. public function updated(FireSync $fireSync)
  82. {
  83. if ($fireSync['push_key'] !== null) {
  84. Log::debug('Update called for ' . $fireSync['push_key']);
  85. // Grab all the columns except push_key
  86. $row = $fireSync->makeHidden('push_key')->toArray();
  87. $this->_database->getReference(
  88. $this->_baseReference . '/' . $fireSync['push_key']
  89. )
  90. ->set(
  91. $row
  92. );
  93. }
  94. }
  95.  
  96. /**
  97. * Deleted Event
  98. *
  99. * When a row is deleted remote the matching model from firebase.
  100. *
  101. * @param FireSync $fireSync Database model
  102. *
  103. * @return void
  104. */
  105. public function deleted(FireSync $fireSync)
  106. {
  107. if ($fireSync['push_key'] !== null) {
  108. Log::debug('Delete called for ' . $fireSync['push_key']);
  109. $this->_database->getReference(
  110. $this->_baseReference . '/' . $fireSync['push_key']
  111. )
  112. ->remove();
  113. }
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement