JobClass.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace Mmoreramerino\GearmanBundle\Module;
  3. use Mmoreramerino\GearmanBundle\Driver\Gearman\Job;
  4. use Mmoreramerino\GearmanBundle\Driver\Gearman\Work;
  5. use Mmoreramerino\GearmanBundle\Exceptions\SettingValueMissingException;
  6. /**
  7. * Job class
  8. *
  9. * @author Marc Morera <marc@ulabox.com>
  10. */
  11. class JobClass
  12. {
  13. /**
  14. * Callable name for this job
  15. * If is setted on annotations, this value will be used
  16. * otherwise, natural method name will be used.
  17. *
  18. * @var string
  19. */
  20. private $callableName;
  21. /**
  22. * Description of Job
  23. *
  24. * @var string
  25. */
  26. private $description;
  27. public function __construct( Job $methodAnnotation, \ReflectionMethod $method, Work $classAnnotation, $callableNameClass, array $settings)
  28. {
  29. $this->callableName = (null !== $methodAnnotation->name) ?
  30. $methodAnnotation->name :
  31. $method->getName();
  32. $this->methodName = $method->getName();
  33. $this->realCallableName = $callableNameClass.'~'.$this->callableName;
  34. $this->description = (null !== $method->getDocComment()) ?
  35. $methodAnnotation->description :
  36. 'No description is defined';
  37. /**
  38. * Iterations definition for job
  39. */
  40. if (null !== $settings['defaults']['iter']) {
  41. $iter = (int)($settings['defaults']['iter']);
  42. if (null !== $classAnnotation->iter) {
  43. $iter = (int)($classAnnotation->iter);
  44. }
  45. if (null !== $methodAnnotation->iter) {
  46. $iter = (int)($methodAnnotation->iter);
  47. }
  48. } else {
  49. throw new SettingValueMissingException('defaults/iter');
  50. }
  51. $this->iter = $iter;
  52. /**
  53. * Servers definition for job
  54. */
  55. if (null !== $settings['defaults']['servers']) {
  56. if (is_array($settings['defaults']['servers'])) {
  57. $servers = $settings['defaults']['servers'];
  58. } else {
  59. $servers = array($settings['defaults']['servers']);
  60. }
  61. if (null !== $classAnnotation->servers) {
  62. if (is_array($classAnnotation->servers)) {
  63. $servers = $classAnnotation->servers;
  64. } else {
  65. $servers = array($classAnnotation->servers);
  66. }
  67. }
  68. if (null !== $methodAnnotation->servers) {
  69. if (is_array($methodAnnotation->servers)) {
  70. $servers = $methodAnnotation->servers;
  71. } else {
  72. $servers = array($methodAnnotation->servers);
  73. }
  74. }
  75. } else {
  76. throw new SettingValueMissingException('defaults/servers');
  77. }
  78. $this->servers = $servers;
  79. }
  80. /**
  81. * Retrieve all Job data in cache format
  82. *
  83. * @return array
  84. */
  85. public function __toCache()
  86. {
  87. return array(
  88. 'callableName' => $this->callableName,
  89. 'methodName' => $this->methodName,
  90. 'realCallableName' => $this->realCallableName,
  91. 'description' => $this->description,
  92. 'iter' => $this->iter,
  93. 'servers' => $this->servers,
  94. );
  95. }
  96. }