Skip to content

Commit 5c21dff

Browse files
committed
AC-520: Oauth token improvements.
Refactoring the patch scripts.
1 parent e153c0c commit 5c21dff

File tree

2 files changed

+45
-33
lines changed

2 files changed

+45
-33
lines changed

app/code/Magento/Integration/Setup/Patch/Data/UpgradeConsumerSecret.php

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -82,26 +82,32 @@ public function apply()
8282
$this->consumerCollection->addFieldToSelect('entity_id');
8383
$this->consumerCollection->addFieldToSelect('secret');
8484
$connection = $this->consumerResourceModel->getConnection();
85-
$this->consumerCollection->getSelect()->limit(self::BATCH_SIZE);
86-
$collectionItems = $this->consumerCollection->getItems();
85+
$this->consumerCollection->setPageSize(self::BATCH_SIZE);
86+
$pages = $this->consumerCollection->getLastPageNumber();
87+
$tableName = $this->consumerResourceModel->getMainTable();
88+
89+
for ($currentPage = 1; $currentPage <= $pages; $currentPage++) {
90+
$this->consumerCollection->setCurPage($currentPage);
8791

8892
/** @var $consumer Consumer */
89-
foreach ($collectionItems as $consumer) {
90-
$existingSecret = $consumer->getSecret();
91-
$entityId = $consumer->getEntityId();
92-
93-
if ($entityId && $existingSecret) {
94-
if (strlen($existingSecret) <= OauthHelper::LENGTH_TOKEN_SECRET) {
95-
$data = ['secret' => $this->encryptor->encrypt($existingSecret)];
96-
$where = ['entity_id = ?' => $entityId];
97-
try {
98-
$connection->update($this->consumerResourceModel->getMainTable(), $data, $where);
99-
} catch (\Exception $exception) {
100-
$this->logger->critical($exception->getMessage());
101-
return $this;
93+
foreach ($this->consumerCollection as $consumer) {
94+
$existingSecret = $consumer->getSecret();
95+
$entityId = $consumer->getEntityId();
96+
97+
if ($entityId && $existingSecret) {
98+
if (strlen($existingSecret) <= OauthHelper::LENGTH_TOKEN_SECRET) {
99+
$data = ['secret' => $this->encryptor->encrypt($existingSecret)];
100+
$where = ['entity_id = ?' => $entityId];
101+
try {
102+
$connection->update($tableName, $data, $where);
103+
} catch (\Exception $exception) {
104+
$this->logger->critical($exception->getMessage());
105+
return $this;
106+
}
102107
}
103108
}
104109
}
110+
$this->consumerCollection->clear();
105111
}
106112

107113
return $this;

app/code/Magento/Integration/Setup/Patch/Data/UpgradeOauthToken.php

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -84,28 +84,34 @@ public function apply()
8484
$this->tokenCollection->addFieldToSelect('entity_id');
8585
$this->tokenCollection->addFieldToSelect('secret');
8686
$this->tokenCollection->addFieldToSelect('type');
87-
$this->tokenCollection->getSelect()->limit(self::BATCH_SIZE);
88-
$collectionItems = $this->tokenCollection->getItems();
87+
$this->tokenCollection->setPageSize(self::BATCH_SIZE);
8988
$connection = $this->tokenResourceModel->getConnection();
90-
91-
/** @var $token Token */
92-
foreach ($collectionItems as $token) {
93-
$existingSecret = $token->getSecret();
94-
$entityId = $token->getEntityId();
95-
$type = strtolower($token->getType());
96-
97-
if ($entityId && $existingSecret && $type === TokenModel::TYPE_ACCESS) {
98-
if (strlen($existingSecret) <= OauthHelper::LENGTH_TOKEN_SECRET) {
99-
$data = ['secret' => $this->encryptor->encrypt($existingSecret)];
100-
$where = ['entity_id = ?' => $entityId, 'type = ?' => 'access'];
101-
try {
102-
$connection->update($this->tokenResourceModel->getMainTable(), $data, $where);
103-
} catch (\Exception $exception) {
104-
$this->logger->critical($exception->getMessage());
105-
return $this;
89+
$pages = $this->tokenCollection->getLastPageNumber();
90+
$tableName = $this->tokenResourceModel->getMainTable();
91+
92+
for ($currentPage = 1; $currentPage <= $pages; $currentPage++) {
93+
$this->tokenCollection->setCurPage($currentPage);
94+
95+
/** @var $token Token */
96+
foreach ($this->tokenCollection as $token) {
97+
$existingSecret = $token->getSecret();
98+
$entityId = $token->getEntityId();
99+
$type = strtolower($token->getType());
100+
101+
if ($entityId && $existingSecret && $type === TokenModel::TYPE_ACCESS) {
102+
if (strlen($existingSecret) <= OauthHelper::LENGTH_TOKEN_SECRET) {
103+
$data = ['secret' => $this->encryptor->encrypt($existingSecret)];
104+
$where = ['entity_id = ?' => $entityId, 'type = ?' => 'access'];
105+
try {
106+
$connection->update($tableName, $data, $where);
107+
} catch (\Exception $exception) {
108+
$this->logger->critical($exception->getMessage());
109+
return $this;
110+
}
106111
}
107112
}
108113
}
114+
$this->tokenCollection->clear();
109115
}
110116
return $this;
111117
}

0 commit comments

Comments
 (0)