I have a graph service where I use if/then/else statements.
class GraphService
{
function getGraphData(array $products)
{
foreach ($products as $products)
{
$gotPumpGraph = false;
$model = $product['model'];
$item = $product['model'];
if ($model == "a" || $model == "b")
{
$graph = new APump();
$graph->getGraphData($itemId);
$graphData = $graph;
$gotPumpGraph = true;
}
if ($model == "c" || $model == "d")
{
$graph = new CPump(new SomeDifferentDependency());
$graph->getSomeOtherData($itemId);
$graphData = $graph;
$gotPumpGraph = true;
}
if ($gotPumpGraph)
{
//put graphs into common structure
$graphs[] = array(
'itemId' => $itemId,
'graphData' => $graphData
);
}
}
}
}
What the service is doing is consolidating various graph formats into one. Thus every if
block has different dependencies and structures, that are all consolidated into one standardized structure.
The goal of my question is to find a suitable set of design patterns, to where it can hide the different dependencies found in each if
block....
How?
My first thought is to use a factory but ... I see no clear path to that. The code inside if
blocks is various legacy code that is different for different block and graphs come in different formats. I cleaned up the code a bit for the question, but left different methods/dependencies in the if
blocks.
supports
$model. Then ask to the selected one for a $graph. – Laiv Aug 8 '18 at 5:44APumpGraphProvider
, and so on. – Laiv Aug 8 '18 at 5:52