55use Drupal \Core \Form \FormStateInterface ;
66use Drupal \Core \Messenger \MessengerInterface ;
77use Drupal \Core \Render \Markup ;
8- use Drupal \os2web_datalookup \LookupResult \CvrLookupResult ;
9- use GuzzleHttp \Client ;
8+ use Drupal \os2web_datalookup \LookupResult \CompanyLookupResult ;
109use GuzzleHttp \Exception \ClientException ;
1110
1211/**
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