Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
82.61% |
19 / 23 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| SqlClientInstanceRepository | |
81.82% |
18 / 22 |
|
33.33% |
1 / 3 |
7.29 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| findById | |
77.78% |
7 / 9 |
|
0.00% |
0 / 1 |
3.10 | |||
| findAllActive | |
83.33% |
10 / 12 |
|
0.00% |
0 / 1 |
3.04 | |||
| 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\Instance\Infrastructure\Repository; |
| 8 | |
| 9 | defined('AMMONLY_APP') || exit('Direct script access is forbidden.'); |
| 10 | |
| 11 | use App\Core\Instance\Domain\Model\ClientInstance; |
| 12 | use App\Core\Instance\Domain\Repository\ClientInstanceRepositoryInterface; |
| 13 | use PDO; |
| 14 | |
| 15 | /** |
| 16 | * SQL Implementation of ClientInstanceRepositoryInterface. |
| 17 | * |
| 18 | * @package App\Core\Instance\Infrastructure\Repository |
| 19 | */ |
| 20 | final readonly class SqlClientInstanceRepository implements ClientInstanceRepositoryInterface |
| 21 | { |
| 22 | private string $table; |
| 23 | |
| 24 | /** |
| 25 | * SqlClientInstanceRepository constructor. |
| 26 | * |
| 27 | * @param PDO $pdo Database connection. |
| 28 | * @param string $prefix Table prefix (default: a_). |
| 29 | */ |
| 30 | public function __construct( |
| 31 | private PDO $pdo, |
| 32 | string $prefix = 'a_' |
| 33 | ) { |
| 34 | $this->table = $prefix . 'mod_client_instances_records'; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * @inheritDoc |
| 39 | */ |
| 40 | public function findById(int $id): ?ClientInstance |
| 41 | { |
| 42 | try { |
| 43 | $stmt = $this->pdo->prepare( |
| 44 | "SELECT `id`, `name`, `environment`, `api_base_url`, `api_bearer_token`, " . |
| 45 | "`is_active`, `description` FROM `{$this->table}` WHERE `id` = :id LIMIT 1" |
| 46 | ); |
| 47 | $stmt->execute([':id' => $id]); |
| 48 | $row = $stmt->fetch(PDO::FETCH_ASSOC); |
| 49 | |
| 50 | return ($row !== false) ? ClientInstance::fromRow($row) : null; |
| 51 | } catch (\Throwable) { |
| 52 | return null; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @inheritDoc |
| 58 | */ |
| 59 | public function findAllActive(): array |
| 60 | { |
| 61 | try { |
| 62 | $stmt = $this->pdo->prepare( |
| 63 | "SELECT `id`, `name`, `environment`, `api_base_url`, `api_bearer_token`, " . |
| 64 | "`is_active`, `description` FROM `{$this->table}` WHERE `is_active` = 1 ORDER BY `id` ASC" |
| 65 | ); |
| 66 | $stmt->execute(); |
| 67 | $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); |
| 68 | |
| 69 | $instances = []; |
| 70 | foreach ($rows as $row) { |
| 71 | $instances[] = ClientInstance::fromRow($row); |
| 72 | } |
| 73 | |
| 74 | return $instances; |
| 75 | } catch (\Throwable) { |
| 76 | return []; |
| 77 | } |
| 78 | } |
| 79 | } |