| Author | Dave Jarvis <email> |
|---|---|
| Date | 2026-01-26 00:04:26 GMT-0800 |
| Commit | 0b667ef6be439fd132a09362febcaaac0a228379 |
| Parent | b2df21b |
| /** | ||
| - * 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 ); | ||
| } | ||
| 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; | ||
| } | ||
| } | ||
| - 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' ); | ||
| } | ||
| $session = new Session(); | ||
| -$config->load(); | ||
| $config->configure( $publisher ); | ||
| $valid = $address->process( [ |
| 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 ); | ||
| } | ||
| } | ||
| $this->harness = new TestHarness(); | ||
| $config = new Configuration(); | ||
| - $config->load(); | ||
| $this->publisher = new Publisher(); | ||
| $config->configure( $this->publisher ); |
| $publisher = new Publisher(); | ||
| - $config->load(); | ||
| $config->configure( $publisher ); | ||
| Delta | 33 lines added, 29 lines removed, 4-line increase |
|---|