-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LiFE data structure should be updated on setup:upgrade (#245)
* LiFE data structure should be updated on setup:upgrade * Apply suggestions from code review Co-authored-by: Victor Petryk <166521057+victor-petryk@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Victor Petryk <166521057+victor-petryk@users.noreply.github.com> --------- Co-authored-by: Pavel Bystritsky <p.bystritskyi@gmail.com> Co-authored-by: Victor Petryk <166521057+victor-petryk@users.noreply.github.com>
- Loading branch information
1 parent
1dc5a3a
commit d09a2f5
Showing
1 changed file
with
139 additions
and
0 deletions.
There are no files selected for viewing
139 changes: 139 additions & 0 deletions
139
Setup/Patch/Data/AddLifeElementsValidationValuePatch.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Bold\Checkout\Setup\Patch\Data; | ||
|
||
use Magento\Framework\Serialize\Serializer\Json; | ||
use Magento\Framework\Setup\ModuleDataSetupInterface; | ||
use Magento\Framework\Setup\Patch\DataPatchInterface; | ||
|
||
/** | ||
* Perform Life elements upgrade. | ||
*/ | ||
class AddLifeElementsValidationValuePatch implements DataPatchInterface | ||
{ | ||
private const PATH_LIFE_ELEMENTS = 'checkout/bold_checkout_life_elements/life_elements'; | ||
|
||
/** | ||
* @var ModuleDataSetupInterface | ||
*/ | ||
private $moduleDataSetup; | ||
|
||
/** | ||
* @var Json | ||
*/ | ||
private $serializer; | ||
|
||
/** | ||
* @param ModuleDataSetupInterface $moduleDataSetup | ||
* @param Json $serializer | ||
*/ | ||
public function __construct( | ||
ModuleDataSetupInterface $moduleDataSetup, | ||
Json $serializer | ||
) { | ||
$this->moduleDataSetup = $moduleDataSetup; | ||
$this->serializer = $serializer; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public static function getDependencies() | ||
{ | ||
return []; | ||
} | ||
|
||
/** | ||
* Perform Life elements upgrade. | ||
* | ||
* @return void | ||
*/ | ||
public function apply(): void | ||
{ | ||
$this->moduleDataSetup->startSetup(); | ||
|
||
$existingLifeElements = $this->getLifeElements(); | ||
$updatedLifeElements = $this->updateLifeElements($existingLifeElements); | ||
$this->saveLifeElements($updatedLifeElements); | ||
|
||
$this->moduleDataSetup->endSetup(); | ||
} | ||
|
||
/** | ||
* Get existing Life elements from database. | ||
* | ||
* @return array | ||
*/ | ||
private function getLifeElements(): array | ||
{ | ||
$connection = $this->moduleDataSetup->getConnection(); | ||
$table = $connection->getTableName('core_config_data'); | ||
$select = $connection->select()->from( | ||
$table, | ||
[ | ||
'scope', | ||
'scope_id', | ||
'path', | ||
'value', | ||
] | ||
)->where( | ||
'path = ?', | ||
self::PATH_LIFE_ELEMENTS | ||
); | ||
|
||
return $connection->fetchAll($select); | ||
} | ||
|
||
/** | ||
* Save updated Life elements to database. | ||
* | ||
* @param array $lifeElements | ||
* @return void | ||
*/ | ||
private function saveLifeElements(array $lifeElements): void | ||
{ | ||
$connection = $this->moduleDataSetup->getConnection(); | ||
$table = $connection->getTableName('core_config_data'); | ||
$connection->insertOnDuplicate( | ||
$table, | ||
$lifeElements | ||
); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getAliases() | ||
{ | ||
return []; | ||
} | ||
|
||
/** | ||
* Add to each element a 'input_regex' if it is absent. | ||
* | ||
* @param array $allLifeElements | ||
* @return array | ||
*/ | ||
private function updateLifeElements(array $allLifeElements): array | ||
{ | ||
$result = []; | ||
foreach ($allLifeElements as $scopedLifeElement) { | ||
$updated = false; | ||
$lifeElements = $this->serializer->unserialize($scopedLifeElement['value']); | ||
foreach ($lifeElements as &$lifeElement) { | ||
if (!isset($lifeElement['input_regex'])) { | ||
$lifeElement['input_regex'] = ''; | ||
$updated = true; | ||
} | ||
} | ||
if ($updated) { | ||
$scopedLifeElement['value'] = $this->serializer->serialize($lifeElements); | ||
$result[] = $scopedLifeElement; | ||
} | ||
} | ||
|
||
return $result; | ||
} | ||
} |