Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
49 / 49 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| SqlDataMappingRepository | |
100.00% |
48 / 48 |
|
100.00% |
4 / 4 |
13 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| findMapping | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
5 | |||
| findAll | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
4 | |||
| save | |
100.00% |
22 / 22 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /** @license For full copyright and license information, please see the LICENSE.md file. */ |
| 6 | |
| 7 | namespace App\Core\Engine\Infrastructure\Repository; |
| 8 | |
| 9 | defined('AMMONLY_APP') || exit('Direct script access is forbidden.'); |
| 10 | |
| 11 | use App\Core\Database\Repository\TenantAwareRepositoryTrait; |
| 12 | use App\Core\Engine\Domain\Repository\DataMappingRepositoryInterface; |
| 13 | use App\Core\Instance\Application\Service\InstanceContextManagerInterface; |
| 14 | use PDO; |
| 15 | |
| 16 | final readonly class SqlDataMappingRepository implements DataMappingRepositoryInterface |
| 17 | { |
| 18 | use TenantAwareRepositoryTrait; |
| 19 | |
| 20 | public function __construct( |
| 21 | protected PDO $pdo, |
| 22 | private string $tablePrefix = 'a_', |
| 23 | protected ?InstanceContextManagerInterface $instanceManager = null, |
| 24 | protected ?PDO $clientPdo = null |
| 25 | ) { |
| 26 | } |
| 27 | |
| 28 | public function findMapping(int $sourceModuleId, int $targetModuleId): ?array |
| 29 | { |
| 30 | $table = $this->tablePrefix . 'core_data_mapping_records'; |
| 31 | $sql = "SELECT `id`, `source_module_id`, `target_module_id`, `mapping_name`, `mapping_rules`, |
| 32 | `is_active`, `created_at`, `updated_at`, `created_by`, `owner` |
| 33 | FROM `{$table}` |
| 34 | WHERE `source_module_id` = :source AND `target_module_id` = :target |
| 35 | LIMIT 1"; |
| 36 | |
| 37 | $stmt = $this->getPdo()->prepare($sql); |
| 38 | $stmt->execute([ |
| 39 | 'source' => $sourceModuleId, |
| 40 | 'target' => $targetModuleId, |
| 41 | ]); |
| 42 | |
| 43 | $result = $stmt->fetch(PDO::FETCH_ASSOC); |
| 44 | if ($result && isset($result['mapping_rules']) && is_string($result['mapping_rules'])) { |
| 45 | $result['mapping_rules'] = json_decode($result['mapping_rules'], true); |
| 46 | } |
| 47 | |
| 48 | return is_array($result) ? $result : null; |
| 49 | } |
| 50 | |
| 51 | public function findAll(): array |
| 52 | { |
| 53 | $table = $this->tablePrefix . 'core_data_mapping_records'; |
| 54 | $sql = "SELECT `id`, `source_module_id`, `target_module_id`, `mapping_name`, `mapping_rules`, |
| 55 | `is_active`, `created_at`, `updated_at`, `created_by`, `owner` |
| 56 | FROM `{$table}` |
| 57 | ORDER BY `mapping_name` ASC"; |
| 58 | |
| 59 | $stmt = $this->getPdo()->query($sql); |
| 60 | $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); |
| 61 | |
| 62 | $results = []; |
| 63 | foreach ($rows as $row) { |
| 64 | if (isset($row['mapping_rules']) && is_string($row['mapping_rules'])) { |
| 65 | $row['mapping_rules'] = json_decode($row['mapping_rules'], true); |
| 66 | } |
| 67 | $results[] = $row; |
| 68 | } |
| 69 | |
| 70 | return $results; |
| 71 | } |
| 72 | |
| 73 | public function save(array $data): int |
| 74 | { |
| 75 | $table = $this->tablePrefix . 'core_data_mapping_records'; |
| 76 | $sql = "INSERT INTO `{$table}` |
| 77 | (`source_module_id`, `target_module_id`, `mapping_name`, `mapping_rules`, `created_by`, `owner`) |
| 78 | VALUES (:source, :target, :name, :rules, :created_by, :owner) |
| 79 | ON DUPLICATE KEY UPDATE |
| 80 | `mapping_name` = VALUES(`mapping_name`), |
| 81 | `mapping_rules` = VALUES(`mapping_rules`), |
| 82 | `updated_at` = CURRENT_TIMESTAMP(6)"; |
| 83 | |
| 84 | $stmt = $this->getPdo()->prepare($sql); |
| 85 | $creator = (int) ($data['created_by'] ?? 1); |
| 86 | $owner = (int) ($data['owner'] ?? $creator); |
| 87 | $rules = is_array($data['mapping_rules'] ?? null) |
| 88 | ? json_encode($data['mapping_rules']) |
| 89 | : (string) ($data['mapping_rules'] ?? '[]'); |
| 90 | |
| 91 | $stmt->execute([ |
| 92 | 'source' => (int) $data['source_module_id'], |
| 93 | 'target' => (int) $data['target_module_id'], |
| 94 | 'name' => (string) ($data['mapping_name'] ?? 'Default Mapping'), |
| 95 | 'rules' => $rules, |
| 96 | 'created_by' => $creator, |
| 97 | 'owner' => $owner, |
| 98 | ]); |
| 99 | |
| 100 | $lastId = (int) $this->getPdo()->lastInsertId(); |
| 101 | if ($lastId > 0) { |
| 102 | return $lastId; |
| 103 | } |
| 104 | |
| 105 | $existing = $this->findMapping((int) $data['source_module_id'], (int) $data['target_module_id']); |
| 106 | return (int) ($existing['id'] ?? 0); |
| 107 | } |
| 108 | } |