PHP Classes

File: script.php

Recommend this page to a friend!
  Classes of Maniruzzaman Akash   PHP Commission Calculator From CSV File   script.php   Download  
File: script.php
Role: Example script
Content type: text/plain
Description: Example script
Class: PHP Commission Calculator From CSV File
Get transactions from file and compute commissions
Author: By
Last change:
Date: 1 year ago
Size: 1,608 bytes
 

Contents

Class file image Download
<?php

use App\CommissionTask\Currency\CurrencyContainer;
use
App\CommissionTask\Currency\Currency;
use
App\CommissionTask\File\CsvReader;
use
App\CommissionTask\Mapper\TransactionItems;
use
App\CommissionTask\Transaction\Transaction;

require_once
__DIR__ . '/vendor/autoload.php';

/**
 * Get the filename form cmd and run.
 *
 * @example
 * ```sh
 * php script.php
 * input.csv
 * ```
 */
$fileName = fgets(STDIN);

try {
   
// Handle File from terminal
   
$csvData = file_get_contents(__DIR__ . '/' . trim($fileName));
   
$csvReader = new CsvReader();
   
$csvReader->setRows($csvData);

   
/**
     * Add some currencies.
     *
     * We can also process this from Currencies class.
     * To make the process simpler we've added it
     */
   
$baseCurrency = new Currency();
   
$baseCurrency->setCurrency('EUR');

   
$currencyUsd = new Currency();
   
$currencyUsd->setCurrency('USD');

   
$currencyJpy = new Currency();
   
$currencyJpy->setCurrency('JPY')
        ->
setDecimals(0);

   
$currencyData = CurrencyContainer::getInstance();
   
$currencyData->add($baseCurrency)
        ->
add($currencyUsd)
        ->
add($currencyJpy);

   
// Map transaction data into TransactionItem object.
   
$transactionItems = new TransactionItems();
   
$transactionItems->setItems($csvReader->getRows());

   
// Process transaction
   
$transaction = new Transaction($transactionItems);
   
$transaction->processBulk();
    echo
"\n";
    echo
implode("\r\n", $transaction->responses);
    echo
"\n";
} catch (\
Exception $e) {
    echo
'Something went wrong processing. Error: ' . $e->getMessage();
}