Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.91% |
10 / 11 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| TablePrefixResolver | |
90.91% |
10 / 11 |
|
50.00% |
1 / 2 |
5.02 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| resolve | |
90.00% |
9 / 10 |
|
0.00% |
0 / 1 |
4.02 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Shared\Host; |
| 6 | |
| 7 | class TablePrefixResolver |
| 8 | { |
| 9 | public function __construct(private ActiveHostProvider $activeHostProvider) {} |
| 10 | |
| 11 | /** |
| 12 | * Resolves the actual database table name based on the active host context. |
| 13 | * If activeHost is set: returns '{db_prefix}_{tableName}' (e.g. 'c01_menu_records'). |
| 14 | * If activeHost is null: returns 'srv_{tableName}' (e.g. 'srv_menu_records'). |
| 15 | */ |
| 16 | public function resolve(string $baseTableName): string |
| 17 | { |
| 18 | $cleanName = ltrim($baseTableName, '_'); |
| 19 | |
| 20 | // If the table already starts with srv_ or c01_, strip the leading prefix first |
| 21 | if (str_starts_with($cleanName, 'srv_')) { |
| 22 | $cleanName = substr($cleanName, 4); |
| 23 | } elseif (preg_match('/^c\d+_(.+)$/', $cleanName, $matches)) { |
| 24 | $cleanName = $matches[1]; |
| 25 | } |
| 26 | |
| 27 | $activeHost = $this->activeHostProvider->getActiveHost(); |
| 28 | if ($activeHost !== null) { |
| 29 | $prefix = $activeHost->getDbPrefix(); |
| 30 | return $prefix . '_' . $cleanName; |
| 31 | } |
| 32 | |
| 33 | return 'srv_' . $cleanName; |
| 34 | } |
| 35 | } |