Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/pod.git

Loads and uses Lulu configuration by default

AuthorDave Jarvis <email>
Date2026-01-26 00:04:26 GMT-0800
Commit0b667ef6be439fd132a09362febcaaac0a228379
Parentb2df21b
Configuration.php
/**
- * Loads configuration settings from the lulu.config file in the
- * user's home directory.
+ * Constructor - automatically loads configuration on instantiation.
*/
- public function load() {
+ public function __construct() {
$path = $this->homeDirectory() . '/.keys/lulu.config';
]
);
+ }
+
+ public function createExchangeRate( Currency $base ): ExchangeRate {
+ $lifetime = (int)($this->settings['FOREX_LIFETIME'] ?? 14400);
+ $cache = $this->settings['FOREX_CACHE_PATH'] ??
+ sys_get_temp_dir() . '/forex_cache.json';
+ $url = $this->settings['FOREX_URL'] ?? '';
+
+ return new ExchangeRate( $base, $url, $cache, $lifetime );
}
ExchangeRate.php
private array $rates = [];
private string $base;
+ private string $cacheFile;
// How long until we refresh the exchange rate cache (in minutes).
- private const CACHE_LIFETIME = 14400;
+ private int $cacheLifetime = 14400;
- public function __construct( Currency $base, string $url, string $cache ) {
+ public function __construct(
+ Currency $base, string $url, string $cache, int $lifetime
+ ) {
+ $this->cacheFile = $cache;
+ $this->cacheLifetime = $lifetime;
$url = $base->replace( $url, '{{currency}}' );
return $targetRate / $sourceRate;
+ }
+
+ public function clearCache(): void {
+ if( file_exists( $this->cacheFile ) ) {
+ unlink( $this->cacheFile );
+ }
}
private function expired( string $cache ): bool {
return !file_exists( $cache )
- || ( time() - filemtime( $cache ) ) > self::CACHE_LIFETIME;
+ || ( time() - filemtime( $cache ) ) > $this->cacheLifetime;
}
Order.php
}
- public function render() {
+ public function render(): void {
$this->{$this->renderMethod}();
}
}
- private function renderError() {
+ private function renderError(): void {
?>
<h2>Shipping calculation error</h2>
}
- private function escape( $value ) {
+ private function escape( $value ): string {
return htmlspecialchars( $value ?? '', ENT_QUOTES, 'UTF-8' );
}
calculate.php
$session = new Session();
-$config->load();
$config->configure( $publisher );
$valid = $address->process( [
tests/TestExchangeRate.php
class TestExchangeRate {
- private string $cacheFile;
+ private Configuration $config;
public function __construct() {
- $this->cacheFile = sys_get_temp_dir() . '/rates.json';
+ $this->config = new Configuration();
}
/**
* Executes all test methods via callback.
*
* @param callable $callback Function to execute test methods
*/
public function run( $callback ) {
- $this->cleanCache();
+ $forex = $this->createExchangeRate();
+ $forex->clearCache();
$callback( [
*/
private function createExchangeRate(): ExchangeRate {
- return new ExchangeRate(
- Currency::USD,
- 'https://api.frankfurter.dev/v1/latest?base={{currency}}',
- $this->cacheFile
- );
- }
-
- /**
- * Deletes the cache file to force fresh data download.
- */
- private function cleanCache(): void {
- if( file_exists( $this->cacheFile ) ) {
- unlink( $this->cacheFile );
- }
+ return $this->config->createExchangeRate( Currency::CAD );
}
}
tests/TestOrder.php
$this->harness = new TestHarness();
$config = new Configuration();
- $config->load();
$this->publisher = new Publisher();
$config->configure( $this->publisher );
tests/TestPublisher.php
$publisher = new Publisher();
- $config->load();
$config->configure( $publisher );
Delta33 lines added, 29 lines removed, 4-line increase