Completed
Push — master ( 20e80f...9e4566 )
by Joachim
08:10
created

GenerateLabelsCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 7
dl 0
loc 44
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A configure() 0 7 1
1
<?php
2
3
namespace Loevgaard\PakkelabelsBundle\Command;
4
5
use Assert\Assert;
6
use Doctrine\Common\Persistence\ObjectManager;
7
use Loevgaard\PakkelabelsBundle\Entity\Label;
8
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
9
use Symfony\Component\Console\Command\LockableTrait;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
class GenerateLabelsCommand extends ContainerAwareCommand
15
{
16
    use LockableTrait;
17
18
    protected function configure()
19
    {
20
        $this->setName('loevgaard:pakkelabels:generate-labels')
21
            ->setDescription('Generates labels')
22
            ->addOption('limit', 'l', InputOption::VALUE_REQUIRED, 'The number of labels to normalize', 20)
23
        ;
24
    }
25
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