MultiExecState.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. * @return bool
  72. */
  73. public function check($flags)
  74. {
  75. return ($this->flags & $flags) === $flags;
  76. }
  77. /**
  78. * Resets the state of a transaction.
  79. */
  80. public function reset()
  81. {
  82. $this->flags = 0;
  83. }
  84. /**
  85. * Returns the state of the RESET flag.
  86. *
  87. * @return bool
  88. */
  89. public function isReset()
  90. {
  91. return $this->flags === 0;
  92. }
  93. /**
  94. * Returns the state of the INITIALIZED flag.
  95. *
  96. * @return bool
  97. */
  98. public function isInitialized()
  99. {
  100. return $this->check(self::INITIALIZED);
  101. }
  102. /**
  103. * Returns the state of the INSIDEBLOCK flag.
  104. *
  105. * @return bool
  106. */
  107. public function isExecuting()
  108. {
  109. return $this->check(self::INSIDEBLOCK);
  110. }
  111. /**
  112. * Returns the state of the CAS flag.
  113. *
  114. * @return bool
  115. */
  116. public function isCAS()
  117. {
  118. return $this->check(self::CAS);
  119. }
  120. /**
  121. * Returns if WATCH is allowed in the current state.
  122. *
  123. * @return bool
  124. */
  125. public function isWatchAllowed()
  126. {
  127. return $this->check(self::INITIALIZED) && !$this->check(self::CAS);
  128. }
  129. /**
  130. * Returns the state of the WATCH flag.
  131. *
  132. * @return bool
  133. */
  134. public function isWatching()
  135. {
  136. return $this->check(self::WATCH);
  137. }
  138. /**
  139. * Returns the state of the DISCARDED flag.
  140. *
  141. * @return bool
  142. */
  143. public function isDiscarded()
  144. {
  145. return $this->check(self::DISCARDED);
  146. }
  147. }