CaptchaFactory.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. /**
  3. * Captcha model factory
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Captcha\Model;
  9. class CaptchaFactory
  10. {
  11. /**
  12. * @var \Magento\Framework\ObjectManagerInterface
  13. */
  14. protected $_objectManager;
  15. /**
  16. * @param \Magento\Framework\ObjectManagerInterface $objectManager
  17. */
  18. public function __construct(\Magento\Framework\ObjectManagerInterface $objectManager)
  19. {
  20. $this->_objectManager = $objectManager;
  21. }
  22. /**
  23. * Get captcha instance
  24. *
  25. * @param string $captchaType
  26. * @param string $formId
  27. * @return \Magento\Captcha\Model\CaptchaInterface
  28. * @throws \InvalidArgumentException
  29. */
  30. public function create($captchaType, $formId)
  31. {
  32. $className = 'Magento\Captcha\Model\\' . ucfirst($captchaType);
  33. $instance = $this->_objectManager->create($className, ['formId' => $formId]);
  34. if (!$instance instanceof \Magento\Captcha\Model\CaptchaInterface) {
  35. throw new \InvalidArgumentException(
  36. $className . ' does not implement \Magento\Captcha\Model\CaptchaInterface'
  37. );
  38. }
  39. return $instance;
  40. }
  41. }