Changeset View
Changeset View
Standalone View
Standalone View
src/app/Utils.php
<?php | <?php | ||||
namespace App; | namespace App; | ||||
use Carbon\Carbon; | use Carbon\Carbon; | ||||
use Illuminate\Support\Facades\Auth; | use Illuminate\Support\Facades\Auth; | ||||
use Ramsey\Uuid\Uuid; | use Ramsey\Uuid\Uuid; | ||||
use Illuminate\Support\Facades\Cache; | |||||
/** | /** | ||||
* Small utility functions for App. | * Small utility functions for App. | ||||
*/ | */ | ||||
class Utils | class Utils | ||||
{ | { | ||||
// Note: Removed '0', 'O', '1', 'I' as problematic with some fonts | // Note: Removed '0', 'O', '1', 'I' as problematic with some fonts | ||||
public const CHARS = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ'; | public const CHARS = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ'; | ||||
▲ Show 20 Lines • Show All 372 Lines • ▼ Show 20 Lines | public static function uiEnv(): array | ||||
$env['paymentProvider'] = \config('services.payment_provider'); | $env['paymentProvider'] = \config('services.payment_provider'); | ||||
$env['stripePK'] = \config('services.stripe.public_key'); | $env['stripePK'] = \config('services.stripe.public_key'); | ||||
$env['languages'] = \App\Http\Controllers\ContentController::locales(); | $env['languages'] = \App\Http\Controllers\ContentController::locales(); | ||||
$env['menu'] = \App\Http\Controllers\ContentController::menu(); | $env['menu'] = \App\Http\Controllers\ContentController::menu(); | ||||
return $env; | return $env; | ||||
} | } | ||||
/** | |||||
* Retrieve an exchange rate. | |||||
* | |||||
* @param string $sourceCurrency: Currency from which to convert | |||||
* @param string $targetCurrency: Currency to convert to | |||||
* | |||||
machniak: Again, the phpdoc formatting is not correct. | |||||
* @return float Exchange rate | |||||
*/ | |||||
public static function exchangeRate(string $sourceCurrency, string $targetCurrency): float | |||||
{ | |||||
if (strcasecmp($sourceCurrency, $targetCurrency) == 0) { | |||||
return 1.0; | |||||
} | |||||
$currencyFile = resource_path("exchangerates-$sourceCurrency.php"); | |||||
//Attempt to find the reverse exchange rate, if we don't have the file for the source currency | |||||
if (!file_exists($currencyFile)) { | |||||
$rates = include resource_path("exchangerates-$targetCurrency.php"); | |||||
if (!isset($rates[$sourceCurrency])) { | |||||
throw new \Exception("Failed to find the reverse exchange rate for " . $sourceCurrency); | |||||
} | |||||
return 1.0 / floatval($rates[$sourceCurrency]); | |||||
Done Inline ActionsI think Jeroen and I would prefer to do this differently. When fetching the exchange rates we rather use cron and store the rates in a file (or DB). This way a failing request to openexchangerates website does not cause issues for our users. In such cases we'd just use the old rates instead of throwing an exception. machniak: I think Jeroen and I would prefer to do this differently. When fetching the exchange rates we… | |||||
Done Inline ActionsSo is that a data:import:exchangerate artisan command? How do I store and load something like the exchangerates in the db best? mollekopf: So is that a data:import:exchangerate artisan command?
How do I store and load something like… | |||||
Done Inline ActionsI think I could follow either what ip4net does and store it in the db or what countriescommand does and store it in a file. Up to you. mollekopf: I think I could follow either what ip4net does and store it in the db or what countriescommand… | |||||
Done Inline ActionsI think a file would be fine. machniak: I think a file would be fine. | |||||
} | |||||
$rates = include $currencyFile; | |||||
if (!isset($rates[$targetCurrency])) { | |||||
throw new \Exception("Failed to find exchange rate for " . $targetCurrency); | |||||
} | |||||
return floatval($rates[$targetCurrency]); | |||||
} | |||||
} | } |
Again, the phpdoc formatting is not correct.