|
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 | */
|
@@ -83,24 +88,32 @@ class DefaultModel extends \Zend\Captcha\Image implements \Magento\Captcha\Model
|
83 | 88 | */
|
84 | 89 | private $words;
|
85 | 90 |
|
| 91 | + /** |
| 92 | + * @var Random |
| 93 | + */ |
| 94 | + private $randomMath; |
| 95 | + |
86 | 96 | /**
|
87 | 97 | * @param \Magento\Framework\Session\SessionManagerInterface $session
|
88 | 98 | * @param \Magento\Captcha\Helper\Data $captchaData
|
89 | 99 | * @param ResourceModel\LogFactory $resLogFactory
|
90 | 100 | * @param string $formId
|
| 101 | + * @param Random $randomMath |
91 | 102 | * @throws \Zend\Captcha\Exception\ExtensionNotLoadedException
|
92 | 103 | */
|
93 | 104 | public function __construct(
|
94 | 105 | \Magento\Framework\Session\SessionManagerInterface $session,
|
95 | 106 | \Magento\Captcha\Helper\Data $captchaData,
|
96 | 107 | \Magento\Captcha\Model\ResourceModel\LogFactory $resLogFactory,
|
97 |
| - $formId |
| 108 | + $formId, |
| 109 | + Random $randomMath = null |
98 | 110 | ) {
|
99 | 111 | parent::__construct();
|
100 | 112 | $this->session = $session;
|
101 | 113 | $this->captchaData = $captchaData;
|
102 | 114 | $this->resLogFactory = $resLogFactory;
|
103 | 115 | $this->formId = $formId;
|
| 116 | + $this->randomMath = $randomMath ?? \Magento\Framework\App\ObjectManager::getInstance()->get(Random::class); |
104 | 117 | }
|
105 | 118 |
|
106 | 119 | /**
|
@@ -382,23 +395,9 @@ public function setShowCaptchaInSession($value = true)
|
382 | 395 | */
|
383 | 396 | protected function generateWord()
|
384 | 397 | {
|
385 |
| - $word = ''; |
386 |
| - $symbols = $this->getSymbols(); |
| 398 | + $symbols = (string)$this->captchaData->getConfig('symbols'); |
387 | 399 | $wordLen = $this->getWordLen();
|
388 |
| - for ($i = 0; $i < $wordLen; $i++) { |
389 |
| - $word .= $symbols[array_rand($symbols)]; |
390 |
| - } |
391 |
| - return $word; |
392 |
| - } |
393 |
| - |
394 |
| - /** |
395 |
| - * Get symbols array to use for word generation |
396 |
| - * |
397 |
| - * @return array |
398 |
| - */ |
399 |
| - private function getSymbols() |
400 |
| - { |
401 |
| - return str_split((string)$this->captchaData->getConfig('symbols')); |
| 400 | + return $this->randomMath->getRandomString($wordLen, $symbols); |
402 | 401 | }
|
403 | 402 |
|
404 | 403 | /**
|
@@ -562,7 +561,7 @@ protected function randomSize()
|
562 | 561 | */
|
563 | 562 | protected function gc()
|
564 | 563 | {
|
565 |
| - //do nothing |
| 564 | + return; // required for static testing to pass |
566 | 565 | }
|
567 | 566 |
|
568 | 567 | /**
|
|
0 commit comments