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/stilloriginal Jun 28 '18 edited Jun 28 '18
I’m just a n00b but to me this is all business logic that should live inside the model class and you should test it using mock data while wring the methods. Like your calculator class is really just a method. And it returns inside the foreach loop so im not sure it would even work. But like I said I might be thinking too basic. Also, to me the results belong in a materialized view of somesort but that might be complicating things.