1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Loevgaard\DandomainAltapayBundle\Command; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
6
|
|
|
use League\ISO3166\ISO3166; |
7
|
|
|
use Loevgaard\PakkelabelsBundle\Entity\Label; |
8
|
|
|
use Loevgaard\PakkelabelsBundle\Entity\ShippingMethodMapping; |
9
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
10
|
|
|
use Symfony\Component\Console\Command\LockableTrait; |
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
|
14
|
|
|
class NormalizeLabelsCommand extends ContainerAwareCommand |
15
|
|
|
{ |
16
|
|
|
use LockableTrait; |
17
|
|
|
|
18
|
|
|
protected function configure() |
19
|
|
|
{ |
20
|
|
|
$this->setName('loevgaard:pakkelabels:normalize-labels') |
21
|
|
|
->setDescription('Normalizes labels before creation') |
22
|
|
|
; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
26
|
|
|
{ |
27
|
|
|
if (!$this->lock($this->getName())) { |
28
|
|
|
$output->writeln('The command is already running in another process.'); |
29
|
|
|
|
30
|
|
|
return 0; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$em = $this->getEntityManager(); |
34
|
|
|
|
35
|
|
|
$labels = $em->getRepository('LoevgaardPakkelabelsBundle:Label')->findBy([ |
36
|
|
|
'status' => Label::STATUS_PENDING_NORMALIZATION |
37
|
|
|
], null, 20); |
38
|
|
|
|
39
|
|
|
$iso3166 = new ISO3166(); |
40
|
|
|
|
41
|
|
|
foreach ($labels as $label) { |
42
|
|
|
// check that countries are valid ISO 3166 alpha 2 codes |
43
|
|
|
$senderCountryValid = false; |
44
|
|
|
try { |
45
|
|
|
$iso3166->alpha2($label->getSenderCountryCode()); |
46
|
|
|
$senderCountryValid = true; |
47
|
|
|
} catch (\Exception $e) {} |
|
|
|
|
48
|
|
|
|
49
|
|
|
if(!$senderCountryValid) { |
50
|
|
|
$senderCountry = $this->countryMapping($label->getSenderCountryCode()); |
51
|
|
|
if($senderCountry) { |
|
|
|
|
52
|
|
|
$label->setSenderCountryCode($senderCountry); |
53
|
|
|
} else { |
54
|
|
|
$label->markAsError('Sender country code `'.$label->getSenderCountryCode().'` is not a valid ISO 3166 alpha 2 country code. Create a mapping or change the country manually.'); |
55
|
|
|
$em->flush(); |
56
|
|
|
continue; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$receiverCountryValid = false; |
61
|
|
|
try { |
62
|
|
|
$iso3166->alpha2($label->getReceiverCountryCode()); |
63
|
|
|
$receiverCountryValid = true; |
64
|
|
|
} catch (\Exception $e) {} |
|
|
|
|
65
|
|
|
|
66
|
|
|
if(!$receiverCountryValid) { |
67
|
|
|
$receiverCountry = $this->countryMapping($label->getReceiverCountryCode()); |
68
|
|
|
if($receiverCountry) { |
|
|
|
|
69
|
|
|
$label->setReceiverCountryCode($receiverCountry); |
70
|
|
|
} else { |
71
|
|
|
$label->markAsError('Receiver country code `'.$label->getReceiverCountryCode().'` is not a valid ISO 3166 alpha 2 country code. Create a mapping or change the country manually.'); |
72
|
|
|
$em->flush(); |
73
|
|
|
continue; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// check that the shipping method is mapped |
78
|
|
|
if($label->getShippingMethod()) { |
79
|
|
|
$shippingMethodMapping = $this->shippingMethodMapping($label->getShippingMethod()); |
|
|
|
|
80
|
|
|
if($shippingMethodMapping) { |
81
|
|
|
$label->setProductCode($shippingMethodMapping->getProductCode()); |
82
|
|
|
if(!empty($shippingMethodMapping->getServiceCodes())) { |
83
|
|
|
$label->setServiceCodes(join(',', $shippingMethodMapping->getServiceCodes())); |
84
|
|
|
} |
85
|
|
|
} else { |
86
|
|
|
$label->markAsError('Shipping method `'.$label->getShippingMethod().'` is not mapped. Map it manually.'); |
87
|
|
|
$em->flush(); |
88
|
|
|
continue; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$label->setStatus(Label::STATUS_PENDING_CREATION); |
93
|
|
|
|
94
|
|
|
$em->flush(); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $country |
100
|
|
|
* @return null|string |
101
|
|
|
*/ |
102
|
|
|
protected function countryMapping(string $country) |
103
|
|
|
{ |
104
|
|
|
$em = $this->getEntityManager(); |
105
|
|
|
$countryMapping = $em->getRepository('LoevgaardPakkelabelsBundle:CountryMapping')->findOneBy([ |
106
|
|
|
'source' => $country |
107
|
|
|
]); |
108
|
|
|
|
109
|
|
|
if($countryMapping && $countryMapping->getCountryCode()) { |
110
|
|
|
return $countryMapping->getCountryCode(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return null; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $shippingMethod |
118
|
|
|
* @return ShippingMethodMapping|null |
119
|
|
|
*/ |
120
|
|
|
protected function shippingMethodMapping(string $shippingMethod) |
121
|
|
|
{ |
122
|
|
|
$em = $this->getEntityManager(); |
123
|
|
|
$shippingMethodMapping = $em->getRepository('LoevgaardPakkelabelsBundle:ShippingMethodMapping')->findOneBy([ |
124
|
|
|
'source' => $shippingMethod |
125
|
|
|
]); |
126
|
|
|
|
127
|
|
|
if($shippingMethodMapping && $shippingMethodMapping->getProductCode()) { |
128
|
|
|
return $shippingMethodMapping; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return null; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return EntityManager |
136
|
|
|
*/ |
137
|
|
|
protected function getEntityManager() : EntityManager |
138
|
|
|
{ |
139
|
|
|
/** @var EntityManager $em */ |
140
|
|
|
$em = $this->getContainer()->get('doctrine')->getManager(); |
141
|
|
|
|
142
|
|
|
return $em; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|