Skip to content

Commit

Permalink
Added lookup commands
Browse files Browse the repository at this point in the history
  • Loading branch information
rimi-itk committed Nov 1, 2023
1 parent 81fe6af commit 109fd3e
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Nedenfor ses dato for release og beskrivelse af opgaver som er implementeret.
* Opdaterede til [OS2Forms organisation
1.3.3](https://github.com/itk-dev/os2forms_organisation/releases/tag/1.3.3)
* Tilføjede beskrivelsestekst til email-handler.
* Tilføjede praktiske CPR- og CVR-opslagskommandoer.

## [2.6.5] 2023-10-30

Expand Down
6 changes: 6 additions & 0 deletions web/modules/custom/os2forms_selvbetjening/drush.services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
services:
Drupal\os2forms_selvbetjening\Commands\LookupCommands:
arguments:
- '@plugin.manager.os2web_datalookup'
tags:
- { name: drush.command }
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace Drupal\os2forms_selvbetjening\Commands;

use Drupal\os2web_datalookup\Plugin\DataLookupManager;
use Drupal\os2web_datalookup\Plugin\os2web\DataLookup\DatafordelerCVR;
use Drupal\os2web_datalookup\Plugin\os2web\DataLookup\DataLookupInterfaceCpr;
use Drush\Commands\DrushCommands;
use Symfony\Component\Yaml\Yaml;

/**
* Lookup commands.
*/
class LookupCommands extends DrushCommands {

/**
* Constructor.
*/
public function __construct(
private readonly DataLookupManager $dataLookupManager
) {
}

/**
* Look up CPR.
*
* @param string $cpr
* The cpr.
* @param array $options
* The command options.
*
* @command os2forms-selvbetjening:look-up:cpr
* @usage os2forms-selvbetjening:look-up:cpr --help
*/
public function lookUpCpr(string $cpr, array $options = [
'dump-configuration' => FALSE,
]) {
try {
$instance = $this->dataLookupManager->createDefaultInstanceByGroup('cpr_lookup');
assert($instance instanceof DataLookupInterfaceCpr);

if ($options['dump-configuration']) {
$this->output()->writeln([
Yaml::dump($instance->getConfiguration()),
]);
}
$result = $instance->lookup($cpr);

if (!$result->isSuccessful()) {
$this->output()->writeln($result->getErrorMessage());
}
else {
$this->output()->write($result->getName());
}
}
catch (\Exception $exception) {
$this->output()->writeln($exception->getMessage());
}
}

/**
* Look up CVR.
*
* @param string $cvr
* The cvr.
* @param array $options
* The command options.
*
* @command os2forms-selvbetjening:look-up:cvr
* @usage os2forms-selvbetjening:look-up:cvr --help
*/
public function lookUpCvr(string $cvr, array $options = [
'dump-configuration' => FALSE,
]) {
try {
$instance = $this->dataLookupManager->createDefaultInstanceByGroup('cvr_lookup');
assert($instance instanceof DatafordelerCVR);

if ($options['dump-configuration']) {
$this->output()->writeln([
Yaml::dump($instance->getConfiguration()),
]);
}
$result = $instance->lookup($cvr);

if (!$result->isSuccessful()) {
$this->output()->writeln($result->getErrorMessage());
}
else {
$this->output()->write($result->getName());
}
}
catch (\Exception $exception) {
$this->output()->writeln($exception->getMessage());
}
}

}

0 comments on commit 109fd3e

Please sign in to comment.