|
3 | 3 | * Copyright © Magento, Inc. All rights reserved.
|
4 | 4 | * See COPYING.txt for license details.
|
5 | 5 | */
|
| 6 | +declare(strict_types=1); |
| 7 | + |
6 | 8 | namespace Magento\Captcha\Model;
|
7 | 9 |
|
8 | 10 | use Magento\Captcha\Helper\Data;
|
| 11 | +use Magento\Framework\Math\Random; |
9 | 12 |
|
10 | 13 | /**
|
11 | 14 | * Implementation of \Zend\Captcha\Image
|
12 | 15 | *
|
| 16 | + * @SuppressWarnings(PHPMD.CookieAndSessionMisuse) |
| 17 | + * |
13 | 18 | * @api
|
14 | 19 | * @since 100.0.2
|
15 | 20 | */
|
@@ -78,24 +83,32 @@ class DefaultModel extends \Zend\Captcha\Image implements \Magento\Captcha\Model
|
78 | 83 | */
|
79 | 84 | protected $session;
|
80 | 85 |
|
| 86 | + /** |
| 87 | + * @var Random |
| 88 | + */ |
| 89 | + private $randomMath; |
| 90 | + |
81 | 91 | /**
|
82 | 92 | * @param \Magento\Framework\Session\SessionManagerInterface $session
|
83 | 93 | * @param \Magento\Captcha\Helper\Data $captchaData
|
84 | 94 | * @param ResourceModel\LogFactory $resLogFactory
|
85 | 95 | * @param string $formId
|
| 96 | + * @param Random $randomMath |
86 | 97 | * @throws \Zend\Captcha\Exception\ExtensionNotLoadedException
|
87 | 98 | */
|
88 | 99 | public function __construct(
|
89 | 100 | \Magento\Framework\Session\SessionManagerInterface $session,
|
90 | 101 | \Magento\Captcha\Helper\Data $captchaData,
|
91 | 102 | \Magento\Captcha\Model\ResourceModel\LogFactory $resLogFactory,
|
92 |
| - $formId |
| 103 | + $formId, |
| 104 | + Random $randomMath = null |
93 | 105 | ) {
|
94 | 106 | parent::__construct();
|
95 | 107 | $this->session = $session;
|
96 | 108 | $this->captchaData = $captchaData;
|
97 | 109 | $this->resLogFactory = $resLogFactory;
|
98 | 110 | $this->formId = $formId;
|
| 111 | + $this->randomMath = $randomMath ?? \Magento\Framework\App\ObjectManager::getInstance()->get(Random::class); |
99 | 112 | }
|
100 | 113 |
|
101 | 114 | /**
|
@@ -377,23 +390,9 @@ public function setShowCaptchaInSession($value = true)
|
377 | 390 | */
|
378 | 391 | protected function generateWord()
|
379 | 392 | {
|
380 |
| - $word = ''; |
381 |
| - $symbols = $this->getSymbols(); |
| 393 | + $symbols = (string)$this->captchaData->getConfig('symbols'); |
382 | 394 | $wordLen = $this->getWordLen();
|
383 |
| - for ($i = 0; $i < $wordLen; $i++) { |
384 |
| - $word .= $symbols[array_rand($symbols)]; |
385 |
| - } |
386 |
| - return $word; |
387 |
| - } |
388 |
| - |
389 |
| - /** |
390 |
| - * Get symbols array to use for word generation |
391 |
| - * |
392 |
| - * @return array |
393 |
| - */ |
394 |
| - private function getSymbols() |
395 |
| - { |
396 |
| - return str_split((string)$this->captchaData->getConfig('symbols')); |
| 395 | + return $this->randomMath->getRandomString($wordLen, $symbols); |
397 | 396 | }
|
398 | 397 |
|
399 | 398 | /**
|
|
0 commit comments