Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
EncryptionUpdate.php
Go to the documentation of this file.
1 <?php
7 declare(strict_types=1);
8 
10 
15 {
16  const LEGACY_PATTERN = '^[[:digit:]]+:[^%s]:.*$';
17 
21  private $paymentResource;
22 
26  private $encryptor;
27 
32  public function __construct(
33  \Magento\Sales\Model\ResourceModel\Order\Payment $paymentResource,
34  \Magento\Framework\Encryption\Encryptor $encryptor
35  ) {
36  $this->paymentResource = $paymentResource;
37  $this->encryptor = $encryptor;
38  }
39 
44  public function reEncryptCreditCardNumbers()
45  {
46  $connection = $this->paymentResource->getConnection();
47  $table = $this->paymentResource->getMainTable();
48  $select = $connection->select()->from($table, ['entity_id', 'cc_number_enc'])
49  ->where(
50  'cc_number_enc REGEXP ?',
51  sprintf(self::LEGACY_PATTERN, \Magento\Framework\Encryption\Encryptor::CIPHER_LATEST)
52  )->limit(1000);
53 
54  while ($attributeValues = $connection->fetchPairs($select)) {
55  // save new values
56  foreach ($attributeValues as $valueId => $value) {
57  $connection->update(
58  $table,
59  ['cc_number_enc' => $this->encryptor->encrypt($this->encryptor->decrypt($value))],
60  ['entity_id = ?' => (int)$valueId, 'cc_number_enc = ?' => (string)$value]
61  );
62  }
63  }
64  }
65 }
__construct(\Magento\Sales\Model\ResourceModel\Order\Payment $paymentResource, \Magento\Framework\Encryption\Encryptor $encryptor)
$value
Definition: gender.phtml:16
$connection
Definition: bulk.php:13
$table
Definition: trigger.php:14