77
88namespace Magento \Config \Model \Data \ReEncryptorList \CoreConfigDataReEncryptor ;
99
10- use Magento \Framework \App \Config \Initial ;
1110use Magento \Framework \App \ResourceConnection ;
12- use Magento \Config \Model \Config \Backend \Encrypted ;
1311use Magento \Framework \Encryption \EncryptorInterface ;
1412use Magento \EncryptionKey \Model \Data \ReEncryptorList \ReEncryptor \HandlerInterface ;
1513use Magento \EncryptionKey \Model \Data \ReEncryptorList \ReEncryptor \Handler \ErrorFactory ;
@@ -22,17 +20,17 @@ class Handler implements HandlerInterface
2220 /**
2321 * @var string
2422 */
25- private const TABLE_NAME = "core_config_data " ;
23+ private const PATTERN = "^[[:digit:]]+:[[:digit:]]+:.*$ " ;
2624
2725 /**
2826 * @var string
2927 */
30- private const BACKEND_MODEL = Encrypted::class ;
28+ private const TABLE_NAME = " core_config_data " ;
3129
3230 /**
33- * @var Initial
31+ * @var int
3432 */
35- private Initial $ config ;
33+ private const QUERY_SIZE = 1000 ;
3634
3735 /**
3836 * @var EncryptorInterface
@@ -50,18 +48,15 @@ class Handler implements HandlerInterface
5048 private ErrorFactory $ errorFactory ;
5149
5250 /**
53- * @param Initial $config
5451 * @param EncryptorInterface $encryptor
5552 * @param ResourceConnection $resourceConnection
5653 * @param ErrorFactory $errorFactory
5754 */
5855 public function __construct (
59- Initial $ config ,
6056 EncryptorInterface $ encryptor ,
6157 ResourceConnection $ resourceConnection ,
6258 ErrorFactory $ errorFactory
6359 ) {
64- $ this ->config = $ config ;
6560 $ this ->encryptor = $ encryptor ;
6661 $ this ->resourceConnection = $ resourceConnection ;
6762 $ this ->errorFactory = $ errorFactory ;
@@ -72,46 +67,34 @@ public function __construct(
7267 */
7368 public function reEncrypt (): array
7469 {
75- $ paths = [];
7670 $ errors = [];
77-
78- foreach ($ this ->config ->getMetadata () as $ path => $ processor ) {
79- if (isset ($ processor ['backendModel ' ]) &&
80- $ processor ['backendModel ' ] === self ::BACKEND_MODEL
81- ) {
82- $ paths [] = $ path ;
83- }
84- }
85-
86- if ($ paths ) {
87- $ tableName = $ this ->resourceConnection ->getTableName (
88- self ::TABLE_NAME
89- );
90-
91- $ connection = $ this ->resourceConnection ->getConnection ();
92-
93- $ select = $ connection ->select ()
94- ->from ($ tableName , ['config_id ' , 'value ' ])
95- ->where ('path IN (?) ' , $ paths )
96- ->where ('value != ? ' , '' )
97- ->where ('value IS NOT NULL ' );
98-
99- foreach ($ connection ->fetchPairs ($ select ) as $ configId => $ value ) {
100- try {
101- $ connection ->update (
102- $ tableName ,
103- ['value ' => $ this ->encryptor ->encrypt ($ this ->encryptor ->decrypt ($ value ))],
104- ['config_id = ? ' => (int ) $ configId ]
105- );
106- } catch (\Throwable $ e ) {
107- $ errors [] = $ this ->errorFactory ->create (
108- "config_id " ,
109- $ configId ,
110- $ e ->getMessage ()
111- );
112-
113- continue ;
114- }
71+ $ tableName = $ this ->resourceConnection ->getTableName (
72+ self ::TABLE_NAME
73+ );
74+ $ connection = $ this ->resourceConnection ->getConnection ();
75+
76+ $ select = $ connection ->select ()
77+ ->from ($ tableName , ['config_id ' , 'value ' ])
78+ ->where ('value != ? ' , '' )
79+ ->where ('value IS NOT NULL ' )
80+ ->where ('value REGEXP ? ' , self ::PATTERN )
81+ ->limit (self ::QUERY_SIZE );
82+
83+ foreach ($ connection ->fetchPairs ($ select ) as $ configId => $ value ) {
84+ try {
85+ $ connection ->update (
86+ $ tableName ,
87+ ['value ' => $ this ->encryptor ->encrypt ($ this ->encryptor ->decrypt ($ value ))],
88+ ['config_id = ? ' => (int )$ configId ]
89+ );
90+ } catch (\Throwable $ e ) {
91+ $ errors [] = $ this ->errorFactory ->create (
92+ "config_id " ,
93+ $ configId ,
94+ $ e ->getMessage ()
95+ );
96+
97+ continue ;
11598 }
11699 }
117100
0 commit comments