Conditions | 7 |
Paths | 29 |
Total Lines | 63 |
Code Lines | 36 |
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 | ], null, $limit); |
||
46 | |||
47 | if($output->isVerbose()) { |
||
48 | $output->writeln('Label count: '.count($labels)); |
||
49 | } |
||
50 | |||
51 | foreach ($labels as $label) { |
||
52 | $output->writeln('Creating label id: '.$label->getId().' with order id: '.$label->getOrderId(), OutputInterface::VERBOSITY_VERBOSE); |
||
53 | |||
54 | try { |
||
55 | $res = $pakkelabels->doRequest('post', '/shipments', [ |
||
56 | 'json' => $label->arrayForApi() |
||
57 | ]); |
||
58 | |||
59 | if (isset($res['error'])) { |
||
60 | $label->markAsError($res['error']); |
||
61 | } else { |
||
62 | $label->setExternalId($res['id']); |
||
63 | |||
64 | // download label |
||
65 | $labelRes = $pakkelabels->doRequest('get', '/shipments/' . $res['id'] . '/labels', [ |
||
66 | 'query' => [ |
||
67 | 'label_format' => 'png' |
||
68 | ] |
||
69 | ]); |
||
70 | |||
71 | if (isset($labelRes['error'])) { |
||
72 | $label->markAsError($labelRes['error']); |
||
73 | } else { |
||
74 | $labelFileFactory = $this->getContainer()->get('loevgaard_pakkelabels.label_file_factory'); |
||
75 | $labelFile = $labelFileFactory->create($label); |
||
76 | $labelFile->fwrite(base64_decode($labelRes['base64'])); |
||
77 | $label->markAsSuccess(); |
||
78 | |||
79 | $output->writeln('Label was created', OutputInterface::VERBOSITY_VERBOSE); |
||
80 | } |
||
81 | } |
||
82 | } catch (\Exception $e) { |
||
83 | $label->markAsError('An error occurred during creation of the label. The error was: '.$e->getMessage()); |
||
84 | } |
||
85 | |||
86 | $manager->flush(); |
||
87 | } |
||
88 | } |
||
89 | } |
||
90 |