Skip to content

Commit

Permalink
ITKDev: Code style fixes in plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
cableman committed Apr 24, 2024
1 parent f0a3948 commit 1b72571
Show file tree
Hide file tree
Showing 14 changed files with 128 additions and 107 deletions.
52 changes: 31 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,41 +1,48 @@
# OS2Web Data lookup [![Build Status](https://travis-ci.org/OS2web/os2web_datalookup.svg?branch=8.x)](https://travis-ci.org/OS2web/os2web_datalookup)

## Install

OS2Web Data lookup provides integration with Danish data lookup services such as Service platformen or Datafordeler.
Module is available to download via composer.
```
OS2Web Data lookup provides integration with Danish data lookup services such as
Service platformen or Datafordeler. Module is available to download via
composer.

```shell
composer require os2web/os2web_datalookup
drush en os2web_datalookup
```

## Update
Updating process for OS2Web Data lookup module is similar to usual Drupal 8 module.
Use Composer's built-in command for listing packages that have updates available:
Updating process for OS2Web Data lookup module is similar to the usual Drupal 8
module. Use Composer's built-in command for listing packages that have updates
available:

```
```shell
composer outdated os2web/os2web_datalookup
```

## Automated testing and code quality

See [OS2Web testing and CI information](https://github.com/OS2Web/docs#testing-and-ci)

## Contribution

Project is opened for new features and os course bugfixes.
If you have any suggestion or you found a bug in project, you are very welcome
to create an issue in github repository issue tracker.
For issue description there is expected that you will provide clear and
sufficient information about your feature request or bug report.
If you have any suggestion, or you found a bug in project, you are very welcome
to create an issue in GitHub repository issue tracker. For issue description,
there is expected that you will provide clear and sufficient information about
your feature request or bug report.

### Code review policy

See [OS2Web code review policy](https://github.com/OS2Web/docs#code-review)

### Git name convention

See [OS2Web git name convention](https://github.com/OS2Web/docs#git-guideline)

### Using services in other modules

```
```php
// CVR lookup
/** @var \Drupal\os2web_datalookup\Plugin\DataLookupManager $pluginManager */
$pluginManager = \Drupal::service('plugin.manager.os2web_datalookup');
Expand Down Expand Up @@ -68,22 +75,25 @@ if ($cprPlugin->isReady()) {

### Datafordeler integration (https://datafordeler.dk)

In scope of os2forms project already implemented light integration
with Danmarks Adresseregister (DAR) via fetching data for form elements
autocomplete. See [os2forms_dawa submodule](https://github.com/OS2Forms/os2forms)
In the scope of os2forms project already implemented light integration with
Danmarks Adresseregister (DAR) via fetching data for form elements autocomplete.

See [os2forms_dawa submodule](https://github.com/OS2Forms/os2forms)

As soon as it is clear how the integration is going to be used, then
os2forms_dawa will be refactored to OS2Web Data lookup plugin plugin.
os2forms_dawa will be refactored to OS2Web Data lookup plugin.

## Important notes

### Serviceplatformen plugins
Settings for CPR and CVR serviceplantormen plugins are storing as configuration
in db and will(could) be exported as `yml` file via Drupal configuration
management system. And afterwards could be tracked by `git`.

If case you have public access to your git repository all setting from plugins
Settings for CPR and CVR serviceplatformen plugins are storing as configuration
in db and will(could) be exported as `yml` file via Drupal's configuration
management system. And afterward could be tracked by `git`.

If case you have public access to your git repository, all settings from plugins
will be exposed for third persons.

To avoid/prevent this behavior we recommend use `Config ignore` module, where
you can add all settings you do not want to export/import via configuration
To avoid/prevent this behavior, we recommend use `Config ignore` module, where
you can add all settings you do not want to export/import via the configuration
management system.
19 changes: 10 additions & 9 deletions src/Plugin/DataLookupManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ class DataLookupManager extends DefaultPluginManager {
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
protected ConfigFactoryInterface $configFactory;

/**
* Constructs an DataLookupManager object.
* Constructs a DataLookupManager object.
*
* @param \Traversable $namespaces
* An object that implements \Traversable which contains the root paths
Expand Down Expand Up @@ -54,7 +54,7 @@ public function __construct(\Traversable $namespaces, CacheBackendInterface $cac
/**
* {@inheritdoc}
*/
public function createInstance($plugin_id, array $configuration = []) {
public function createInstance($plugin_id, array $configuration = []): object {
// Hard switch to another plugin fallback - see
// https://os2web.atlassian.net/browse/OS2FORMS-359.
if ($plugin_id == 'serviceplatformen_cpr') {
Expand All @@ -81,7 +81,7 @@ public function createInstance($plugin_id, array $configuration = []) {
* @throws \Drupal\Component\Plugin\Exception\PluginException
* If the instance cannot be created, such as if the ID is invalid.
*/
public function createDefaultInstanceByGroup($group_id) {
public function createDefaultInstanceByGroup(string $group_id): object {
$plugin_id = $this->getGroupDefaultPlugin($group_id);
return $this->createInstance($plugin_id);
}
Expand All @@ -92,11 +92,11 @@ public function createDefaultInstanceByGroup($group_id) {
* @param $group_id
* Group id.
*
* @return mixed[]
* @return array
* An array of plugin definitions (empty array if no definitions were
* found). Keys are plugin IDs.
*/
public function getDefinitionsByGroup($group_id) {
public function getDefinitionsByGroup($group_id): array {
$definitions = [];
foreach ($this->getDefinitions() as $id => $plugin_definition) {
if ($plugin_definition['group'] == $group_id) {
Expand All @@ -113,11 +113,11 @@ public function getDefinitionsByGroup($group_id) {
* @return array
* Array of groups, keyed by group_id.
*/
public function getDatalookupGroups() {
public function getDatalookupGroups(): array {
$definitions = $this->getDefinitions();
$groups = [];
foreach ($definitions as $definition) {
if (isset($definition['group']) && !empty($definition['group'])) {
if (!empty($definition['group'])) {
$groups[$definition['group']] = $definition['group'];
}
}
Expand All @@ -134,8 +134,9 @@ public function getDatalookupGroups() {
* @return string|null
* Plugin ID or NULL is not set.
*/
public function getGroupDefaultPlugin($group_id) {
public function getGroupDefaultPlugin($group_id): ?string {
$config = \Drupal::config(DataLookupPluginGroupSettingsForm::$configName);

return $config->get("$group_id.default_plugin");
}

Expand Down
15 changes: 8 additions & 7 deletions src/Plugin/os2web/DataLookup/DataLookupBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ abstract class DataLookupBase extends PluginBase implements DataLookupInterface
*
* @var bool
*/
protected $isReady = TRUE;
protected bool $isReady = TRUE;

/**
* {@inheritdoc}
Expand All @@ -42,22 +42,23 @@ public function label() {
/**
* {@inheritdoc}
*/
public function getConfiguration() {
public function getConfiguration(): array {
return $this->configuration;
}

/**
* {@inheritdoc}
*/
public function setConfiguration(array $configuration) {
public function setConfiguration(array $configuration): static {
$this->configuration = $configuration + $this->defaultConfiguration();

return $this;
}

/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
public function defaultConfiguration(): array {
return [];
}

Expand All @@ -71,21 +72,21 @@ public function validateConfigurationForm(array &$form, FormStateInterface $form
/**
* {@inheritdoc}
*/
public function getStatus() {
public function getStatus(): string {
return 'N/A';
}

/**
* {@inheritdoc}
*/
public function getGroup() {
public function getGroup(): string {
return $this->pluginDefinition['group'];
}

/**
* {@inheritdoc}
*/
public function isReady() {
public function isReady(): bool {
return $this->isReady;
}

Expand Down
9 changes: 5 additions & 4 deletions src/Plugin/os2web/DataLookup/DataLookupInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Drupal\Component\Plugin\ConfigurableInterface;
use Drupal\Component\Plugin\PluginInspectionInterface;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
* DataLookupInterface plugin interface for providing some metadata inspection.
Expand All @@ -19,16 +20,16 @@ interface DataLookupInterface extends PluginInspectionInterface, PluginFormInter
/**
* Get plugin status.
*/
public function getStatus();
public function getStatus(): string;

/**
* Get plugin group.
* Get a plugin group.
*/
public function getGroup();
public function getGroup(): string;

/**
* Get plugin readiness.
*/
public function isReady();
public function isReady(): bool;

}
4 changes: 3 additions & 1 deletion src/Plugin/os2web/DataLookup/DataLookupInterfaceCompany.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Drupal\os2web_datalookup\Plugin\os2web\DataLookup;

use Drupal\os2web_datalookup\LookupResult\CompanyLookupResult;

interface DataLookupInterfaceCompany extends DataLookupInterface {

/**
Expand All @@ -13,6 +15,6 @@ interface DataLookupInterfaceCompany extends DataLookupInterface {
* @return \Drupal\os2web_datalookup\LookupResult\CompanyLookupResult
* The company lookup Result.
*/
public function lookup($param);
public function lookup(string $param): CompanyLookupResult;

}
4 changes: 3 additions & 1 deletion src/Plugin/os2web/DataLookup/DataLookupInterfaceCpr.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Drupal\os2web_datalookup\Plugin\os2web\DataLookup;

use Drupal\os2web_datalookup\LookupResult\CprLookupResult;

interface DataLookupInterfaceCpr extends DataLookupInterface {

/**
Expand All @@ -13,6 +15,6 @@ interface DataLookupInterfaceCpr extends DataLookupInterface {
* @return \Drupal\os2web_datalookup\LookupResult\CprLookupResult
* The CPR lookup Result.
*/
public function lookup($cpr);
public function lookup(string $cpr): CprLookupResult;

}
21 changes: 11 additions & 10 deletions src/Plugin/os2web/DataLookup/DatafordelerBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Drupal\os2web_datalookup\Plugin\os2web\DataLookup;

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use GuzzleHttp\Client;

/**
Expand All @@ -11,11 +12,11 @@
abstract class DatafordelerBase extends DataLookupBase {

/**
* Plugin readiness flag.
* Http client.
*
* @var bool
* @var Client
*/
protected $httpClient;
protected Client $httpClient;

/**
* {@inheritdoc}
Expand All @@ -28,7 +29,7 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition
/**
* Plugin init method.
*/
private function init() {
private function init(): void {
$this->isReady = FALSE;

$configuration = $this->getConfiguration();
Expand All @@ -51,19 +52,19 @@ private function init() {
/**
* {@inheritdoc}
*/
public function getStatus() {
if ($this->httpClient) {
return $this->t('Plugin is ready to work');
public function getStatus(): string {
if (isset($this->httpClient)) {
return $this->t('Plugin is ready to work')->render();
}
else {
return $this->t('Configuration is not completed');
return $this->t('Configuration is not completed')->render();
}
}

/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
public function buildConfigurationForm(array $form, FormStateInterface $form_state): array {
$form['webserviceurl_live'] = [
'#type' => 'textfield',
'#title' => $this->t('Webservice URL (LIVE)'),
Expand Down Expand Up @@ -91,7 +92,7 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
public function submitConfigurationForm(array &$form, FormStateInterface $form_state): void {
if ($form_state->getValue('cert_passphrase_live') == '') {
$form_state->unsetValue('cert_passphrase_live');
}
Expand Down
Loading

0 comments on commit 1b72571

Please sign in to comment.