r/PHPhelp • u/syob • Jun 27 '18
Php Object Oriented Design. Need help
I have a list of operations and i need to calculate bank commission for every operation.
Operation types for now: cash in, cash out.
Person types can be: natural or legal. Not sure if there can be more types, but i thinking with in mind, that its possible.
Commission size depends on operation type and what person type made that operation:
Cash in commission for everyone, is 0.07% but not more than 4.00 Eur.
Cash out commission for legal person is 0.2% , but not less than 1.00 Eur.
Cash out commission for natural person is 0.2%, but person can cash out 1000 Eur in a week for free.
Input is csv file. With lines like this:
2015-12-31,1,natural,cash_out,1000.00,EUR
2016-01-05,2,legal,cash_in,200.00,EUR
2016-02-01,1,natural,cash_out,1000.00,EUR
(Second value is user id).
I want to do this using all good oop practices, solid principles etc. So far im doing it like this:
$calculator = new Calculator();
foreach ($data as $row) {
$operation = OperationFactory::create($data); //Returns instance of class CashIn, class CashOut etc
$calculator->addOperation($operation);
}
$results = $calculator->calculate();And now it becomes very confusing to me. If tax price would only depend on operation type, i would do something like this:
class Calculator
{
public function addOperation(Operations\Operation $operation)
{
$this->operations[] = $operation;
}
public function calculate()
{
foreach ($this->operations as $operation) {
$result = $this->operation->amount * $operation->charge / 100;
if ($result < $operation->min) {
$result = $operation->min;
} elseif ($result > $operation->max) {
$result = $operation->max;
}
$results[] = $result;
}
return $result;
}
}I want to make this, that i could easily extend system with new operation types and new person types if neccesary. And this complicates the task for me. Escpecialy that part : "Cash out tax price for natural person is 0.2%, but person can cashout 1000 Eur in a week for free". In which place should i track all of this?
Im doing all this for learning purposes. Any help and tips appreciated.
1
u/syob Jun 27 '18
Normally i would imagine how to calculate that part, but i cant understand where should i put this calculation in overall structure and even how this structure should look like.
I was following this open closed principle example https://scotch.io/bar-talk/s-o-l-i-d-the-first-five-principles-of-object-oriented-design#open-closed-principle .
Now if i want to create a new type of operation, e.g. named transaction. I would make new class Transaction (which extends Operation) with property $charge and set some value to it. Then my calculator class could calculate that. This part i understand.
But now how i need to design this further?When my Transaction charge depends on which person(legal or natural) made that operation.