<commit_before><?php

namespace CurrencyConverter\Controller\Component;

use Cake\Controller\Component;
use Cake\Datasource\ConnectionManager;
use Cake\ORM\TableRegistry;
use Cake\Database\Schema\TableSchema;

class CurrencyConverterComponent extends Component
{
    private $fromCurrency;

    private $toCurrency;

    private $amount;

    private $hourDifference;

    private $saveIntoDb;

    private $dataSource;

    private $rate;

    private $currencyTable;

    /**
     * Convertion function
     *
     * @param string $fromCurrency the starting currency that user wants to convert to.
     * @param string $toCurrency the ending currency that user wants to convert to.
     * @param float $amount the amount to convert.
     * @param boolean $saveIntoDb if develop wants to store convertion rate for use it without resending data to yahoo service.
     * @param int $hourDifference the hour difference to check if the last convertion is passed, if yes make a new call to yahoo finance api.
     * @param string $dataSource which dataSOurce need to use
     * @return float the total amount converted into the new currency
     */
    public function convert(
        $fromCurrency, 
        $toCurrency, 
        $amount, 
        $saveIntoDb = true, 
        $hourDifference = 1, 
        $dataSource = 'default'
    ) {
        $this->fromCurrency = $fromCurrency;
        $this->toCurrency = $toCurrency;
        $this->amount = $amount;
        $this->saveIntoDb = $saveIntoDb;
        $this->hourDifference = $hourDifference;
        $this->dataSource = $dataSource;
        $this->rate = 0;

        if($this->fromCurrency != $this->toCurrency){
            $this->fixFromToCurrency();
            
            if($this->saveIntoDb == true){
                $this->currencyTable = TableRegistry::get('CurrencyConverter', [
                    'className' => 'CurrencyConverter\Model\Table\CurrencyConvertersTable',
                    'table' => 'currency_converter'
                ]);

                $this->ensureIfExistTable();
                $this->saveIntoDatabase();

                return $this->calculateValue();
            }
            
            $this->rate = $this->getRates($this->fromCurrency, $this->toCurrency);

            return $this->calculateValue();
        }
        
        return number_format((double)$this->amount, 2, '.', '');
    }

    private function fixFromToCurrency()
    {
        if ($this->fromCurrency == "PDS"){
            $this->fromCurrency = "GBP";
        }

        if ($this->toCurrency == "PDS"){
            $this->toCurrency = "GBP";
        }
    }

    private function saveIntoDatabase()
    {
        $query = $this->currencyTable->find('all')
            ->where(['fromCurrency' => $this->fromCurrency, 'toCurrency' => $this->toCurrency ]);

        $query->hydrate(false);
        $result =  $query->toArray();

        foreach ($result as $row){
            $lastUpdated = $row['modified'];
            $now = date('Y-m-d H:i:s');
            $dStart = new \DateTime($now);
            $dEnd = new \DateTime(strtotime($lastUpdated));
            $diff = $dStart->diff($dEnd);

            if ($this->ensureNeedToUpdateDatabase($diff, $row)) {
                $this->updateDatabase($row);
            } else {
                $this->rate = $row['rates'];
            }
        }

        if (count($result) <= 0) {
            $this->insertIntoDatabase();
        }
    }

    private function updateDatabase($row)
    {
        $this->rate = $this->getRates($this->fromCurrency, $this->toCurrency);

        $data = [
            'fromCurrency'=> $this->fromCurrency,
            'toCurrency'  => $this->toCurrency,
            'rates'       => $this->rate,
            'modified'    => date('Y-m-d H:i:s'),
        ];

        $entity = $this->currencyTable->get($row['id']); 
        $this->currencyTable->patchEntity($entity, $data);
        $this->currencyTable->save($entity);
    }
<commit_msg>fixed DateTime conversion<commit_after><?php

namespace CurrencyConverter\Controller\Component;

use Cake\Controller\Component;
use Cake\Datasource\ConnectionManager;
use Cake\ORM\TableRegistry;
use Cake\Database\Schema\TableSchema;

class CurrencyConverterComponent extends Component
{
    private $fromCurrency;

