Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 35 | class LoadLeadsData extends AbstractFixture implements ContainerAwareInterface, OrderedFixtureInterface |
||
| 36 | { |
||
| 37 | const FLUSH_MAX = 50; |
||
| 38 | |||
| 39 | /** @var ContainerInterface */ |
||
| 40 | protected $container; |
||
| 41 | |||
| 42 | /** @var User[] */ |
||
| 43 | protected $users; |
||
| 44 | |||
| 45 | /** @var Country[] */ |
||
| 46 | protected $countries; |
||
| 47 | |||
| 48 | /** @var WorkflowManager */ |
||
| 49 | protected $workflowManager; |
||
| 50 | |||
| 51 | /** @var EntityManager */ |
||
| 52 | protected $em; |
||
| 53 | |||
| 54 | /** @var Organization */ |
||
| 55 | protected $organization; |
||
| 56 | |||
| 57 | /** @var BuilderFactory */ |
||
| 58 | protected $channelBuilderFactory; |
||
| 59 | |||
| 60 | /** @var Channel */ |
||
| 61 | protected $channel; |
||
| 62 | |||
| 63 | /** @var AbstractEnumValue[] */ |
||
| 64 | protected $sources; |
||
| 65 | |||
| 66 | |||
| 67 | /** |
||
| 68 | * {@inheritDoc} |
||
| 69 | */ |
||
| 70 | public function setContainer(ContainerInterface $container = null) |
||
| 71 | { |
||
| 72 | $this->container = $container; |
||
| 73 | $this->workflowManager = $container->get('oro_workflow.manager'); |
||
|
|
|||
| 74 | $this->channelBuilderFactory = $container->get('orocrm_channel.builder.factory'); |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * {@inheritDoc} |
||
| 79 | */ |
||
| 80 | public function load(ObjectManager $manager) |
||
| 81 | { |
||
| 82 | $this->organization = $manager->getRepository('OroOrganizationBundle:Organization')->getFirst(); |
||
| 83 | $this->initSupportingEntities($manager); |
||
| 84 | $this->loadLeads($manager); |
||
| 85 | } |
||
| 86 | |||
| 87 | protected function initSupportingEntities(ObjectManager $manager = null) |
||
| 88 | { |
||
| 89 | if ($manager) { |
||
| 90 | $this->em = $manager; |
||
| 91 | } |
||
| 92 | |||
| 93 | $this->users = $this->em->getRepository('OroUserBundle:User')->findAll(); |
||
| 94 | $this->countries = $this->em->getRepository('OroAddressBundle:Country')->findAll(); |
||
| 95 | |||
| 96 | $className = ExtendHelper::buildEnumValueClassName('lead_source'); |
||
| 97 | $enumRepo = $manager->getRepository($className); |
||
| 98 | $this->sources = $enumRepo->findAll(); |
||
| 99 | |||
| 100 | $this->channel = $this |
||
| 101 | ->channelBuilderFactory |
||
| 102 | ->createBuilder() |
||
| 103 | ->setChannelType(DefaultChannelData::B2B_CHANNEL_TYPE) |
||
| 104 | ->setStatus(Channel::STATUS_ACTIVE) |
||
| 105 | ->setEntities() |
||
| 106 | ->getChannel(); |
||
| 107 | |||
| 108 | $manager->persist($this->channel); |
||
| 109 | $manager->flush($this->channel); |
||
| 110 | } |
||
| 111 | |||
| 112 | public function loadLeads(ObjectManager $manager) |
||
| 113 | { |
||
| 114 | $handle = fopen(__DIR__ . DIRECTORY_SEPARATOR . 'dictionaries' . DIRECTORY_SEPARATOR . "leads.csv", "r"); |
||
| 115 | View Code Duplication | if ($handle) { |
|
| 116 | $headers = []; |
||
| 117 | if (($data = fgetcsv($handle, 1000, ",")) !== false) { |
||
| 118 | //read headers |
||
| 119 | $headers = $data; |
||
| 120 | } |
||
| 121 | $randomUser = count($this->users) - 1; |
||
| 122 | $i = 0; |
||
| 123 | while (($data = fgetcsv($handle, 1000, ",")) !== false) { |
||
| 124 | $user = $this->users[mt_rand(0, $randomUser)]; |
||
| 125 | $this->setSecurityContext($user); |
||
| 126 | |||
| 127 | $data = array_combine($headers, array_values($data)); |
||
| 128 | |||
| 129 | $lead = $this->createLead($manager, $data, $user); |
||
| 130 | $this->em->persist($lead); |
||
| 131 | |||
| 132 | $this->loadSalesFlows($lead); |
||
| 133 | |||
| 134 | $i++; |
||
| 135 | if ($i % self::FLUSH_MAX == 0) { |
||
| 136 | $this->em->flush(); |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | $this->em->flush(); |
||
| 141 | fclose($handle); |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param Lead $lead |
||
| 147 | */ |
||
| 148 | protected function loadSalesFlows(Lead $lead) |
||
| 149 | { |
||
| 150 | $leadWorkflowItem = $this->workflowManager->startWorkflow( |
||
| 151 | 'b2b_flow_lead', |
||
| 152 | $lead, |
||
| 153 | 'qualify', |
||
| 154 | [ |
||
| 155 | 'opportunity_name' => $lead->getName(), |
||
| 156 | 'company_name' => $lead->getCompanyName(), |
||
| 157 | ] |
||
| 158 | ); |
||
| 159 | //@TODO change test according to CRM-6344 |
||
| 160 | if ($this->getRandomBoolean()) { |
||
| 161 | /** @var Opportunity $opportunity */ |
||
| 162 | $opportunity = $leadWorkflowItem->getResult()->get('opportunity'); |
||
| 163 | $budgetAmount = MultiCurrency::create(mt_rand(10, 10000), 'USD'); |
||
| 164 | $closeRevenue = MultiCurrency::create(mt_rand(10, 10000), 'USD'); |
||
| 165 | $salesFlowItem = $this->workflowManager->startWorkflow( |
||
| 166 | 'opportunity_flow', |
||
| 167 | $opportunity, |
||
| 168 | '__start__', |
||
| 169 | [ |
||
| 170 | 'budget_amount' => $budgetAmount, |
||
| 171 | 'customer_need' => mt_rand(10, 10000), |
||
| 172 | 'proposed_solution' => mt_rand(10, 10000), |
||
| 173 | 'probability' => round(mt_rand(50, 85) / 100.00, 2) |
||
| 174 | ] |
||
| 175 | ); |
||
| 176 | |||
| 177 | if ($this->getRandomBoolean()) { |
||
| 178 | if ($this->getRandomBoolean()) { |
||
| 179 | $this->transit( |
||
| 180 | $this->workflowManager, |
||
| 181 | $salesFlowItem, |
||
| 182 | 'close_won', |
||
| 183 | [ |
||
| 184 | 'close_revenue' => $closeRevenue, |
||
| 185 | 'close_date' => new \DateTime('now'), |
||
| 186 | ] |
||
| 187 | ); |
||
| 188 | } else { |
||
| 189 | $this->transit( |
||
| 190 | $this->workflowManager, |
||
| 191 | $salesFlowItem, |
||
| 192 | 'close_lost', |
||
| 193 | [ |
||
| 194 | 'close_reason_name' => 'cancelled', |
||
| 195 | 'close_revenue' => $closeRevenue, |
||
| 196 | 'close_date' => new \DateTime('now'), |
||
| 197 | ] |
||
| 198 | ); |
||
| 199 | } |
||
| 200 | } |
||
| 201 | } |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @return bool |
||
| 206 | */ |
||
| 207 | protected function getRandomBoolean() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param User $user |
||
| 214 | */ |
||
| 215 | View Code Duplication | protected function setSecurityContext($user) |
|
| 222 | |||
| 223 | /** |
||
| 224 | * @param ObjectManager $manager |
||
| 225 | * @param array $data |
||
| 226 | * @param User $user |
||
| 227 | * |
||
| 228 | * @return Lead |
||
| 229 | */ |
||
| 230 | protected function createLead(ObjectManager $manager, array $data, $user) |
||
| 231 | { |
||
| 232 | $lead = new Lead(); |
||
| 233 | |||
| 234 | $className = ExtendHelper::buildEnumValueClassName(Lead::INTERNAL_STATUS_CODE); |
||
| 235 | $defaultStatus = $manager->getRepository($className)->find(ExtendHelper::buildEnumValueId('new')); |
||
| 236 | |||
| 237 | $lead->setStatus($defaultStatus); |
||
| 238 | $lead->setName($data['Company']); |
||
| 239 | $lead->setFirstName($data['GivenName']); |
||
| 240 | $lead->setLastName($data['Surname']); |
||
| 241 | |||
| 242 | $leadEmail = new LeadEmail($data['EmailAddress']); |
||
| 243 | $leadEmail->setPrimary(true); |
||
| 244 | $lead->addEmail($leadEmail); |
||
| 245 | |||
| 246 | $leadPhone = new LeadPhone($data['TelephoneNumber']); |
||
| 247 | $leadPhone->setPrimary(true); |
||
| 248 | $lead->addPhone($leadPhone); |
||
| 249 | |||
| 250 | $lead->setCompanyName($data['Company']); |
||
| 251 | $lead->setOwner($user); |
||
| 252 | $lead->setDataChannel($this->channel); |
||
| 253 | /** @var LeadAddress $address */ |
||
| 254 | $address = new LeadAddress(); |
||
| 255 | $address->setLabel('Primary Address'); |
||
| 256 | $address->setCity($data['City']); |
||
| 257 | $address->setStreet($data['StreetAddress']); |
||
| 258 | $address->setPostalCode($data['ZipCode']); |
||
| 259 | $address->setFirstName($data['GivenName']); |
||
| 260 | $address->setLastName($data['Surname']); |
||
| 261 | |||
| 262 | $isoCode = $data['Country']; |
||
| 263 | $country = array_filter( |
||
| 264 | $this->countries, |
||
| 265 | function (Country $a) use ($isoCode) { |
||
| 266 | return $a->getIso2Code() == $isoCode; |
||
| 267 | } |
||
| 268 | ); |
||
| 269 | |||
| 270 | $country = array_values($country); |
||
| 271 | /** @var Country $country */ |
||
| 272 | $country = $country[0]; |
||
| 273 | |||
| 274 | $idRegion = $data['State']; |
||
| 275 | /** @var Collection $regions */ |
||
| 276 | $regions = $country->getRegions(); |
||
| 277 | |||
| 278 | $region = $regions->filter( |
||
| 279 | function (Region $a) use ($idRegion) { |
||
| 280 | return $a->getCode() == $idRegion; |
||
| 281 | } |
||
| 282 | ); |
||
| 283 | |||
| 284 | $address->setCountry($country); |
||
| 285 | if (!$region->isEmpty()) { |
||
| 286 | $address->setRegion($region->first()); |
||
| 287 | } |
||
| 288 | |||
| 289 | $lead->addAddress($address); |
||
| 290 | |||
| 291 | $countSources = count($this->sources) - 1; |
||
| 292 | $source = $this->sources[mt_rand(0, $countSources)]; |
||
| 293 | $lead->setSource($source); |
||
| 294 | |||
| 295 | return $lead; |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param WorkflowManager $workflowManager |
||
| 300 | * @param WorkflowItem $workflowItem |
||
| 301 | * @param string $transition |
||
| 302 | * @param array $data |
||
| 303 | */ |
||
| 304 | protected function transit($workflowManager, $workflowItem, $transition, array $data) |
||
| 315 | |||
| 316 | public function getOrder() |
||
| 320 | } |
||
| 321 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: