Simple Symfony currency exchange demo application (CLI)

Overview

Symfony currency exchange demo

Keynotes

  • Using a small Symfony installation as possible
  • Using SQLite database for simplicity but with price of some caveats
    • There are no real decimal or numeric data type so emulation with harcoded 16,8 precision is used
    • There are no INSERT IGNORE method so unique constraint violation are just skipped
  • There is \App\Services\RatesUpdater::makeReverseRate method to make reverse exchange rate. However, separate buy and sell rates should be used in real life.
  • There are two tables for rates: rates with provided data and pairs with all currencies combinations ( triangles). Triangulations are implemented in the \App\Services\RatesTriangulator service. It's inspired by http://www.dpxo.net/articles/fx_rate_triangulation_sql.html
  • There was some date errors in ECB rates so rates' date are writing to the tables but not using in the finally exchange calculation
  • There are simple functional tests with fake data from \App\Providers\FakeRatesProvider provider

There are two commands

  • currency:update - Update currencies exchange rates
  • currency:exchange - Exchange currency

And two installation & usage ways: local PHP and Docker.

1. Local-way

Requirements

  • PHP 8.1
    • ext-bcmath
    • ext-ctype
    • ext-iconv
    • ext-intl
    • ext-pdo_sqlite
    • ext-simplexml
    • ext-sqlite3
  • Composer

Installation

git clone https://github.com/vladimirmartsul/symfony-exchange-demo.git
cd symfony-exchange-demo
composer install --no-dev --no-interaction
php bin/console doctrine:database:create
php bin/console doctrine:migrations:migrate --no-interaction

Usage

Update currency rates first

php bin/console currency:update

Use

php bin/console currency:exchange <amount> <from> <to>

For example

php bin/console currency:exchange 2 EUR BTC

should output

[OK] 2 EUR is 0.00005254 BTC

Testing

cd symfony-exchange-demo
echo APP_ENV=test > .env.local
composer install --no-interaction
php bin/console doctrine:database:create
php bin/console doctrine:migrations:migrate --no-interaction
php bin/phpunit

2. Docker-way

Requirements

Docker Desktop (Windows, MacOS) or docker-cli and docker-compose (Linux)

Installation

git clone https://[email protected]/vladimirmartsul/symfony-exchange-demo.git
cd symfony-exchange-demo
docker compose up --build

Currency rates will be updated during build.

Usage

docker compose run symfony-exchange-demo currency:exchange <amount> <from> <to>

For example

docker compose run symfony-exchange-demo currency:exchange 2 EUR BTC

should output

[OK] 2 EUR is 0.00005254 BTC

Testing

cd symfony-exchange-demo
echo APP_ENV=test > .env.local
docker compose run symfony-exchange-demo composer install --no-interaction
docker compose run symfony-exchange-demo doctrine:database:create
docker compose run symfony-exchange-demo doctrine:migrations:migrate --no-interaction
docker compose run symfony-exchange-demo bin/phpunit

Extending

There is ability to add new exchange rates providers.

The new provider must implement \App\Contracts\RatesProviderInterface interface with constructor __construct(\Symfony\Contracts\HttpClient\HttpClientInterface $client, string $url, string $base) and __invoke(): App\Dto\Rate[] method.

However, in many cases it is enough to extend abstract \App\Providers\RatesProvider class and implement \App\Providers\RatesProvider::transform(array $data): RateDto[] method.

Additionaly the new provider must be registered in config/services.yaml with its own arguments and app.rates_provider tag. Real params such as URL and base currency must be writen in .env file.

You might also like...
Dockerise Symfony Application (Symfony 6 + Clean Architecture+ DDD+ CQRS + Docker + Xdebug + PHPUnit + Doctrine ORM + JWT Auth + Static analysis)

Symfony Dockerise Symfony Application Install Docker Install Docker Compose Docker PHP & Nginx Create Symfony Application Debugging Install Xdebug Con

Simple Symfony API-Platform Template which you can use to start to develop with symfony and api-platform

symfony-api-platform-skeleton Simple Template for Symfony API You can fork it and change the git remote to your Repo git remote set-url your-git-remo

