Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/pod.git
<?php
class Configuration {
  private $settings = [];

  /**
   * Constructor - automatically loads configuration on instantiation.
   */
  public function __construct() {
    $path = $this->homeDirectory() . '/.keys/lulu.config';

    if( file_exists( $path ) ) {
      $this->settings = json_decode( file_get_contents( $path ), true );
    }
  }

  /**
   * Initializes the publisher with API credentials and order
   * specifications from the loaded configuration.
   *
   * @param Publisher $publisher Publisher object to configure.
   */
  public function configure( Publisher $publisher ) {
    $publisher->initialize(
      $this->settings[ 'CLIENT_KEY' ] ?? '',
      $this->settings[ 'URL_BASE' ] ?? '',
      [
        'package'  => $this->settings[ 'ORDER_PACKAGE' ] ?? '',
        'pages'    => $this->settings[ 'ORDER_PAGE_COUNT' ] ?? 0,
        'quantity' => $this->settings[ 'ORDER_QUANTITY' ] ?? 1,
        'shipping' => $this->settings[ 'ORDER_SHIPPING' ] ?? ''
      ]
    );
  }

  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 function homeDirectory() {
    $result = '';

    if( function_exists( 'posix_getpwnam' ) ) {
      $homedir = posix_getpwnam( get_current_user() );

      if( !empty( $homedir[ 'dir' ] ) ) {
        $result = $homedir[ 'dir' ];
      }
    }

    if( empty( $result ) ) {
      if( !empty( $_SERVER[ 'HOME' ] ) ) {
        $result = $_SERVER[ 'HOME' ];
      }
      elseif( !empty( getenv( 'HOME' ) ) ) {
        $result = getenv( 'HOME' );
      }
      elseif( function_exists( 'posix_getpwuid' ) &&
              function_exists( 'posix_getuid' ) ) {
        $userInfo = posix_getpwuid( posix_getuid() );

        if( !empty( $userInfo[ 'dir' ] ) ) {
          $result = $userInfo[ 'dir' ];
        }
      }
    }

    return $result;
  }
}