Skip to content

Commit 85dec05

Browse files
committed
OS2FORMS-358 p-number lookup
1 parent 7bd98b6 commit 85dec05

File tree

10 files changed

+270
-122
lines changed

10 files changed

+270
-122
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ See [OS2Web git name convention](https://github.com/OS2Web/docs#git-guideline)
3939
// CVR lookup
4040
/** @var \Drupal\os2web_datalookup\Plugin\DataLookupManager $pluginManager */
4141
$pluginManager = \Drupal::service('plugin.manager.os2web_datalookup');
42-
/** @var \Drupal\os2web_datalookup\Plugin\os2web\DataLookup\DataLookupInterfaceCvr $cvrPlugin */
42+
/** @var \Drupal\os2web_datalookup\Plugin\os2web\DataLookup\DataLookupInterfaceCompany $cvrPlugin */
4343
$cvrPlugin = $pluginManager->createDefaultInstanceByGroup('cvr_lookup');
4444
4545
if ($cvrPlugin->isReady()) {
46-
$cvrResult = $cvrPlugin->lookup(cvr);
46+
$cvrResult = $cvrPlugin->lookup($cvr);
4747
}
4848
4949
// CPR lookup.

os2web_datalookup.install

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,12 @@ function os2web_datalookup_update_9002() {
1919
$config->set("cvr_lookup.default_plugin", 'datafordeler_cvr');
2020
$config->save();
2121
}
22+
23+
/**
24+
* Setting "datafordeler_pnumber" as default P-Number lookup plugin.
25+
*/
26+
function os2web_datalookup_update_9003() {
27+
$config = \Drupal::service('config.factory')->getEditable(DataLookupPluginGroupSettingsForm::$configName);
28+
$config->set("pnumber_lookup.default_plugin", 'datafordeler_pnumber');
29+
$config->save();
30+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Drupal\os2web_datalookup\LookupResult;
44

5-
class CvrLookupResult {
5+
class CompanyLookupResult {
66

77
const CVR = 'cvr';
88
const NAME = 'name';
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Drupal\os2web_datalookup\Plugin\os2web\DataLookup;
4+
5+
interface DataLookupInterfaceCompany extends DataLookupInterface {
6+
7+
/**
8+
* Performs lookup for the provided param.
9+
*
10+
* @param string $param
11+
* The param to query for.
12+
*
13+
* @return \Drupal\os2web_datalookup\LookupResult\CompanyLookupResult
14+
* The company lookup Result.
15+
*/
16+
public function lookup($param);
17+
18+
}

src/Plugin/os2web/DataLookup/DataLookupInterfaceCvr.php

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
namespace Drupal\os2web_datalookup\Plugin\os2web\DataLookup;
4+
5+
use Drupal\Core\Form\FormStateInterface;
6+
use GuzzleHttp\Client;
7+
8+
/**
9+
* Defines base plugin class for Datafordeler lookup plugins.
10+
*/
11+
abstract class DatafordelerBase extends DataLookupBase {
12+
13+
/**
14+
* Plugin readiness flag.
15+
*
16+
* @var bool
17+
*/
18+
protected $httpClient;
19+
20+
/**
21+
* {@inheritdoc}
22+
*/
23+
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
24+
parent::__construct($configuration, $plugin_id, $plugin_definition);
25+
$this->init();
26+
}
27+
28+
/**
29+
* Plugin init method.
30+
*/
31+
private function init() {
32+
$this->isReady = FALSE;
33+
34+
$configuration = $this->getConfiguration();
35+
36+
if ($webserviceUrl = $configuration['webserviceurl_live']) {
37+
$options = [
38+
'base_uri' => $webserviceUrl,
39+
'headers' => [
40+
'accept' => 'application/json',
41+
],
42+
];
43+
if ($certPath = $configuration['cert_path_live']) {
44+
$options['cert'] = $certPath;
45+
$this->httpClient = new Client($options);
46+
$this->isReady = TRUE;
47+
}
48+
}
49+
}
50+
51+
/**
52+
* {@inheritdoc}
53+
*/
54+
public function getStatus() {
55+
if ($this->httpClient) {
56+
return $this->t('Plugin is ready to work');
57+
}
58+
else {
59+
return $this->t('Configuration is not completed');
60+
}
61+
}
62+
63+
/**
64+
* {@inheritdoc}
65+
*/
66+
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
67+
$form['webserviceurl_live'] = [
68+
'#type' => 'textfield',
69+
'#title' => $this->t('Webservice URL (LIVE)'),
70+
'#description' => $this->t('Live URL against which to make the request, e.g. https://s5-certservices.datafordeler.dk/CVR/HentCVRData/1/REST/'),
71+
'#default_value' => $this->configuration['webserviceurl_live'],
72+
];
73+
74+
$form['cert_path_live'] = [
75+
'#type' => 'textfield',
76+
'#title' => $this->t('Certificate (LIVE)'),
77+
'#description' => $this->t('Path to the certificate'),
78+
'#default_value' => $this->configuration['cert_path_live'],
79+
];
80+
81+
$form['cert_passphrase_live'] = [
82+
'#type' => 'password',
83+
'#title' => $this->t('Certificate passphrase (LIVE)'),
84+
'#description' => $this->t('leave empty if not used'),
85+
'#default_value' => $this->configuration['cert_passphrase_live'],
86+
];
87+
88+
return $form;
89+
}
90+
91+
/**
92+
* {@inheritdoc}
93+
*/
94+
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
95+
if ($form_state->getValue('cert_passphrase_live') == '') {
96+
$form_state->unsetValue('cert_passphrase_live');
97+
}
98+
99+
$keys = array_keys($this->defaultConfiguration());
100+
$configuration = $this->getConfiguration();
101+
foreach ($keys as $key) {
102+
$configuration[$key] = $form_state->getValue($key);
103+
}
104+
$this->setConfiguration($configuration);
105+
}
106+
107+
}

src/Plugin/os2web/DataLookup/DatafordelerCVR.php

Lines changed: 9 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
use Drupal\Core\Form\FormStateInterface;
66
use Drupal\Core\Messenger\MessengerInterface;
77
use Drupal\Core\Render\Markup;
8-
use Drupal\os2web_datalookup\LookupResult\CvrLookupResult;
9-
use GuzzleHttp\Client;
8+
use Drupal\os2web_datalookup\LookupResult\CompanyLookupResult;
109
use GuzzleHttp\Exception\ClientException;
1110

1211
/**
@@ -18,57 +17,7 @@
1817
* group = "cvr_lookup"
1918
* )
2019
*/
21-
class DatafordelerCVR extends DataLookupBase implements DataLookupInterfaceCvr {
22-
23-
/**
24-
* Plugin readiness flag.
25-
*
26-
* @var bool
27-
*/
28-
protected $httpClient;
29-
30-
/**
31-
* {@inheritdoc}
32-
*/
33-
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
34-
parent::__construct($configuration, $plugin_id, $plugin_definition);
35-
$this->init();
36-
}
37-
38-
/**
39-
* Plugin init method.
40-
*/
41-
private function init() {
42-
$this->isReady = FALSE;
43-
44-
$configuration = $this->getConfiguration();
45-
46-
if ($webserviceUrl = $configuration['webserviceurl_live']) {
47-
$options = [
48-
'base_uri' => $webserviceUrl,
49-
'headers' => [
50-
'accept' => 'application/json',
51-
],
52-
];
53-
if ($certPath = $configuration['cert_path_live']) {
54-
$options['cert'] = $certPath;
55-
$this->httpClient = new Client($options);
56-
$this->isReady = TRUE;
57-
}
58-
}
59-
}
60-
61-
/**
62-
* {@inheritdoc}
63-
*/
64-
public function getStatus() {
65-
if ($this->httpClient) {
66-
return $this->t('Plugin is ready to work');
67-
}
68-
else {
69-
return $this->t('Configuration is not completed');
70-
}
71-
}
20+
class DatafordelerCVR extends DatafordelerBase implements DataLookupInterfaceCompany {
7221

7322
/**
7423
* {@inheritdoc}
@@ -85,26 +34,7 @@ public function defaultConfiguration() {
8534
* {@inheritdoc}
8635
*/
8736
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
88-
$form['webserviceurl_live'] = [
89-
'#type' => 'textfield',
90-
'#title' => $this->t('Webservice URL (LIVE)'),
91-
'#description' => $this->t('Live URL against which to make the request, e.g. https://s5-certservices.datafordeler.dk/CVR/HentCVRData/1/REST/'),
92-
'#default_value' => $this->configuration['webserviceurl_live'],
93-
];
94-
95-
$form['cert_path_live'] = [
96-
'#type' => 'textfield',
97-
'#title' => $this->t('Certificate (LIVE)'),
98-
'#description' => $this->t('Path to the certificate'),
99-
'#default_value' => $this->configuration['cert_path_live'],
100-
];
101-
102-
$form['cert_passphrase_live'] = [
103-
'#type' => 'password',
104-
'#title' => $this->t('Certificate passphrase (LIVE)'),
105-
'#description' => $this->t('leave empty if not used'),
106-
'#default_value' => $this->configuration['cert_passphrase_live'],
107-
];
37+
$form = parent::buildConfigurationForm($form, $form_state);
10838

10939
$form['test_cvr'] = [
11040
'#type' => 'textfield',
@@ -118,30 +48,21 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
11848
* {@inheritdoc}
11949
*/
12050
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
121-
if ($form_state->getValue('cert_passphrase_live') == '') {
122-
$form_state->unsetValue('cert_passphrase_live');
123-
}
124-
125-
$keys = array_keys($this->defaultConfiguration());
126-
$configuration = $this->getConfiguration();
127-
foreach ($keys as $key) {
128-
$configuration[$key] = $form_state->getValue($key);
129-
}
130-
$this->setConfiguration($configuration);
51+
parent::submitConfigurationForm($form, $form_state);
13152

13253
if (!empty($form_state->getValue('test_cvr'))) {
133-
$cvrResult = $this->lookup($form_state->getValue('test_cvr'));
134-
$response = (array) $cvrResult;
54+
$lookupResult = $this->lookup($form_state->getValue('test_cvr'));
55+
$response = (array) $lookupResult;
13556

13657
\Drupal::messenger()->addMessage(
13758
Markup::create('<pre>' . print_r($response, 1) . '</pre>'),
138-
$cvrResult->isSuccessful() ? MessengerInterface::TYPE_STATUS : MessengerInterface::TYPE_WARNING
59+
$lookupResult->isSuccessful() ? MessengerInterface::TYPE_STATUS : MessengerInterface::TYPE_WARNING
13960
);
14061
}
14162
}
14263

14364
/**
144-
* @inheritDoc
65+
* {@inheritdoc}
14566
*/
14667
public function lookup($cvr) {
14768
try {
@@ -152,7 +73,7 @@ public function lookup($cvr) {
15273
$result = $e->getMessage();
15374
}
15475

155-
$cvrResult = new CvrLookupResult();
76+
$cvrResult = new CompanyLookupResult();
15677
if ($result && isset($result->virksomhed) && !empty($result->virksomhed)) {
15778
$cvrResult->setSuccessful();
15879
$cvrResult->setCvr($cvr);

0 commit comments

Comments
 (0)