Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure to always use same environment to fetch global announcements and news #613

Merged
merged 4 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions modules/helfi_global_announcement/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,3 @@ Global announcements can be fetched to any instance and rendered using blocks.
## How to set up locally

Local setup requires Etusivu-instance to be up and running with some relevant data created to it.

Add following line to local.settings.php in order to connect to local etusivu-instance
`$config['helfi_global_announcement.settings']['source_environment'] = 'local'`
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,8 @@ public static function create(

/** @var \Drupal\helfi_api_base\Environment\EnvironmentResolver $environmentResolver */
$environmentResolver = $container->get('helfi_api_base.environment_resolver');

// Attempt to read environment from config override and fallback to
// currently active environment.
$environment = $container->get('config.factory')
->get('helfi_global_announcement.settings')
->get('source_environment');

if (!$environment) {
try {
$environment = $environmentResolver->getActiveEnvironment()
->getEnvironmentName();
}
catch (\InvalidArgumentException) {
$environment = 'prod';
}
}
$instance->environment = $environmentResolver
->getEnvironment(Project::ETUSIVU, $environment);
->getEnvironment(Project::ETUSIVU, $environmentResolver->getActiveEnvironmentName());

return $instance;
}
Expand Down
9 changes: 0 additions & 9 deletions modules/helfi_paragraphs_news_list/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@ News items are fetched runtime using [external_entities](https://www.drupal.org/

Enable the `helfi_paragraphs_news_list` module.

## Overriding the source environment

The source environment (production by default) can be changed by overriding the `helfi_paragraphs_news_list.settings.source_environment` configuration.

For example:
```php
# public/sites/default/local.settings.php
$config['helfi_paragraphs_news_list.settings']['source_environment'] = 'dev';
```

## How to add new fields to paragraph type

Expand Down

This file was deleted.

37 changes: 9 additions & 28 deletions modules/helfi_paragraphs_news_list/src/HelfiExternalEntityBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
use Drupal\external_entities\ExternalEntityInterface;
use Drupal\external_entities\StorageClient\ExternalEntityStorageClientBase;
use Drupal\helfi_api_base\Environment\Environment;
use Drupal\helfi_api_base\Environment\EnvironmentEnum;
use Drupal\helfi_api_base\Environment\EnvironmentResolverInterface;
use Drupal\helfi_api_base\Environment\Project;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\GuzzleException;
Expand Down Expand Up @@ -52,11 +50,11 @@ abstract class HelfiExternalEntityBase extends ExternalEntityStorageClientBase {
protected string $endpoint;

/**
* The environment resolver.
* The active endpoint environment.
*
* @var \Drupal\helfi_api_base\Environment\EnvironmentResolverInterface
* @var \Drupal\helfi_api_base\Environment\Environment
*/
protected EnvironmentResolverInterface $environmentResolver;
private Environment $environment;

/**
* The config factory service.
Expand All @@ -77,8 +75,12 @@ public static function create(
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance->languageManager = $container->get('language_manager');
$instance->client = $container->get('http_client');
$instance->environmentResolver = $container->get('helfi_api_base.environment_resolver');
$instance->configFactory = $container->get('config.factory');
/** @var \Drupal\helfi_api_base\Environment\EnvironmentResolver $environmentResolver */
$environmentResolver = $container->get('helfi_api_base.environment_resolver');
$instance->environment = $environmentResolver
->getEnvironment(Project::ETUSIVU, $environmentResolver->getActiveEnvironmentName());

return $instance;
}

Expand Down Expand Up @@ -191,27 +193,6 @@ public function delete(ExternalEntityInterface $entity) : void {
throw new EntityStorageException('::delete() is not supported.');
}

/**
* Gets the endpoint environment.
*
* @return \Drupal\helfi_api_base\Environment\Environment
* The environment.
*/
protected function getEnvironment() : Environment {
static $environment = NULL;

if ($environment === NULL) {
$environment = $this->configFactory->get('helfi_paragraphs_news_list.settings')
->get('source_environment') ?: EnvironmentEnum::Prod->value;

/** @var \Drupal\helfi_api_base\Environment\EnvironmentResolver $environmentResolver */
$environment = $this->environmentResolver
->getEnvironment(Project::ETUSIVU, $environment);
}

return $environment;
}

/**
* Formats the JSON response.
*
Expand Down Expand Up @@ -318,7 +299,7 @@ protected function request(
->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
->getId();
$uri = vsprintf('%s%s?%s', [
$this->getEnvironment()->getInternalAddress($langcode),
$this->environment->getInternalAddress($langcode),
$this->endpoint,
\GuzzleHttp\http_build_query($parameters),
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

use Drupal\Core\Entity\EntityStorageException;
use Drupal\external_entities\ExternalEntityStorageInterface;
use Drupal\helfi_api_base\Environment\EnvironmentEnum;
use Drupal\helfi_api_base\Environment\Project;
use Drupal\Tests\helfi_api_base\Traits\ApiTestTrait;
use Drupal\Tests\helfi_api_base\Traits\EnvironmentResolverTrait;
use Drupal\Tests\helfi_api_base\Traits\TestLoggerTrait;
use GuzzleHttp\Exception\RequestException;
use Psr\Http\Message\RequestInterface;
Expand All @@ -20,13 +23,15 @@ class NewsExternalEntityTest extends KernelTestBase {

use ApiTestTrait;
use TestLoggerTrait;
use EnvironmentResolverTrait;

/**
* {@inheritdoc}
*/
public function setUp(): void {
parent::setUp();
$this->setUpMockLogger();
$this->setActiveProject(Project::ASUMINEN, EnvironmentEnum::Local);
}

/**
Expand Down
Loading