Completed
Push — master ( d6c0fd...e0d2a4 )
by Joachim
12:44
created

GenerateLabelsCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 6 1
B execute() 0 31 4
1
<?php
2
3
namespace Loevgaard\DandomainAltapayBundle\Command;
4
5
use Doctrine\ORM\EntityManager;
6
use Loevgaard\PakkelabelsBundle\Entity\Label;
7
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
8
use Symfony\Component\Console\Command\LockableTrait;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class GenerateLabelsCommand extends ContainerAwareCommand
13
{
14
    use LockableTrait;
15
16
    protected function configure()
17
    {
18
        $this->setName('loevgaard:pakkelabels:generate-labels')
19
            ->setDescription('Generates labels')
20
        ;
21
    }
22
23
    protected function execute(InputInterface $input, OutputInterface $output)
24
    {
25
        if (!$this->lock($this->getName())) {
26
            $output->writeln('The command is already running in another process.');
27
28
            return 0;
29
        }
30
31
        /** @var EntityManager $em */
32
        $em = $this->getContainer()->get('doctrine')->getManager();
33
34
        $pakkelabels = $this->getContainer()->get('loevgaard_pakkelabels.client');
35
36
        $labels = $em->getRepository('LoevgaardPakkelabelsBundle:Label')->findBy([
37
            'status' => Label::STATUS_PENDING_CREATION
38
        ], null, 20);
39
40
        foreach ($labels as $label) {
41
            $res = $pakkelabels->doRequest('post', '/shipments', [
42
                'json' => $label->arrayForApi()
43
            ]);
44
45
            if(isset($res['error'])) {
46
                $label->markAsError($res['error']);
47
            } else {
48
                $label->markAsSuccess();
49
            }
50
51
            $em->flush();
52
        }
53
    }
54
}
55