<commit_before><?php namespace Opb\Forex;

use DateTime;
use Money\Money;
use Money\Currency;
use Money\CurrencyPair;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Contracts\Cache\Repository as Cache;

class ForexService{

    use DispatchesJobs;

    const RATE_CACHE_KEY_PREFIX = 'forex_rates:';
    const BACKUP_CACHE_KEY_PREFIX = 'forex_rates_backup:';

    protected $rates = [];

    protected $cache;

    public function __construct(Cache $cache)
    {
        $this->cache = $cache;
    }

    public function getRate(Currency $from, Currency $to, DateTime $date = null)
    {
        $rates = $this->getRatesFromStore($date);
        $dollarFrom = $rates[$from->getName()];

        $rate = $rates[$to->getName()] / $dollarFrom;

        return new CurrencyPair($from, $to, $rate);
    }

    public function convert(Money $from, Currency $to, DateTime $date = null)
    {
        $pair = $this->getRate($from->getCurrency(), $to);

        return $pair->convert($from);
    }

    protected function getRatesFromStore(DateTime $date = null)
    {
        $rateKey = $date ? $date->format('Y-m-d') : 'live';

        // 1. try to get form this object
        if(isset($this->rates[$rateKey])){<commit_msg>force USD to USD rate to be 1.0<commit_after><?php namespace Opb\Forex;

use DateTime;
use Money\Money;
use Money\Currency;
use Money\CurrencyPair;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Contracts\Cache\Repository as Cache;

class ForexService{

    use DispatchesJobs;

    const RATE_CACHE_KEY_PREFIX = 'forex_rates:';
    const BACKUP_CACHE_KEY_PREFIX = 'forex_rates_backup:';

    protected $rates = [];

    protected $cache;

    public function __construct(Cache $cache)
    {
        $this->cache = $cache;
    }

    public function getRate(Currency $from, Currency $to, DateTime $date = null)
    {
        $rates = $this->getRatesFromStore($date);
        $dollarFrom = $from->getName() == 'USD'? 1.0 : $rates[$from->getName()];
        $dollarTo = $to->getName() == 'USD'? 1.0 : $rates[$to->getName()];

        $rate = $dollarTo / $dollarFrom;

        return new CurrencyPair($from, $to, $rate);
    }

    public function convert(Money $from, Currency $to, DateTime $date = null)
    {
        $pair = $this->getRate($from->getCurrency(), $to);

        return $pair->convert($from);
    }

    protected function getRatesFromStore(DateTime $date = null)
    {
        $rateKey = $date ? $date->format('Y-m-d') : 'live';

        // 1. try to get form this object
        if(isset($this->rates[$rateKey])){
{'fraction_non_alphanumeric': 0.14418966187329965, 'fraction_numerical': 0.003109211037699184, 'mean_word_length': 3.125, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1737429', 'n_tokens_mistral': 861, 'n_tokens_neox': 820, 'n_words': 192}