AbstractGearmanService.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace Mmoreram\GearmanBundle\Service\Abstracts;
  3. use Mmoreram\GearmanBundle\Service\GearmanCacheWrapper;
  4. use Mmoreram\GearmanBundle\Exceptions\JobDoesNotExistException;
  5. use Mmoreram\GearmanBundle\Exceptions\WorkerDoesNotExistException;
  6. /**
  7. * Gearman execute methods. All Worker methods
  8. *
  9. * @author Marc Morera <yuhu@mmoreram.com>
  10. */
  11. abstract class AbstractGearmanService
  12. {
  13. /**
  14. * All workers
  15. *
  16. * @var type
  17. */
  18. protected $workers;
  19. /**
  20. * Construct method
  21. *
  22. * @param GearmanCacheWrapper $gearmanCacheWrapper GearmanCacheWrapper
  23. */
  24. public function __construct(GearmanCacheWrapper $gearmanCacheWrapper)
  25. {
  26. $this->workers = $gearmanCacheWrapper->getWorkers();
  27. }
  28. /**
  29. * Return worker containing a job with $jobName as name
  30. * If is not found, throws JobDoesNotExistException Exception
  31. *
  32. * @param string $jobName Name of job
  33. *
  34. * @return Array
  35. */
  36. public function getJob($jobName)
  37. {
  38. foreach ($this->workers as $worker) {
  39. if (is_array($worker['jobs'])) {
  40. foreach ($worker['jobs'] as $job) {
  41. if ($jobName === $job['realCallableName']) {
  42. $worker['job'] = $job;
  43. return $worker;
  44. }
  45. }
  46. }
  47. }
  48. throw new JobDoesNotExistException($jobName);
  49. }
  50. /**
  51. * Return worker with $workerName as name and all its jobs
  52. * If is not found, throws WorkerDoesNotExistException Exception
  53. *
  54. * @param string $workerName Name of worker
  55. *
  56. * @return Array
  57. */
  58. public function getWorker($workerName)
  59. {
  60. foreach ($this->workers as $worker) {
  61. if ($workerName === $worker['callableName']) {
  62. return $worker;
  63. }
  64. }
  65. throw new WorkerDoesNotExistException($workerName);
  66. }
  67. /**
  68. * Return array of workers
  69. *
  70. * @return array all available workers
  71. */
  72. public function getWorkers()
  73. {
  74. return $this->workers;
  75. }
  76. }