Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
80.77% covered (warning)
80.77%
21 / 26
66.67% covered (warning)
66.67%
4 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
DataMappingApplicationService
80.00% covered (warning)
80.00%
20 / 25
66.67% covered (warning)
66.67%
4 / 6
24.53
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getAllMappings
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getMapping
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 saveMapping
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 shouldCopyInventory
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
20
 mapRecordData
94.12% covered (success)
94.12%
16 / 17
0.00% covered (danger)
0.00%
0 / 1
13.03
1<?php
2
3declare(strict_types=1);
4
5/** @license For full copyright and license information, please see the LICENSE.md file. */
6
7namespace App\Core\Engine\Application\Service;
8
9defined('AMMONLY_APP') || exit('Direct script access is forbidden.');
10
11use App\Core\Engine\Domain\Repository\DataMappingRepositoryInterface;
12
13final readonly class DataMappingApplicationService
14{
15    public function __construct(
16        private DataMappingRepositoryInterface $mappingRepo
17    ) {
18    }
19
20    /**
21     * Retrieves all configured data mappings.
22     *
23     * @return array<int, array<string, mixed>>
24     */
25     public function getAllMappings(): array
26     {
27         return $this->mappingRepo->findAll();
28     }
29
30     /**
31      * Retrieves mapping configuration for specific source and target modules.
32      *
33      * @param int $sourceModuleId
34      * @param int $targetModuleId
35      * @return array<string, mixed>|null
36      */
37     public function getMapping(int $sourceModuleId, int $targetModuleId): ?array
38     {
39         return $this->mappingRepo->findMapping($sourceModuleId, $targetModuleId);
40     }
41
42     /**
43      * Saves or updates a data mapping configuration.
44      *
45      * @param array<string, mixed> $data
46      * @return int
47      */
48     public function saveMapping(array $data): int
49     {
50         return $this->mappingRepo->save($data);
51     }
52
53     /**
54      * Checks if inventory items should be copied according to mapping configuration.
55      *
56      * @param int $sourceModuleId
57      * @param int $targetModuleId
58      * @return bool
59      */
60     public function shouldCopyInventory(int $sourceModuleId, int $targetModuleId): bool
61     {
62         $mapping = $this->mappingRepo->findMapping($sourceModuleId, $targetModuleId);
63         if (!$mapping || empty($mapping['mapping_rules']) || !is_array($mapping['mapping_rules'])) {
64             return false;
65         }
66
67         return !empty($mapping['mapping_rules']['copy_inventory_items']);
68     }
69
70    /**
71     * Maps data from a source record to a format ready for the target module.
72     *
73     * @param int                  $sourceModuleId Source module ID.
74     * @param int                  $targetModuleId Target module ID.
75     * @param array<string, mixed> $sourceRecord   Source record data.
76     * @return array<string, mixed> Mapped data ready for target module insertion.
77     */
78    public function mapRecordData(int $sourceModuleId, int $targetModuleId, array $sourceRecord): array
79    {
80        $mapping = $this->mappingRepo->findMapping($sourceModuleId, $targetModuleId);
81
82        if (!$mapping || empty($mapping['mapping_rules']) || !is_array($mapping['mapping_rules'])) {
83            return [];
84        }
85
86        $rules = $mapping['mapping_rules']['fields'] ?? $mapping['mapping_rules'];
87        if (!is_array($rules)) {
88            return [];
89        }
90
91        $mappedData = [];
92        foreach ($rules as $rule) {
93            $sourceField = $rule['source_field'] ?? null;
94            $targetField = $rule['target_field'] ?? null;
95            $type = $rule['type'] ?? 'copy';
96
97            if ($targetField && is_string($targetField)) {
98                if ($type === 'copy' && is_string($sourceField) && array_key_exists($sourceField, $sourceRecord)) {
99                    $mappedData[$targetField] = $sourceRecord[$sourceField];
100                } elseif ($type === 'static' && isset($rule['static_value'])) {
101                    $mappedData[$targetField] = $rule['static_value'];
102                }
103            }
104        }
105
106        return $mappedData;
107    }
108}