I want to refactor this code to be clean and solid. Currently I'm a bit stuck how to do this.
Imagine a price calculation: (I simplified the code, I put the elseif
just make it clearer)
class Prices {
function generatePrices($dataSource1) {
$dataSource2 = $dataSource1->getDataSource2();
$cases = $dataSource2->getCases();
foreach($cases as $case) {
if($case->isCaseA()) {
$fee1 = $dataSource2->getFee1();
if($dataSource1->isCaseA_1()) {
$fee2 = $dataSource1->getFee('caseA_1');
if($dataSource1->isCase1()) {
return $fee1 + $fee2;
} elseif($dataSource1->isCase2) {
return $fee1 + $fee2 + $dataSource2->getFee3() + $dataSource2->getFee4();
}
} elseif($dataSource1->isCaseA_2()) {
return $fee1 + $dataSource2->getFee('caseA_2') + $dataSource2->getFee4() + $dataSource2->getFee5();
}
} elseif($case->isCaseB()) {
if($dataSource1->isCaseB_1()) {
$fee = $dataSource2->getFee('caseB_1');
if($dataSource1->isCase1) {
return $fee;
} elseif($dataSource1->isCase2) {
$return $fee + $dataSource1->getFee6();
}
} elseif($dataSource1->isCaseB_2()) {
return $dataSource2->getFee('caseB_2') + $dataSource1->getFee6();
}
}
}
}
}
So I thought about making a Fee
factory which gets all data sources. Depending on the case it will instantiate a concrete FeeCase
class that then will return the appropriate fee. But I don't like how I have to pass all data sources. Yet if I would pass all data needed for all cases I'd have to pass like 10 or more variables.
Any input on this is highly welcome. I'd prefer a path to get to my own solution. But of course if you have a solution at hand I'm grateful about that too.
Aucun commentaire:
Enregistrer un commentaire