| Conditions | 13 |
| Paths | 151 |
| Total Lines | 87 |
| Code Lines | 57 |
| Lines | 22 |
| Ratio | 25.29 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 28 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 29 | { |
||
| 30 | if (!$this->lock($this->getName())) { |
||
| 31 | $output->writeln('The command is already running in another process.'); |
||
| 32 | |||
| 33 | return 0; |
||
| 34 | } |
||
| 35 | |||
| 36 | $limit = (int)$input->getOption('limit'); |
||
| 37 | Assert::that($limit)->integer()->greaterThan(0); |
||
| 38 | |||
| 39 | $manager = $this->getManager(); |
||
| 40 | |||
| 41 | /** @var Label[] $labels */ |
||
| 42 | $labels = $manager->getRepository('LoevgaardPakkelabelsBundle:Label')->findBy([ |
||
| 43 | 'status' => Label::STATUS_PENDING_NORMALIZATION |
||
| 44 | ], null, $limit); |
||
| 45 | |||
| 46 | if($output->isVerbose()) { |
||
| 47 | $output->writeln('Label count: '.count($labels)); |
||
| 48 | } |
||
| 49 | |||
| 50 | $iso3166 = new ISO3166(); |
||
| 51 | |||
| 52 | foreach ($labels as $label) { |
||
| 53 | $output->writeln('Normalizing label id: '.$label->getId().' with order id: '.$label->getOrderId(), OutputInterface::VERBOSITY_VERBOSE); |
||
| 54 | |||
| 55 | // check that countries are valid ISO 3166 alpha 2 codes |
||
| 56 | $senderCountryValid = false; |
||
| 57 | try { |
||
| 58 | $iso3166->alpha2($label->getSenderCountryCode()); |
||
| 59 | $senderCountryValid = true; |
||
| 60 | } catch (\Exception $e) {} |
||
|
|
|||
| 61 | |||
| 62 | View Code Duplication | if(!$senderCountryValid) { |
|
| 63 | $senderCountry = $this->countryMapping($label->getSenderCountryCode()); |
||
| 64 | if($senderCountry) { |
||
| 65 | $label->setSenderCountryCode($senderCountry); |
||
| 66 | } else { |
||
| 67 | $output->writeln('Sender country code `'.$label->getSenderCountryCode().'` is not a valid ISO 3166 alpha 2 country code. Create a mapping or change the country manually.', OutputInterface::VERBOSITY_VERBOSE); |
||
| 68 | $label->markAsError('Sender country code `'.$label->getSenderCountryCode().'` is not a valid ISO 3166 alpha 2 country code. Create a mapping or change the country manually.'); |
||
| 69 | $manager->flush(); |
||
| 70 | continue; |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | $receiverCountryValid = false; |
||
| 75 | try { |
||
| 76 | $iso3166->alpha2($label->getReceiverCountryCode()); |
||
| 77 | $receiverCountryValid = true; |
||
| 78 | } catch (\Exception $e) {} |
||
| 79 | |||
| 80 | View Code Duplication | if(!$receiverCountryValid) { |
|
| 81 | $receiverCountry = $this->countryMapping($label->getReceiverCountryCode()); |
||
| 82 | if($receiverCountry) { |
||
| 83 | $label->setReceiverCountryCode($receiverCountry); |
||
| 84 | } else { |
||
| 85 | $output->writeln('Receiver country code `'.$label->getReceiverCountryCode().'` is not a valid ISO 3166 alpha 2 country code. Create a mapping or change the country manually.', OutputInterface::VERBOSITY_VERBOSE); |
||
| 86 | $label->markAsError('Receiver country code `'.$label->getReceiverCountryCode().'` is not a valid ISO 3166 alpha 2 country code. Create a mapping or change the country manually.'); |
||
| 87 | $manager->flush(); |
||
| 88 | continue; |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | // check that the shipping method is mapped |
||
| 93 | if($label->getShippingMethod()) { |
||
| 94 | $shippingMethodMapping = $this->shippingMethodMapping($label->getShippingMethod()); |
||
| 95 | if($shippingMethodMapping) { |
||
| 96 | $label->setProductCode($shippingMethodMapping->getProductCode()); |
||
| 97 | if(!empty($shippingMethodMapping->getServiceCodes())) { |
||
| 98 | $label->setServiceCodes(join(',', $shippingMethodMapping->getServiceCodes())); |
||
| 99 | } |
||
| 100 | } else { |
||
| 101 | $output->writeln('Shipping method `'.$label->getShippingMethod().'` is not mapped. Map it manually.', OutputInterface::VERBOSITY_VERBOSE); |
||
| 102 | $label->markAsError('Shipping method `'.$label->getShippingMethod().'` is not mapped. Map it manually.'); |
||
| 103 | $manager->flush(); |
||
| 104 | continue; |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | $label->setStatus(Label::STATUS_PENDING_CREATION); |
||
| 109 | |||
| 110 | $manager->flush(); |
||
| 111 | |||
| 112 | $output->writeln('Label was normalized', OutputInterface::VERBOSITY_VERBOSE); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 162 |