66use Drupal \Core \Messenger \MessengerInterface ;
77use Drupal \Core \Render \Markup ;
88use Drupal \os2web_datalookup \LookupResult \CompanyLookupResult ;
9- use GuzzleHttp \Exception \ClientException ;
9+ use GuzzleHttp \Client ;
10+ use GuzzleHttp \Exception \GuzzleException ;
11+ use Psr \Http \Message \ResponseInterface ;
1012
1113/**
1214 * Defines a plugin for DatafordelerCVR.
1719 * group = "cvr_lookup"
1820 * )
1921 */
20- class DatafordelerCVR extends DatafordelerBase implements DataLookupCompanyInterface {
22+ class DatafordelerCVR extends DataLookupBase implements DataLookupCompanyInterface {
23+
24+ /**
25+ * Http client.
26+ *
27+ * @var \GuzzleHttp\Client
28+ */
29+ protected Client $ httpClient ;
2130
2231 /**
2332 * {@inheritdoc}
2433 */
2534 public function defaultConfiguration (): array {
2635 return [
27- 'webserviceurl_live ' => 'https://s5-certservices.datafordeler.dk/CVR/HentCVRData/1/REST/ ' ,
36+ 'webserviceurl_live ' => 'https://graphql.datafordeler.dk/flexibleCurrent/v1 ' ,
37+ 'api_key ' => '' ,
2838 ] + parent ::defaultConfiguration ();
2939 }
3040
3141 /**
3242 * {@inheritdoc}
3343 */
3444 public function buildConfigurationForm (array $ form , FormStateInterface $ form_state ): array {
35- $ form = parent ::buildConfigurationForm ($ form , $ form_state );
45+ $ form ['webserviceurl_live ' ] = [
46+ '#type ' => 'textfield ' ,
47+ '#title ' => $ this ->t ('Webservice URL (LIVE) ' ),
48+ '#description ' => $ this ->t ('Live URL against which to make the request, e.g. https://graphql.datafordeler.dk/flexibleCurrent/v1 ' ),
49+ '#default_value ' => $ this ->configuration ['webserviceurl_live ' ],
50+ ];
51+
52+ $ form ['api_key ' ] = [
53+ '#type ' => 'textfield ' ,
54+ '#title ' => $ this ->t ('API Key ' ),
55+ '#default_value ' => $ this ->configuration ['api_key ' ],
56+ ];
3657
3758 $ form ['test_cvr ' ] = [
3859 '#type ' => 'textfield ' ,
@@ -46,7 +67,12 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
4667 * {@inheritdoc}
4768 */
4869 public function submitConfigurationForm (array &$ form , FormStateInterface $ form_state ): void {
49- parent ::submitConfigurationForm ($ form , $ form_state );
70+ $ keys = array_keys ($ this ->defaultConfiguration ());
71+ $ configuration = $ this ->getConfiguration ();
72+ foreach ($ keys as $ key ) {
73+ $ configuration [$ key ] = $ form_state ->getValue ($ key );
74+ }
75+ $ this ->setConfiguration ($ configuration );
5076
5177 if (!empty ($ form_state ->getValue ('test_cvr ' ))) {
5278 $ lookupResult = $ this ->lookup ($ form_state ->getValue ('test_cvr ' ));
@@ -64,28 +90,34 @@ public function submitConfigurationForm(array &$form, FormStateInterface $form_s
6490 */
6591 public function lookup (string $ param ): CompanyLookupResult {
6692 try {
93+ if (!preg_match ('/^\d{8}$/ ' , $ param )) {
94+ throw new \InvalidArgumentException ('CVR must be exactly 8 digits. ' );
95+ }
96+
6797 $ msg = sprintf ('Hent virksomhed med CVRNummer: %s ' , $ param );
6898 $ this ->auditLogger ->info ('DataLookup ' , $ msg );
69- $ response = $ this ->getResponse ( ' hentVirksomhedMedCVRNummer ' , [ ' query ' => [ ' pCVRNummer ' => $ param]] );
99+ $ response = $ this ->executeQuery ( $ param );
70100 $ result = json_decode ((string ) $ response ->getBody ());
71101 }
72- catch (ClientException $ e ) {
102+ catch (GuzzleException | \ InvalidArgumentException $ e ) {
73103 $ msg = sprintf ('Hent virksomhed med CVRNummer (%s): %s ' , $ param , $ e ->getMessage ());
74104 $ this ->auditLogger ->error ('DataLookup ' , $ msg );
75105 $ result = $ e ->getMessage ();
76106 }
77107
78108 $ cvrResult = new CompanyLookupResult ();
79- if ($ result && isset ($ result ->virksomhed ) && !empty ((array ) $ result ->virksomhed )) {
109+ if ($ result && isset ($ result ->data ->CVR_Virksomhed ->nodes ) && !empty ($ result ->data ->CVR_Virksomhed ->nodes )) {
110+ $ companyGraph = $ result ->data ->CVR_Virksomhed ->nodes [0 ]->id_CVR_CVREnhed_id_ref ->nodes [0 ];
111+
80112 $ cvrResult ->setSuccessful ();
81113 $ cvrResult ->setCvr ($ param );
82114
83- if ($ result -> virksomhedsnavn ) {
84- $ cvrResult ->setName ($ result -> virksomhedsnavn ->vaerdi );
115+ if ($ companyGraph -> id_CVR_Navn_CVREnhedsId_ref ) {
116+ $ cvrResult ->setName ($ companyGraph -> id_CVR_Navn_CVREnhedsId_ref ->vaerdi );
85117 }
86118
87- if ($ result -> beliggenhedsadresse ) {
88- $ address = $ result -> beliggenhedsadresse ;
119+ if ($ companyGraph -> id_CVR_Adressering_CVREnhedsId_ref ) {
120+ $ address = $ companyGraph -> id_CVR_Adressering_CVREnhedsId_ref -> nodes [ 0 ] ;
89121
90122 $ cvrResult ->setStreet ($ address ->CVRAdresse_vejnavn ?? '' );
91123 $ cvrResult ->setHouseNr ($ address ->CVRAdresse_husnummerFra ?? '' );
@@ -124,4 +156,64 @@ public function lookup(string $param): CompanyLookupResult {
124156 return $ cvrResult ;
125157 }
126158
159+ /**
160+ * Executes the GraphQL lookup request for a specific CVR number.
161+ *
162+ * Builds the GraphQL payload and sends it to the configured Datafordeler
163+ * endpoint using the shared HTTP request flow from the parent class.
164+ *
165+ * @param string $cvr
166+ * The CVR number to look up.
167+ *
168+ * @return \Psr\Http\Message\ResponseInterface
169+ * The raw HTTP response from the GraphQL endpoint.
170+ *
171+ * @throws \GuzzleHttp\Exception\GuzzleException
172+ */
173+ private function executeQuery ($ cvr ): ResponseInterface {
174+ $ this ->httpClient = new Client ();
175+
176+ // Setting date to TODAY 00:00:00, so that we are always getting up-to-date
177+ // information.
178+ $ virkningstid = (new \DateTimeImmutable ('today ' , new \DateTimeZone ('UTC ' )))
179+ ->format ('Y-m-d\T00:00:00\Z ' );
180+
181+ $ query = <<<GRAPHQL
182+ { CVR_Virksomhed(first: 1, virkningstid: " {$ virkningstid }", where: { CVRNummer: { eq: {$ cvr } } }) {
183+ nodes {
184+ CVRNummer
185+ id_CVR_CVREnhed_id_ref(first: 1) {
186+ nodes {
187+ id_CVR_Navn_CVREnhedsId_ref { vaerdi }
188+ id_CVR_Adressering_CVREnhedsId_ref(first: 1, where: { AdresseringAnvendelse: { in: ["beliggenhedsadresse", "postadresse"] } }) {
189+ nodes {
190+ AdresseringAnvendelse
191+ CVRAdresse_vejnavn
192+ CVRAdresse_husnummerFra
193+ CVRAdresse_etagebetegnelse
194+ CVRAdresse_doerbetegnelse
195+ CVRAdresse_postnummer
196+ CVRAdresse_postdistrikt
197+ CVRAdresse_kommunekode
198+ }
199+ }
200+ }
201+ }
202+ }
203+ }
204+ }
205+ GRAPHQL ;
206+
207+ $ webserviceUrl = $ this ->configuration ['webserviceurl_live ' ];
208+
209+ return $ this ->httpClient ->post ($ webserviceUrl , [
210+ 'query ' => [
211+ 'apiKey ' => $ this ->configuration ['api_key ' ],
212+ ],
213+ 'json ' => [
214+ 'query ' => $ query ,
215+ ],
216+ ]);
217+ }
218+
127219}
0 commit comments