Skip to content

Commit

Permalink
symfony configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
sveneld committed Jun 1, 2024
1 parent 8da6eac commit 9cbcd35
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 10 deletions.
1 change: 1 addition & 0 deletions admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
echo 'var creditcurrency="' . $creditSystem->getCreditCurrency() . '"' . PHP_EOL;
$minRequiredCredit = $creditSystem->getMinRequiredCredit();
} else {
$minRequiredCredit = 0;
echo 'var creditenabled=0;', "\n";
}
?>
Expand Down
2 changes: 0 additions & 2 deletions config/bundles.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\MonologBundle\MonologBundle;
use Symfony\Bundle\TwigBundle\TwigBundle;

return [
FrameworkBundle::class => ['all' => true],
MonologBundle::class => ['all' => true],
TwigBundle::class => ['all' => true],
];
4 changes: 4 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use BikeShare\Credit\CodeGenerator\CodeGenerator;
use BikeShare\Credit\CodeGenerator\CodeGeneratorInterface;
use BikeShare\Credit\CreditSystem;
use BikeShare\Credit\CreditSystemFactory;
use BikeShare\Credit\CreditSystemInterface;
use BikeShare\Db\DbInterface;
use BikeShare\Db\MysqliDb;
Expand Down Expand Up @@ -69,6 +70,9 @@
expr("service('BikeShare\\\App\\\Configuration').get('countrycode')"),
]);

$services->get(CreditSystemFactory::class)
->bind('$creditConfiguration', expr("service('BikeShare\\\App\\\Configuration').get('credit')"));

$services->get(CreditSystem::class)
->bind('$creditConfiguration', expr("service('BikeShare\\\App\\\Configuration').get('credit')"));

Expand Down
13 changes: 8 additions & 5 deletions src/Credit/CreditSystemFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@
class CreditSystemFactory
{
private ServiceLocator $locator;
private array $creditConfiguration;

public function __construct(
ServiceLocator $locator
ServiceLocator $locator,
array $creditConfiguration
) {
$this->locator = $locator;
$this->creditConfiguration = $creditConfiguration;
}

public function getCreditSystem(array $creditConfiguration = []): CreditSystemInterface
public function getCreditSystem(): CreditSystemInterface
{
if (!isset($creditConfiguration["enabled"])) {
$creditConfiguration["enabled"] = false;
if (!isset($this->creditConfiguration["enabled"])) {
$this->creditConfiguration["enabled"] = false;
}
if (!$creditConfiguration["enabled"]) {
if (!$this->creditConfiguration["enabled"]) {
return $this->locator->get(DisabledCreditSystem::class);
} else {
return $this->locator->get(CreditSystem::class);
Expand Down
5 changes: 4 additions & 1 deletion src/SmsConnector/SmsConnectorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public function __construct(
) {
$this->logger = $logger;
$this->locator = $locator;
if (empty($connectorConfig['sms'])) {
$connectorConfig['sms'] = DisabledConnector::class;
}
$this->connectorConfig = $connectorConfig;
}

Expand All @@ -28,7 +31,7 @@ public function getConnector(): SmsConnectorInterface
try {
return $this->locator->get($this->connectorConfig['sms']);
} catch (\Throwable $exception) {
$connector = $this->connectorConfig['sms'] ?? 'unknown';
$connector = $this->connectorConfig['sms'];
$this->logger->error('Error creating SMS connector', compact('connector', 'exception'));

return new DisabledConnector();
Expand Down
7 changes: 5 additions & 2 deletions tests/Credit/CreditSystemFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ public function testGetCreditSystem(
$expectedSystemClass
) {
$serviceLocatorMock = $this->createMock(ServiceLocator::class);
$factory = new CreditSystemFactory($serviceLocatorMock);
$factory = new CreditSystemFactory(
$serviceLocatorMock,
$configuration
);

$serviceLocatorMock->expects($this->once())
->method('get')
Expand All @@ -27,7 +30,7 @@ public function testGetCreditSystem(

$this->assertInstanceOf(
$expectedSystemClass,
$factory->getCreditSystem($configuration)
$factory->getCreditSystem()
);
}

Expand Down

0 comments on commit 9cbcd35

Please sign in to comment.