Conditions | 12 |
Paths | 76 |
Total Lines | 72 |
Code Lines | 48 |
Lines | 0 |
Ratio | 0 % |
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 |
||
25 | protected function execute(InputInterface $input, OutputInterface $output) |
||
26 | { |
||
27 | if (!$this->lock($this->getName())) { |
||
28 | $output->writeln('The command is already running in another process.'); |
||
29 | |||
30 | return 0; |
||
31 | } |
||
32 | |||
33 | $em = $this->getEntityManager(); |
||
34 | |||
35 | $labels = $em->getRepository('LoevgaardPakkelabelsBundle:Label')->findBy([ |
||
36 | 'status' => Label::STATUS_PENDING_NORMALIZATION |
||
37 | ], null, 20); |
||
38 | |||
39 | $iso3166 = new ISO3166(); |
||
40 | |||
41 | foreach ($labels as $label) { |
||
42 | // check that countries are valid ISO 3166 alpha 2 codes |
||
43 | $senderCountryValid = false; |
||
44 | try { |
||
45 | $iso3166->alpha2($label->getSenderCountryCode()); |
||
46 | $senderCountryValid = true; |
||
47 | } catch (\Exception $e) {} |
||
|
|||
48 | |||
49 | if(!$senderCountryValid) { |
||
50 | $senderCountry = $this->countryMapping($label->getSenderCountryCode()); |
||
51 | if($senderCountry) { |
||
52 | $label->setSenderCountryCode($senderCountry); |
||
53 | } else { |
||
54 | $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.'); |
||
55 | $em->flush(); |
||
56 | continue; |
||
57 | } |
||
58 | } |
||
59 | |||
60 | $receiverCountryValid = false; |
||
61 | try { |
||
62 | $iso3166->alpha2($label->getReceiverCountryCode()); |
||
63 | $receiverCountryValid = true; |
||
64 | } catch (\Exception $e) {} |
||
65 | |||
66 | if(!$receiverCountryValid) { |
||
67 | $receiverCountry = $this->countryMapping($label->getReceiverCountryCode()); |
||
68 | if($receiverCountry) { |
||
69 | $label->setReceiverCountryCode($receiverCountry); |
||
70 | } else { |
||
71 | $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.'); |
||
72 | $em->flush(); |
||
73 | continue; |
||
74 | } |
||
75 | } |
||
76 | |||
77 | // check that the shipping method is mapped |
||
78 | if($label->getShippingMethod()) { |
||
79 | $shippingMethodMapping = $this->shippingMethodMapping($label->getShippingMethod()); |
||
80 | if($shippingMethodMapping) { |
||
81 | $label->setProductCode($shippingMethodMapping->getProductCode()); |
||
82 | if(!empty($shippingMethodMapping->getServiceCodes())) { |
||
83 | $label->setServiceCodes(join(',', $shippingMethodMapping->getServiceCodes())); |
||
84 | } |
||
85 | } else { |
||
86 | $label->markAsError('Shipping method `'.$label->getShippingMethod().'` is not mapped. Map it manually.'); |
||
87 | $em->flush(); |
||
88 | continue; |
||
89 | } |
||
90 | } |
||
91 | |||
92 | $label->setStatus(Label::STATUS_PENDING_CREATION); |
||
93 | |||
94 | $em->flush(); |
||
95 | } |
||
96 | } |
||
97 | |||
145 |