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
|
|
|
], [ |
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
|
|
|
|