JobClass.php 4.0 KB

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