mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-05-07 15:05:38 +02:00
35 lines
838 B
PHP
35 lines
838 B
PHP
<?php
|
|
|
|
namespace DesignPatterns\Structural\DependencyInjection;
|
|
|
|
class DatabaseConnection
|
|
{
|
|
/**
|
|
* @var DatabaseConfiguration
|
|
*/
|
|
private $configuration;
|
|
|
|
/**
|
|
* @param DatabaseConfiguration $config
|
|
*/
|
|
public function __construct(DatabaseConfiguration $config)
|
|
{
|
|
$this->configuration = $config;
|
|
}
|
|
|
|
public function getDsn(): string
|
|
{
|
|
// this is just for the sake of demonstration, not a real DSN
|
|
// notice that only the injected config is used here, so there is
|
|
// a real separation of concerns here
|
|
|
|
return sprintf(
|
|
'%s:%s@%s:%d',
|
|
$this->configuration->getUsername(),
|
|
$this->configuration->getPassword(),
|
|
$this->configuration->getHost(),
|
|
$this->configuration->getPort()
|
|
);
|
|
}
|
|
}
|