MultiExecState.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Predis\Transaction;
  11. /**
  12. * Utility class used to track the state of a MULTI / EXEC transaction.
  13. *
  14. * @author Daniele Alessandri <suppakilla@gmail.com>
  15. */
  16. class MultiExecState
  17. {
  18. const INITIALIZED = 1; // 0b00001
  19. const INSIDEBLOCK = 2; // 0b00010
  20. const DISCARDED = 4; // 0b00100
  21. const CAS = 8; // 0b01000
  22. const WATCH = 16; // 0b10000
  23. private $flags;
  24. /**
  25. *
  26. */
  27. public function __construct()
  28. {
  29. $this->flags = 0;
  30. }
  31. /**
  32. * Sets the internal state flags.
  33. *
  34. * @param int $flags Set of flags
  35. */
  36. public function set($flags)
  37. {
  38. $this->flags = $flags;
  39. }
  40. /**
  41. * Gets the internal state flags.
  42. *
  43. * @return int
  44. */
  45. public function get()
  46. {
  47. return $this->flags;
  48. }
  49. /**
  50. * Sets one or more flags.
  51. *
  52. * @param int $flags Set of flags
  53. */
  54. public function flag($flags)
  55. {
  56. $this->flags |= $flags;
  57. }
  58. /**
  59. * Resets one or more flags.
  60. *
  61. * @param int $flags Set of flags
  62. */
  63. public function unflag($flags)
  64. {
  65. $this->flags &= ~$flags;
  66. }
  67. /**
  68. * Returns if the specified flag or set of flags is set.
  69. *
  70. * @param int $flags Flag
  71. *
  72. * @return bool
  73. */
  74. public function check($flags)
  75. {
  76. return ($this->flags & $flags) === $flags;
  77. }
  78. /**
  79. * Resets the state of a transaction.
  80. */
  81. public function reset()
  82. {
  83. $this->flags = 0;
  84. }
  85. /**
  86. * Returns the state of the RESET flag.
  87. *
  88. * @return bool
  89. */
  90. public function isReset()
  91. {
  92. return $this->flags === 0;
  93. }
  94. /**
  95. * Returns the state of the INITIALIZED flag.
  96. *
  97. * @return bool
  98. */
  99. public function isInitialized()
  100. {
  101. return $this->check(self::INITIALIZED);
  102. }
  103. /**
  104. * Returns the state of the INSIDEBLOCK flag.
  105. *
  106. * @return bool
  107. */
  108. public function isExecuting()
  109. {
  110. return $this->check(self::INSIDEBLOCK);
  111. }
  112. /**
  113. * Returns the state of the CAS flag.
  114. *
  115. * @return bool
  116. */
  117. public function isCAS()
  118. {
  119. return $this->check(self::CAS);
  120. }
  121. /**
  122. * Returns if WATCH is allowed in the current state.
  123. *
  124. * @return bool
  125. */
  126. public function isWatchAllowed()
  127. {
  128. return $this->check(self::INITIALIZED) && !$this->check(self::CAS);
  129. }
  130. /**
  131. * Returns the state of the WATCH flag.
  132. *
  133. * @return bool
  134. */
  135. public function isWatching()
  136. {
  137. return $this->check(self::WATCH);
  138. }
  139. /**
  140. * Returns the state of the DISCARDED flag.
  141. *
  142. * @return bool
  143. */
  144. public function isDiscarded()
  145. {
  146. return $this->check(self::DISCARDED);
  147. }
  148. }