    private $toCurrency;

    private $amount;

    private $hourDifference;

    private $saveIntoDb;

    private $dataSource;

    private $rate;

    private $currencyTable;

    /**
     * Convertion function
     *
     * @param string $fromCurrency the starting currency that user wants to convert to.
     * @param string $toCurrency the ending currency that user wants to convert to.
     * @param float $amount the amount to convert.
     * @param boolean $saveIntoDb if develop wants to store convertion rate for use it without resending data to yahoo service.
     * @param int $hourDifference the hour difference to check if the last convertion is passed, if yes make a new call to yahoo finance api.
     * @param string $dataSource which dataSOurce need to use
     * @return float the total amount converted into the new currency
     */
    public function convert(
        $fromCurrency, 
        $toCurrency, 
        $amount, 
        $saveIntoDb = true, 
        $hourDifference = 1, 
        $dataSource = 'default'
    ) {
        $this->fromCurrency = $fromCurrency;
        $this->toCurrency = $toCurrency;
        $this->amount = $amount;
        $this->saveIntoDb = $saveIntoDb;
        $this->hourDifference = $hourDifference;
        $this->dataSource = $dataSource;
        $this->rate = 0;

        if($this->fromCurrency != $this->toCurrency){
            $this->fixFromToCurrency();
            
            if($this->saveIntoDb == true){
                $this->currencyTable = TableRegistry::get('CurrencyConverter', [
                    'className' => 'CurrencyConverter\Model\Table\CurrencyConvertersTable',
                    'table' => 'currency_converter'
                ]);

                $this->ensureIfExistTable();
                $this->saveIntoDatabase();

                return $this->calculateValue();
            }
            
            $this->rate = $this->getRates($this->fromCurrency, $this->toCurrency);

            return $this->calculateValue();
        }
        
        return number_format((double)$this->amount, 2, '.', '');
    }

    private function fixFromToCurrency()
    {
        if ($this->fromCurrency == "PDS"){
            $this->fromCurrency = "GBP";
        }

        if ($this->toCurrency == "PDS"){
            $this->toCurrency = "GBP";
        }
    }

    private function saveIntoDatabase()
    {
        $query = $this->currencyTable->find('all')
            ->where(['fromCurrency' => $this->fromCurrency, 'toCurrency' => $this->toCurrency ]);

        $query->hydrate(false);
        $result =  $query->toArray();

        foreach ($result as $row){
            $lastUpdated = $row['modified'];

            $now = date('Y-m-d H:i:s');
            $dStart = new \DateTime($now);
            $diff = $dStart->diff($lastUpdated);

            if ($this->ensureNeedToUpdateDatabase($diff, $row)) {
                $this->updateDatabase($row);
            } else {
                $this->rate = $row['rates'];
            }
        }

        if (count($result) <= 0) {
            $this->insertIntoDatabase();
        }
    }

    private function updateDatabase($row)
    {
        $this->rate = $this->getRates($this->fromCurrency, $this->toCurrency);

        $data = [
            'fromCurrency'=> $this->fromCurrency,
            'toCurrency'  => $this->toCurrency,
            'rates'       => $this->rate,
            'modified'    => date('Y-m-d H:i:s'),
        ];

        $entity = $this->currencyTable->get($row['id']); 
        $this->currencyTable->patchEntity($entity, $data);
        $this->currencyTable->save($entity);
    }

{'fraction_non_alphanumeric': 0.13297201915805396, 'fraction_numerical': 0.001008318628686665, 'mean_word_length': 2.112985484503727, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 119, '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': '3746254', 'n_tokens_mistral': 2229, 'n_tokens_neox': 2199, 'n_words': 495}