The Stack Overflow podcast is back! Listen to an interview with our new CEO.
2 added 30 characters in body
source | link

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 the goal remainsleft different methods/dependencies in the sameif blocks. 

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 the goal remains the same.

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. 

1
source | link

How do I refactor a graph service to where I can encapsulate if/then/else statements?

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 the goal remains the same.