Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
87.50% covered (warning)
87.50%
7 / 8
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
TenantAwareRepositoryTrait
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
6
100.00% covered (success)
100.00%
1 / 1
 getPdo
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
6
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\Database\Repository;
8
9defined('AMMONLY_APP') || exit('Direct script access is forbidden.');
10
11use PDO;
12
13/**
14 * Trait providing tenant-aware PDO connection resolution for SQL repositories.
15 *
16 * Expects the composing repository class to hold `$pdo`, optional `$instanceManager`,
17 * and optional `$clientPdo` properties.
18 */
19trait TenantAwareRepositoryTrait
20{
21    /**
22     * Resolves the active PDO connection depending on the current instance context.
23     *
24     * @return PDO Client PDO when in remote tenant context, fallback PDO otherwise.
25     */
26    private function getPdo(): PDO
27    {
28        if (
29            isset($this->instanceManager)
30            && $this->instanceManager !== null
31            && $this->instanceManager->isRemote()
32            && isset($this->clientPdo)
33            && $this->clientPdo !== null
34        ) {
35            return $this->clientPdo;
36        }
37
38        return $this->pdo;
39    }
40}