-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathServiceplatformenCVR.php
More file actions
161 lines (146 loc) · 5.2 KB
/
Copy pathServiceplatformenCVR.php
File metadata and controls
161 lines (146 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
namespace Drupal\os2web_datalookup\Plugin\os2web\DataLookup;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Render\Markup;
use Drupal\os2web_datalookup\LookupResult\CompanyLookupResult;
/**
* Defines a plugin for ServiceplatformenCVR.
*
* @DataLookup(
* id = "serviceplatformen_cvr",
* label = @Translation("Serviceplatformen CVR"),
* group = "cvr_lookup"
* )
*/
class ServiceplatformenCVR extends ServiceplatformenBase implements DataLookupCompanyInterface {
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state): array {
$form = parent::buildConfigurationForm($form, $form_state);
$form['test_cvr'] = [
'#type' => 'textfield',
'#title' => $this->t('Test CVR nr.'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state): void {
parent::submitConfigurationForm($form, $form_state);
if (!empty($form_state->getValue('test_cvr'))) {
$response = $this->getInfo($form_state->getValue('test_cvr'));
\Drupal::messenger()->addMessage(
Markup::create('<pre>' . print_r($response, 1) . '</pre>'),
$response['status'] ? MessengerInterface::TYPE_STATUS : MessengerInterface::TYPE_WARNING
);
}
}
/**
* Implementation of getLegalUnit call.
*
* @param string $cvr
* Requested CVR.
*
* @return array
* [status] => TRUE/FALSE
* [cvr] => CVR code
* [company_name] => Name of the organization
* [company_street] => Street name
* [company_house_nr] => House nr
* [company_floor] => Floor nr
* [company_zipcode] => ZIP code
* [company_city] => City
*/
public function getLegalUnit(string $cvr): array {
$request = $this->prepareRequest();
$request['GetLegalUnitRequest'] = [
'level' => 1,
'UserId' => NULL,
'Password' => NULL,
'LegalUnitIdentifier' => $cvr,
];
return $this->query('getLegalUnit', $request);
}
/**
* Translates the fetch CVR information to a nice looking array.
*
* @param string $cvr
* Requested CVR.
*
* @return array
* [status] => TRUE/FALSE
* [cvr] => CVR code,
* [company_name] => Name of the organization,
* [company_street] => Street name,
* [company_house_nr] => House nr,
* [company_apartment_nr] => House nr,
* [company_floor] => Floor nr,
* [company_zipcode] => ZIP code
* [company_city] => City
* [company_municipalitycode] => Municipality code,
*/
public function getInfo(string $cvr): array {
$result = $this->getLegalUnit($cvr);
if ($result['status']) {
$legalUnit = (array) $result['GetLegalUnitResponse']->LegalUnit;
return [
'status' => TRUE,
'cvr' => $legalUnit['LegalUnitIdentifier'],
'company_name' => $legalUnit['LegalUnitName']->name,
'company_street' => $legalUnit['AddressOfficial']->AddressPostalExtended->StreetName,
'company_house_nr' => $legalUnit['AddressOfficial']->AddressPostalExtended->StreetBuildingIdentifier,
'company_apartment_nr' => $legalUnit['AddressOfficial']->AddressPostalExtended->SuiteIdentifier ?? '',
'company_floor' => $legalUnit['AddressOfficial']->AddressPostalExtended->FloorIdentifier ?? '',
'company_zipcode' => $legalUnit['AddressOfficial']->AddressPostalExtended->PostCodeIdentifier,
'company_city' => $legalUnit['AddressOfficial']->AddressPostalExtended->DistrictName,
'company_municipalitycode' => $legalUnit['AddressOfficial']->AddressAccess->MunicipalityCode,
];
}
else {
return $result;
}
}
/**
* {@inheritdoc}
*/
public function lookup(string $param): CompanyLookupResult {
$result = $this->getInfo($param);
$cvrResult = new CompanyLookupResult();
if ($result['status']) {
$cvrResult->setSuccessful();
$cvrResult->setCvr($result['cvr']);
$cvrResult->setName($result['company_name']);
$cvrResult->setStreet($result['company_street']);
$cvrResult->setHouseNr($result['company_house_nr']);
$cvrResult->setApartmentNr($result['company_apartment_nr']);
$cvrResult->setFloor($result['company_floor']);
$cvrResult->setPostalCode($result['company_zipcode']);
$cvrResult->setMunicipalityCode($result['company_municipalitycode']);
$city = $result['company_zipcode'] . ' ' . $result['company_city'];
$cvrResult->setCity($city);
// Composing full address in one line.
$address = $cvrResult->getStreet();
if ($cvrResult->getHouseNr()) {
$address .= ' ' . $cvrResult->getHouseNr();
}
if ($cvrResult->getFloor()) {
$address .= ' ' . $cvrResult->getFloor();
}
if ($cvrResult->getApartmentNr()) {
$address .= ' ' . $cvrResult->getApartmentNr();
}
if ($cvrResult->getPostalCode() && $cvrResult->getCity()) {
$address .= ', ' . $cvrResult->getPostalCode() . ' ' . $cvrResult->getCity();
}
$cvrResult->setAddress($address ?? '');
}
else {
$cvrResult->setSuccessful(FALSE);
$cvrResult->setErrorMessage($result['error']);
}
return $cvrResult;
}
}