Skip to content

Commit

Permalink
Merge pull request #27 from akrobate67/#26-Error-connecting-to-Databa…
Browse files Browse the repository at this point in the history
…se-Host-when-triggering-DbSwitchEvent

Add 2 new methods to TenantDbConfigurationInterface
  • Loading branch information
RamyHakam authored Nov 29, 2023
2 parents f033e86 + fcbeb90 commit a6fe382
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/EventListener/DbSwitchEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public function onHakamMultiTenancyBundleEventSwitchDbEvent(SwitchDbEvent $switc
'dbname' => $dbConfig->getDbName(),
'user' => $dbConfig->getDbUsername() ?? $this->parseDatabaseUrl($this->databaseURL)['user'],
'password' => $dbConfig->getDbPassword() ?? $this->parseDatabaseUrl($this->databaseURL)['password'],
'host' => $dbConfig->getDbHost() ?? $this->parseDatabaseUrl($this->databaseURL)['host'],
'port' => $dbConfig->getDbPort() ?? $this->parseDatabaseUrl($this->databaseURL)['port'],
];
$tenantConnection->switchConnection($params);
}
Expand All @@ -48,6 +50,8 @@ private function parseDatabaseUrl(string $databaseURL): array
'dbname' => substr($url['path'], 1),
'user' => $url['user'],
'password' => $url['pass'],
'host' => $url['host'],
'port' => $url['port'],
];
}
}
10 changes: 10 additions & 0 deletions src/Services/TenantDbConfigurationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ public function getDbUsername(): ?string;
*/
public function getDbPassword(): ?string;

/**
* Tenant databasehost.
*/
public function getDbHost(): ?string;

/**
* Tenant database port.
*/
public function getDbPort(): ?string;

/**
* Tenant database status.
*/
Expand Down
43 changes: 43 additions & 0 deletions src/Traits/TenantDbConfigTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ trait TenantDbConfigTrait
#[ORM\Column(type: 'string', length: 255, nullable: true, options: ["default" => null])]
protected ?string $dbPassword = null;

#[ORM\Column(type: 'string', length: 255, nullable: true, options: ["default" => null])]
protected ?string $dbHost = null;

#[ORM\Column(type: 'string', length: 5, nullable: true, options: ["default" => null])]
protected ?string $dbPort = null;

#[ORM\Column(type: 'string', length: 255, enumType: DatabaseStatusEnum::class, options: ["default" => DatabaseStatusEnum::DATABASE_NOT_CREATED])]
private DatabaseStatusEnum $databaseStatus = DatabaseStatusEnum::DATABASE_NOT_CREATED;

Expand Down Expand Up @@ -94,4 +100,41 @@ public function setDatabaseStatus(DatabaseStatusEnum $databaseStatus): self
$this->databaseStatus = $databaseStatus;
return $this;
}

/**
* @param string|null $dbHost
* @return self
*/
public function setDbHost(?string $dbHost): self
{
$this->dbHost = $dbHost;
return $this;
}

/**
* @return string|null
*/
public function getDbHost(): ?string
{
return $this->dbHost;
}

/**
* @param string|null $dbPort
* @return self
*/
public function setDbPort(?string $dbPort): self
{
$this->dbPort = $dbPort;
return $this;
}

/**
* @return string|null
*/
public function getDbPort(): ?string
{
return $this->dbPort;
}

}
46 changes: 46 additions & 0 deletions tests/Unit/EventListener/DbSwitchEventListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public function testOnHakamMultiTenancyBundleEventSwitchDbEvent()
$mockDbConfig->setDbName('test_db_name');
$mockDbConfig->setDbUsername('test_username');
$mockDbConfig->setDbPassword('test_password');
$mockDbConfig->setDbHost('127.0.0.1');
$mockDbConfig->setDbPort('3306');
$mockDbConfigService->expects($this->once())
->method('findDbConfig')
->with($testDbIndex)
Expand Down Expand Up @@ -59,6 +61,8 @@ class DbConfig implements TenantDbConfigurationInterface
private string $dbName;
private string $dbUsername;
private ?string $dbPassword;
private ?string $dbPort;
private ?string $dbHost;

public function getDbName(): string
{
Expand Down Expand Up @@ -104,4 +108,46 @@ public function setDatabaseStatus(DatabaseStatusEnum $databaseStatus): TenantDbC
{
return $this;
}



/**
* Get the value of dbPort
*/
public function getDbPort(): null|string
{
return $this->dbPort;
}

/**
* Set the value of dbPort
*
* @return self
*/
public function setDbPort($dbPort)
{
$this->dbPort = $dbPort;

return $this;
}

/**
* Get the value of dbHost
*/
public function getDbHost(): null|string
{
return $this->dbHost;
}

/**
* Set the value of dbHost
*
* @return self
*/
public function setDbHost($dbHost)
{
$this->dbHost = $dbHost;

return $this;
}
}

0 comments on commit a6fe382

Please sign in to comment.