JobStatus.php 2.4 KB

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