Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
42 / 42 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| SqlInventoryFieldRepository | |
100.00% |
41 / 41 |
|
100.00% |
5 / 5 |
13 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| findByModule | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
| create | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
5 | |||
| update | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
4 | |||
| delete | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| 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\InventoryFieldRepositoryInterface; |
| 13 | use App\Core\Instance\Application\Service\InstanceContextManagerInterface; |
| 14 | use PDO; |
| 15 | |
| 16 | final readonly class SqlInventoryFieldRepository implements InventoryFieldRepositoryInterface |
| 17 | { |
| 18 | use TenantAwareRepositoryTrait; |
| 19 | |
| 20 | private const array ALLOWED_COLUMNS = [ |
| 21 | 'module_id', |
| 22 | 'field_key', |
| 23 | 'label', |
| 24 | 'uitype_id', |
| 25 | 'is_mandatory', |
| 26 | 'is_readonly', |
| 27 | 'is_system', |
| 28 | 'sort_order', |
| 29 | 'picklist_id', |
| 30 | 'status', |
| 31 | 'special_access', |
| 32 | 'created_by', |
| 33 | 'owner', |
| 34 | ]; |
| 35 | |
| 36 | public function __construct( |
| 37 | protected PDO $pdo, |
| 38 | private string $tablePrefix = 'a_', |
| 39 | protected ?InstanceContextManagerInterface $instanceManager = null, |
| 40 | protected ?PDO $clientPdo = null |
| 41 | ) { |
| 42 | } |
| 43 | |
| 44 | public function findByModule(int $moduleId): array |
| 45 | { |
| 46 | $table = $this->tablePrefix . 'core_inventory_field_records'; |
| 47 | $sql = "SELECT `id`, `module_id`, `field_key`, `label`, `uitype_id`, `is_mandatory`, |
| 48 | `is_readonly`, `is_system`, `sort_order`, `picklist_id`, `status`, |
| 49 | `special_access`, `created_at`, `updated_at`, `created_by`, `owner` |
| 50 | FROM `{$table}` |
| 51 | WHERE `module_id` = :module_id AND `special_access` != 0 |
| 52 | ORDER BY `sort_order` ASC, `id` ASC"; |
| 53 | |
| 54 | $stmt = $this->getPdo()->prepare($sql); |
| 55 | $stmt->execute(['module_id' => $moduleId]); |
| 56 | $results = $stmt->fetchAll(PDO::FETCH_ASSOC); |
| 57 | |
| 58 | return is_array($results) ? $results : []; |
| 59 | } |
| 60 | |
| 61 | public function create(array $data): int |
| 62 | { |
| 63 | $table = $this->tablePrefix . 'core_inventory_field_records'; |
| 64 | $insertData = []; |
| 65 | foreach (self::ALLOWED_COLUMNS as $col) { |
| 66 | if (array_key_exists($col, $data)) { |
| 67 | $insertData[$col] = $data[$col]; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | if (!isset($insertData['created_by'])) { |
| 72 | $insertData['created_by'] = 1; |
| 73 | } |
| 74 | if (!isset($insertData['owner'])) { |
| 75 | $insertData['owner'] = $insertData['created_by']; |
| 76 | } |
| 77 | |
| 78 | $fields = array_keys($insertData); |
| 79 | $columnsSql = '`' . implode('`, `', $fields) . '`'; |
| 80 | $placeholders = ':' . implode(', :', $fields); |
| 81 | |
| 82 | $sql = "INSERT INTO `{$table}` ({$columnsSql}) VALUES ({$placeholders})"; |
| 83 | $stmt = $this->getPdo()->prepare($sql); |
| 84 | $stmt->execute($insertData); |
| 85 | |
| 86 | return (int) $this->getPdo()->lastInsertId(); |
| 87 | } |
| 88 | |
| 89 | public function update(int $id, array $data): void |
| 90 | { |
| 91 | $table = $this->tablePrefix . 'core_inventory_field_records'; |
| 92 | $setParts = []; |
| 93 | $params = ['id' => $id]; |
| 94 | |
| 95 | foreach (self::ALLOWED_COLUMNS as $col) { |
| 96 | if (array_key_exists($col, $data)) { |
| 97 | $setParts[] = "`{$col}` = :{$col}"; |
| 98 | $params[$col] = $data[$col]; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | if (empty($setParts)) { |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | $sql = "UPDATE `{$table}` SET " . implode(', ', $setParts) . " WHERE `id` = :id"; |
| 107 | $stmt = $this->getPdo()->prepare($sql); |
| 108 | $stmt->execute($params); |
| 109 | } |
| 110 | |
| 111 | public function delete(int $id): void |
| 112 | { |
| 113 | $table = $this->tablePrefix . 'core_inventory_field_records'; |
| 114 | $sql = "DELETE FROM `{$table}` WHERE `id` = :id"; |
| 115 | $stmt = $this->getPdo()->prepare($sql); |
| 116 | $stmt->execute(['id' => $id]); |
| 117 | } |
| 118 | } |