JobStatus.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * Gearman Bundle for Symfony2
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. *
  8. * Feel free to edit as you please, and have fun.
  9. *
  10. * @author Marc Morera <yuhu@mmoreram.com>
  11. */
  12. namespace Mmoreram\GearmanBundle\Module;
  13. /**
  14. * Job status class
  15. *
  16. * @since 2.3.1
  17. */
  18. class JobStatus
  19. {
  20. /**
  21. * @var boolean
  22. *
  23. * Job is known
  24. */
  25. private $known;
  26. /**
  27. * @var boolean
  28. *
  29. * Job is running
  30. */
  31. private $running;
  32. /**
  33. * @var Integer
  34. *
  35. * Job completition
  36. */
  37. private $completed;
  38. /**
  39. * @var integer
  40. *
  41. * Job completition total
  42. */
  43. private $completionTotal;
  44. /**
  45. * Construct method
  46. *
  47. * @param array $response Response to parse
  48. */
  49. public function __construct(array $response)
  50. {
  51. $this->known = (isset($response[0]) && $response[0]);
  52. $this->running = (isset($response[1]) && $response[1]);
  53. $this->completed = (isset($response[2]) && !$response[2])
  54. ? 0
  55. : $response[2];
  56. $this->completionTotal = (isset($response[3]) && !$response[3])
  57. ? 0
  58. : $response[3];
  59. }
  60. /**
  61. * Return if job is known
  62. *
  63. * @return boolean Job is still known
  64. */
  65. public function isKnown()
  66. {
  67. return $this->known;
  68. }
  69. /**
  70. * Return if job is still running
  71. *
  72. * @return boolean Jon is still running
  73. */
  74. public function isRunning()
  75. {
  76. return $this->running;
  77. }
  78. /**
  79. * Return completed value
  80. *
  81. * @return integer Completed
  82. */
  83. public function getCompleted()
  84. {
  85. return $this->completed;
  86. }
  87. /**
  88. * Return completition total
  89. *
  90. * @return integer Completition total
  91. */
  92. public function getCompletionTotal()
  93. {
  94. return $this->completionTotal;
  95. }
  96. /**
  97. * Return percent completed.
  98. *
  99. * 0 is not started or not known
  100. * 1 is finished
  101. * Between 0 and 1 is in process. Value is a float
  102. *
  103. * @return float Percent completed
  104. */
  105. public function getCompletionPercent()
  106. {
  107. $percent = 0;
  108. if (($this->completed > 0) && ($this->completionTotal > 0)) {
  109. $percent = $this->completed / $this->completionTotal;
  110. }
  111. return $percent;
  112. }
  113. /**
  114. * Return if job is still running
  115. *
  116. * @return boolean Jon is still running
  117. */
  118. public function isFinished()
  119. {
  120. return $this->isKnown() && !$this->isRunning() && $this->getCompletionPercent() == 1;
  121. }
  122. }