ControleOnline /
api-platform-multi-tenancy
| 1 | <?php |
||
| 2 | |||
| 3 | namespace ControleOnline\Service; |
||
| 4 | |||
| 5 | use Doctrine\DBAL\Connection; |
||
|
0 ignored issues
–
show
|
|||
| 6 | use Doctrine\DBAL\Platforms\MySqlPlatform; |
||
|
0 ignored issues
–
show
The type
Doctrine\DBAL\Platforms\MySqlPlatform was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 7 | use Doctrine\DBAL\Platforms\SQLServerPlatform; |
||
|
0 ignored issues
–
show
The type
Doctrine\DBAL\Platforms\SQLServerPlatform was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 8 | use Doctrine\DBAL\Types\Type; |
||
|
0 ignored issues
–
show
The type
Doctrine\DBAL\Types\Type was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 9 | use InvalidArgumentException; |
||
| 10 | |||
| 11 | class DatabaseSwitchService |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @var Connection |
||
| 15 | */ |
||
| 16 | private $connection; |
||
| 17 | /** |
||
| 18 | * @var array |
||
| 19 | */ |
||
| 20 | private static $originalDbParams; |
||
| 21 | |||
| 22 | |||
| 23 | /** |
||
| 24 | * DatabaseSwitchService constructor. |
||
| 25 | * @param Connection $connection |
||
| 26 | */ |
||
| 27 | public function __construct(Connection $connection) |
||
| 28 | { |
||
| 29 | $this->connection = $connection; |
||
| 30 | if (!self::$originalDbParams) |
||
|
0 ignored issues
–
show
The expression
self::originalDbParams of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent. Consider making the comparison explicit by using Loading history...
|
|||
| 31 | self::$originalDbParams = $connection->getParams(); |
||
| 32 | } |
||
| 33 | |||
| 34 | /* @param string $domain |
||
| 35 | * @throws \Doctrine\DBAL\Driver\Exception |
||
| 36 | * @throws \Doctrine\DBAL\Exception |
||
| 37 | */ |
||
| 38 | public function switchDatabaseByDomain($domain) |
||
| 39 | { |
||
| 40 | $this->switchDatabase($this->getDbData($domain)); |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @param string $domain |
||
| 45 | * @return bool |
||
| 46 | */ |
||
| 47 | public function switchBackToOriginalDatabase() |
||
| 48 | { |
||
| 49 | $this->switchDatabase(self::$originalDbParams); |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @param array $dbData |
||
| 54 | */ |
||
| 55 | private function switchDatabase($dbData) |
||
| 56 | { |
||
| 57 | if ($this->connection->isConnected()) |
||
| 58 | $this->connection->close(); |
||
| 59 | |||
| 60 | $this->connection->__construct( |
||
| 61 | $dbData, |
||
| 62 | $this->getDriverClass($dbData), |
||
| 63 | $this->connection->getConfiguration(), |
||
| 64 | //$this->connection->getEventManager() |
||
| 65 | ); |
||
| 66 | |||
| 67 | //$this->connection->connect(); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @param string $domain |
||
| 72 | * @return array |
||
| 73 | * @throws \Doctrine\DBAL\Driver\Exception |
||
| 74 | * @throws \Doctrine\DBAL\Exception |
||
| 75 | * @throws \Doctrine\DBAL\Driver\Exception |
||
| 76 | */ |
||
| 77 | private function getDbData($domain) |
||
| 78 | { |
||
| 79 | $this->switchBackToOriginalDatabase(); |
||
| 80 | $params = $this->connection->getParams(); |
||
| 81 | $sql = 'SELECT db_host, db_name, db_port, db_user, db_driver, db_instance, |
||
| 82 | AES_DECRYPT(db_password, :tenancy_secret) AS db_password |
||
| 83 | FROM `databases` WHERE app_host = :app_host'; |
||
| 84 | |||
| 85 | $statement = $this->connection->executeQuery( |
||
| 86 | $sql, |
||
| 87 | [ |
||
| 88 | 'app_host' => $domain, |
||
| 89 | 'tenancy_secret' => $_ENV['TENANCY_SECRET'] |
||
| 90 | ], |
||
| 91 | ['app_host' => Type::getType('string'), 'tenancy_secret' => Type::getType('string')] |
||
| 92 | ); |
||
| 93 | |||
| 94 | $result = $statement->fetchAssociative(); |
||
| 95 | $params['platform'] = $this->getPlatform($result['db_driver']); |
||
| 96 | $params['host'] = $result['db_host']; |
||
| 97 | $params['port'] = $result['db_port']; |
||
| 98 | $params['dbname'] = $result['db_name']; |
||
| 99 | $params['user'] = $result['db_user']; |
||
| 100 | $params['password'] = $result['db_password']; |
||
| 101 | $params['driver'] = $result['db_driver']; |
||
| 102 | $params['instancename'] = $result['db_instance']; |
||
| 103 | return $params; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @return array |
||
| 108 | * @throws \Doctrine\DBAL\Driver\Exception |
||
| 109 | * @throws \Doctrine\DBAL\Exception |
||
| 110 | */ |
||
| 111 | public function getAllDomains() |
||
| 112 | { |
||
| 113 | $this->switchBackToOriginalDatabase(); |
||
| 114 | $sql = 'SELECT app_host FROM `databases`'; |
||
| 115 | $statement = $this->connection->executeQuery($sql); |
||
| 116 | $results = $statement->fetchAllAssociative(); |
||
| 117 | $domains = array_column($results, 'app_host'); |
||
| 118 | return $domains; |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param array $dbData |
||
| 123 | * @return mixed |
||
| 124 | */ |
||
| 125 | private function getDriverClass($dbData) |
||
| 126 | { |
||
| 127 | $driverClass = null; |
||
| 128 | switch ($dbData['driver']) { |
||
| 129 | case 'pdo_mysql': |
||
| 130 | $driverClass = \Doctrine\DBAL\Driver\PDO\MySql\Driver::class; |
||
|
0 ignored issues
–
show
The type
Doctrine\DBAL\Driver\PDO\MySql\Driver was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 131 | break; |
||
| 132 | case 'pdo_sqlsrv': |
||
| 133 | $driverClass = \Doctrine\DBAL\Driver\PDO\SQLSrv\Driver::class; |
||
|
0 ignored issues
–
show
The type
Doctrine\DBAL\Driver\PDO\SQLSrv\Driver was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 134 | break; |
||
| 135 | default: |
||
| 136 | throw new InvalidArgumentException('Driver not supported: ' . $dbData['driver']); |
||
| 137 | } |
||
| 138 | |||
| 139 | return new $driverClass(); |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param string $db_driver |
||
| 144 | * @return MySqlPlatform|SQLServerPlatform |
||
| 145 | */ |
||
| 146 | private function getPlatform($db_driver) |
||
| 147 | { |
||
| 148 | switch ($db_driver) { |
||
| 149 | case 'pdo_mysql': |
||
| 150 | return new MySqlPlatform(); |
||
| 151 | case 'pdo_sqlsrv': |
||
| 152 | return new SQLServerPlatform(); |
||
| 153 | default: |
||
| 154 | throw new InvalidArgumentException('Driver not supported: ' . $db_driver); |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | |||
| 159 | } |
||
| 160 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths