| Conditions | 7 |
| Paths | 33 |
| Total Lines | 68 |
| Code Lines | 40 |
| 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 |
||
| 26 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 27 | { |
||
| 28 | if (!$this->lock($this->getName())) { |
||
| 29 | $output->writeln('The command is already running in another process.'); |
||
| 30 | |||
| 31 | return 0; |
||
| 32 | } |
||
| 33 | |||
| 34 | $limit = (int) $input->getOption('limit'); |
||
| 35 | Assert::that($limit)->integer()->greaterThan(0); |
||
| 36 | |||
| 37 | /** @var ObjectManager $manager */ |
||
| 38 | $manager = $this->getContainer()->get('doctrine')->getManager(); |
||
| 39 | |||
| 40 | $pakkelabels = $this->getContainer()->get('loevgaard_pakkelabels.client'); |
||
| 41 | |||
| 42 | /** @var Label[] $labels */ |
||
| 43 | $labels = $manager->getRepository('LoevgaardPakkelabelsBundle:Label')->findBy([ |
||
| 44 | 'status' => Label::STATUS_PENDING_CREATION, |
||
| 45 | ], [ |
||
| 46 | 'id' => 'asc' |
||
| 47 | ], $limit); |
||
| 48 | |||
| 49 | if ($output->isVerbose()) { |
||
| 50 | $output->writeln('Label count: '.count($labels)); |
||
| 51 | } |
||
| 52 | |||
| 53 | foreach ($labels as $label) { |
||
| 54 | $output->writeln('Creating label id: '.$label->getId().' with order id: '.$label->getOrderId(), OutputInterface::VERBOSITY_VERBOSE); |
||
| 55 | |||
| 56 | try { |
||
| 57 | $res = $pakkelabels->doRequest('post', '/shipments', [ |
||
| 58 | 'json' => $label->arrayForApi(), |
||
| 59 | ]); |
||
| 60 | |||
| 61 | if (isset($res['error'])) { |
||
| 62 | $output->writeln($res['error'], OutputInterface::VERBOSITY_VERBOSE); |
||
| 63 | $label->markAsError($res['error']); |
||
| 64 | } else { |
||
| 65 | $label->setExternalId($res['id']); |
||
| 66 | |||
| 67 | // download label |
||
| 68 | $labelRes = $pakkelabels->doRequest('get', '/shipments/'.$res['id'].'/labels', [ |
||
| 69 | 'query' => [ |
||
| 70 | 'label_format' => 'png', |
||
| 71 | ], |
||
| 72 | ]); |
||
| 73 | |||
| 74 | if (isset($labelRes['error'])) { |
||
| 75 | $output->writeln($labelRes['error'], OutputInterface::VERBOSITY_VERBOSE); |
||
| 76 | $label->markAsError($labelRes['error']); |
||
| 77 | } else { |
||
| 78 | $labelFileFactory = $this->getContainer()->get('loevgaard_pakkelabels.label_file_factory'); |
||
| 79 | $labelFile = $labelFileFactory->create($label); |
||
| 80 | $labelFile->fwrite(base64_decode($labelRes[0]['base64'])); |
||
| 81 | $label->markAsSuccess(); |
||
| 82 | |||
| 83 | $output->writeln('Label was created', OutputInterface::VERBOSITY_VERBOSE); |
||
| 84 | } |
||
| 85 | } |
||
| 86 | } catch (\Exception $e) { |
||
| 87 | $output->writeln($e->getMessage(), OutputInterface::VERBOSITY_VERBOSE); |
||
| 88 | $label->markAsError('An error occurred during creation of the label. The error was: '.$e->getMessage()); |
||
| 89 | } |
||
| 90 | |||
| 91 | $manager->flush(); |
||
| 92 | } |
||
| 93 | } |
||
| 94 | } |
||
| 95 |