Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Data.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Captcha\Helper;
7 
11 
19 {
23  const INPUT_NAME_FIELD_VALUE = 'captcha';
24 
28  const MODE_ALWAYS = 'always';
29 
33  const MODE_AFTER_FAIL = 'after_fail';
34 
38  const XML_PATH_CAPTCHA_FONTS = 'captcha/fonts';
39 
43  const DEFAULT_CAPTCHA_TYPE = 'Zend';
44 
49  protected $_captcha = [];
50 
54  protected $_filesystem;
55 
59  protected $_storeManager;
60 
64  protected $_factory;
65 
72  public function __construct(
73  \Magento\Framework\App\Helper\Context $context,
74  \Magento\Store\Model\StoreManagerInterface $storeManager,
76  \Magento\Captcha\Model\CaptchaFactory $factory
77  ) {
78  $this->_storeManager = $storeManager;
79  $this->_filesystem = $filesystem;
80  $this->_factory = $factory;
81  parent::__construct($context);
82  }
83 
90  public function getCaptcha($formId)
91  {
92  if (!array_key_exists($formId, $this->_captcha)) {
93  $captchaType = ucfirst($this->getConfig('type'));
94  if (!$captchaType) {
95  $captchaType = self::DEFAULT_CAPTCHA_TYPE;
96  } elseif ($captchaType == 'Default') {
97  $captchaType = $captchaType . 'Model';
98  }
99 
100  $this->_captcha[$formId] = $this->_factory->create($captchaType, $formId);
101  }
102  return $this->_captcha[$formId];
103  }
104 
112  public function getConfig($key, $store = null)
113  {
114  return $this->scopeConfig->getValue(
115  'customer/captcha/' . $key,
116  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
117  $store
118  );
119  }
120 
129  public function getFonts()
130  {
131  $fontsConfig = $this->scopeConfig->getValue(\Magento\Captcha\Helper\Data::XML_PATH_CAPTCHA_FONTS, 'default');
132  $fonts = [];
133  if ($fontsConfig) {
134  $libDir = $this->_filesystem->getDirectoryRead(DirectoryList::LIB_INTERNAL);
135  foreach ($fontsConfig as $fontName => $fontConfig) {
136  $fonts[$fontName] = [
137  'label' => $fontConfig['label'],
138  'path' => $libDir->getAbsolutePath($fontConfig['path']),
139  ];
140  }
141  }
142  return $fonts;
143  }
144 
151  public function getImgDir($website = null)
152  {
153  $mediaDir = $this->_filesystem->getDirectoryWrite(DirectoryList::MEDIA);
154  $captchaDir = '/captcha/' . $this->_getWebsiteCode($website);
155  $mediaDir->create($captchaDir);
156  return $mediaDir->getAbsolutePath($captchaDir) . '/';
157  }
158 
165  protected function _getWebsiteCode($website = null)
166  {
167  return $this->_storeManager->getWebsite($website)->getCode();
168  }
169 
176  public function getImgUrl($website = null)
177  {
178  return $this->_storeManager->getStore()->getBaseUrl(
180  ) . 'captcha' . '/' . $this->_getWebsiteCode(
181  $website
182  ) . '/';
183  }
184 }
getImgDir($website=null)
Definition: Data.php:151
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__construct(\Magento\Framework\App\Helper\Context $context, \Magento\Store\Model\StoreManagerInterface $storeManager, Filesystem $filesystem, \Magento\Captcha\Model\CaptchaFactory $factory)
Definition: Data.php:72
$storeManager
getImgUrl($website=null)
Definition: Data.php:176
getConfig($key, $store=null)
Definition: Data.php:112
$filesystem
_getWebsiteCode($website=null)
Definition: Data.php:165