Handlers.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\MessageQueue\Consumer\Config\Validator;
  7. use Magento\Framework\MessageQueue\Consumer\Config\ValidatorInterface;
  8. use Magento\Framework\Reflection\MethodsMap;
  9. /**
  10. * Consumer config data validator for handlers.
  11. */
  12. class Handlers implements ValidatorInterface
  13. {
  14. /**
  15. * @var MethodsMap
  16. */
  17. private $methodsMap;
  18. /**
  19. * Initialize dependencies.
  20. *
  21. * @param MethodsMap $methodsMap
  22. */
  23. public function __construct(MethodsMap $methodsMap)
  24. {
  25. $this->methodsMap = $methodsMap;
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function validate($configData)
  31. {
  32. foreach ($configData as $consumerConfig) {
  33. $consumerName = $consumerConfig['name'];
  34. if (!is_array($consumerConfig['handlers'])) {
  35. throw new \LogicException(
  36. sprintf(
  37. "'handlers' element must be an array for consumer '%s'",
  38. $consumerName
  39. )
  40. );
  41. }
  42. foreach ($consumerConfig['handlers'] as $handler) {
  43. $this->validateHandler($handler, $consumerName);
  44. }
  45. }
  46. }
  47. /**
  48. * Validate handler configuration.
  49. *
  50. * @param array $handler
  51. * @param string $consumerName
  52. * @return void
  53. * @throws \LogicException
  54. */
  55. private function validateHandler($handler, $consumerName)
  56. {
  57. if (!isset($handler['type']) || !isset($handler['method'])) {
  58. throw new \LogicException(
  59. sprintf(
  60. "'%s' consumer declaration is invalid. "
  61. . "Every handler element must be an array. It must contain 'type' and 'method' elements.",
  62. $consumerName
  63. )
  64. );
  65. }
  66. try {
  67. $this->methodsMap->getMethodParams($handler['type'], $handler['method']);
  68. } catch (\Exception $e) {
  69. throw new \LogicException(
  70. sprintf(
  71. 'Service method specified as handler for of consumer "%s" is not available. Given "%s"',
  72. $consumerName,
  73. $handler['type'] . '::' . $handler['method']
  74. )
  75. );
  76. }
  77. }
  78. }