init
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Agent\Setup\Patch\Data;
|
||||
|
||||
use Esun\Agent\Model\Map\CountryPosition;
|
||||
use Magento\Framework\App\ResourceConnection;
|
||||
use Magento\Framework\Setup\ModuleDataSetupInterface;
|
||||
use Magento\Framework\Setup\Patch\DataPatchInterface;
|
||||
|
||||
class CorrectSoutheastAsiaMapPositions implements DataPatchInterface
|
||||
{
|
||||
private const COUNTRY_CODES = ['ID', 'SG', 'TH', 'TW', 'VN'];
|
||||
|
||||
public function __construct(
|
||||
private readonly ModuleDataSetupInterface $moduleDataSetup,
|
||||
private readonly ResourceConnection $resourceConnection,
|
||||
private readonly CountryPosition $countryPosition
|
||||
) {
|
||||
}
|
||||
|
||||
public function apply(): self
|
||||
{
|
||||
$connection = $this->moduleDataSetup->getConnection();
|
||||
$table = $this->resourceConnection->getTableName('esun_agent');
|
||||
|
||||
$this->moduleDataSetup->startSetup();
|
||||
foreach (self::COUNTRY_CODES as $countryCode) {
|
||||
$position = $this->countryPosition->get($countryCode);
|
||||
if ($position === null) {
|
||||
continue;
|
||||
}
|
||||
$connection->update(
|
||||
$table,
|
||||
['map_x' => $position['x'], 'map_y' => $position['y']],
|
||||
['country_code = ?' => $countryCode]
|
||||
);
|
||||
}
|
||||
$this->moduleDataSetup->endSetup();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public static function getDependencies(): array
|
||||
{
|
||||
return [ImportAgents::class];
|
||||
}
|
||||
|
||||
public function getAliases(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Agent\Setup\Patch\Data;
|
||||
|
||||
use Esun\Agent\Model\Map\CountryPosition;
|
||||
use Magento\Framework\App\Filesystem\DirectoryList;
|
||||
use Magento\Framework\Filesystem;
|
||||
use Magento\Framework\Filesystem\Driver\File;
|
||||
use Magento\Framework\Module\Dir\Reader as ModuleDirReader;
|
||||
use Magento\Framework\Setup\ModuleDataSetupInterface;
|
||||
use Magento\Framework\Setup\Patch\DataPatchInterface;
|
||||
|
||||
class ImportAgents implements DataPatchInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ModuleDataSetupInterface $moduleDataSetup,
|
||||
private readonly ModuleDirReader $moduleDirReader,
|
||||
private readonly Filesystem $filesystem,
|
||||
private readonly File $file,
|
||||
private readonly CountryPosition $countryPosition
|
||||
) {
|
||||
}
|
||||
|
||||
public function apply(): self
|
||||
{
|
||||
$this->moduleDataSetup->startSetup();
|
||||
$connection = $this->moduleDataSetup->getConnection();
|
||||
$table = $this->moduleDataSetup->getTable('esun_agent');
|
||||
|
||||
if ((int) $connection->fetchOne($connection->select()->from($table, 'COUNT(*)')) > 0) {
|
||||
$this->moduleDataSetup->endSetup();
|
||||
return $this;
|
||||
}
|
||||
|
||||
$moduleDir = $this->moduleDirReader->getModuleDir('', 'Esun_Agent');
|
||||
$data = json_decode(
|
||||
$this->file->fileGetContents($moduleDir . '/view/frontend/web/data/agents.json'),
|
||||
true,
|
||||
512,
|
||||
JSON_THROW_ON_ERROR
|
||||
);
|
||||
$media = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
|
||||
$media->create('agent/logo');
|
||||
$records = [];
|
||||
|
||||
foreach ($data['agents'] as $agent) {
|
||||
$position = $this->countryPosition->get((string) $agent['country_code']);
|
||||
$logo = '';
|
||||
if ($agent['logo'] !== '') {
|
||||
$source = $moduleDir . '/view/frontend/web/images/agents/' . $agent['logo'];
|
||||
$logo = 'agent/logo/' . $agent['logo'];
|
||||
if ($this->file->isExists($source)) {
|
||||
$media->writeFile($logo, $this->file->fileGetContents($source));
|
||||
}
|
||||
}
|
||||
|
||||
$records[] = [
|
||||
'system_name' => $agent['system_name'],
|
||||
'name' => $agent['name'],
|
||||
'continent' => $agent['continent'],
|
||||
'country_code' => $agent['country_code'],
|
||||
'country' => $agent['country'],
|
||||
'website' => $agent['website'],
|
||||
'address' => $agent['address'],
|
||||
'logo' => $logo,
|
||||
'note' => $agent['note'],
|
||||
'map_x' => $position['x'] ?? null,
|
||||
'map_y' => $position['y'] ?? null,
|
||||
'sort_order' => $agent['sort_order'],
|
||||
'is_active' => 1,
|
||||
];
|
||||
}
|
||||
|
||||
if ($records !== []) {
|
||||
$connection->insertMultiple($table, $records);
|
||||
}
|
||||
|
||||
$this->moduleDataSetup->endSetup();
|
||||
return $this;
|
||||
}
|
||||
|
||||
public static function getDependencies(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function getAliases(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user