Vulnerable demo application for the race condition

Vulnerable PHP App (Race Condition) Environment setup: docker-compose up Environment verification: Connection Test: http://localhost/test.php Vulnera

 Phalcon Demo Application
Phalcon Demo Application

Phalcon Demo Application We use modified Phalcon INVO Application to demonstrate basics of Codeception testing. We expect to implement as many feature

Notejam demo application using Blink framework

Notejam: Blink framework Notejam demo application using Blink framework Installation Clone the repository to you machine. Run vagrant up to create the

PHP demo application showing how to access the Hubstaff Public API

Public API PHP Demo This repository contains a simple sample cli tool that shows off how to access the Hubstaff public api via PHP. The core access is

A PHP CLI application that helps you organize your aliases.
A PHP CLI application that helps you organize your aliases.

Alias CLI A PHP CLI application that helps you organize your aliases. Installation composer require --dev alexgaal/alias After installing Alias CLI we

Simple HTTP smoke testing for your Symfony application

Shopsys HTTP Smoke Testing This package enables you to do simple HTTP smoke testing of your Symfony application. Basically, it generates a HTTP reques

The slides and demo files for my Alpine Day 2021 talk

Building a Better Dialog Austen Cameron - @austencam This repository contains the slides and demos for my talk from Alpine Day 2021. Below, you'll fin

Owner
Vladimir Martsul
Vladimir Martsul
Standardized wrapper for popular currency rate APIs. Currently supports FixerIO, CurrencyLayer, Open Exchange Rates and Exchange Rates API.

?? Wrapper for popular Currency Exchange Rate APIs A PHP API Wrapper to offer a unified programming interface for popular Currency Rate APIs. Dont wor

Alexander Graf 24 Nov 21, 2022
World Currency list in PHP constants and in array (Currency::USD)

World Currency list in PHP constants and in array (Currency::USD) If you need to work with currencies in the code and describe each time "USD", "EUR"

Krepysh 4 Jun 27, 2022
This document provides the details related to Remittance API. This APIs is used to initiate payment request from Mobile client/others exchange house.

City Bank Remittance API This is where your description should go. Limit it to a paragraph or two. Consider adding a small example. Installation You c

MD ARIFUL HAQUE 2 Oct 2, 2022
Demo of symfony/console features to build rich command line utilities

Symfony Console Demo This project is a demonstration of symfony/console features. To run the project with Docker. With bash: docker build -t myconsole

Jérôme Tamarelle 3 Jun 16, 2022
This demo app shows you how to run a simple PHP application on AWS Elastic Beanstalk.

Elastic Beanstalk + PHP Demo App - "Share Your Thoughts" This demo app shows you how to run a simple PHP application on AWS Elastic Beanstalk. Run the

AWS Samples 143 Nov 26, 2022
Library download currency rate and save in database, It's designed to be extended by any available data source.

Library download currency rate and save in database, It's designed to be extended by any available data source.

Flexmind. Krzysztof Bielecki 2 Oct 6, 2021
A money and currency library for PHP

Brick\Money A money and currency library for PHP. Introduction Working with financial data is a serious matter, and small rounding mistakes in an appl

Brick 1.3k Jan 5, 2023
Extract and evolution of the magento2-currency-precision module from the magento2-jp project from @Magento

Currency Precision Module for Magento 2 This module aims to help merchants to manage easily their currency precision in Magento 2. DISCLAIMER Initiall

OpenGento 3 Dec 17, 2021
Buy and sell crypto top 100 crypto with our fake currency. Donate to and use our referal links for discounts

PLEASE ENABLE SQLITE3 AND GD OR GD2 IN XAMPP TO RUN THE APP! (SEE HOW_TO_SETUP_XAMPP.gif) ![alt text](https://github.com/Tby23rd/Project1-Cryptosimul

Tabitha Maru 0 Dec 26, 2021
Xero - a digital currency that allows instant payments to anyone, anywhere

Xeros is a digital currency that allows instant payments to anyone, anywhere. Xeros has been written completely in PHP and mostly follows the technical design of Bitcoin. Xeros uses P2P technology to operate with no central server.

Kladskull 79 Dec 26, 2022