Skip to content

Commit

Permalink
Merge 1805ba2 into b1ca890
Browse files Browse the repository at this point in the history
  • Loading branch information
tuutti authored Jun 2, 2023
2 parents b1ca890 + 1805ba2 commit f5d119b
Show file tree
Hide file tree
Showing 22 changed files with 568 additions and 148 deletions.
5 changes: 0 additions & 5 deletions helfi_navigation.module
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ declare(strict_types = 1);
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\menu_link_content\MenuLinkContentInterface;
Expand Down Expand Up @@ -156,10 +155,6 @@ function helfi_navigation_cron() : void {
* Implements hook_page_attachments().
*/
function helfi_navigation_page_attachments(array &$attachments) : void {
$langcode = Drupal::languageManager()
->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
->getId();

/** @var \Drupal\helfi_api_base\Language\DefaultLanguageResolver $defaultLanguageResolver */
$defaultLanguageResolver = Drupal::service('helfi_api_base.default_language_resolver');

Expand Down
9 changes: 1 addition & 8 deletions helfi_navigation.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ services:
- '@config.factory'
- '@helfi_navigation.api_manager'
- '@helfi_navigation.menu_tree_builder'
- '@account_switcher'
helfi_navigation.menu_tree_builder:
class: Drupal\helfi_navigation\Menu\MenuTreeBuilder
arguments:
Expand All @@ -31,14 +32,6 @@ services:
- '@menu.link_tree'
- '@plugin.manager.menu.link'
- '@event_dispatcher'
helfi_navigation.anonymous_user:
class: Drupal\Core\Session\AnonymousUserSession
helfi_navigation.menu_tree_manipulators:
class: Drupal\helfi_navigation\Menu\MenuTreeManipulator
arguments:
- '@access_manager'
- '@helfi_navigation.anonymous_user'
- '@entity_type.manager'
helfi_navigation.cache_warmer:
class: Drupal\helfi_navigation\CacheWarmer
arguments:
Expand Down
43 changes: 29 additions & 14 deletions src/ApiManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\TransferException;
use GuzzleHttp\Utils;
use Psr\Log\LoggerInterface;

/**
Expand All @@ -34,7 +35,7 @@ class ApiManager {
*
* @var null|string
*/
private ?string $authorization;
private ?string $authorization = NULL;

/**
* The previous exception.
Expand Down Expand Up @@ -67,14 +68,13 @@ class ApiManager {
* The config factory.
*/
public function __construct(
private TimeInterface $time,
private CacheBackendInterface $cache,
private ClientInterface $httpClient,
private EnvironmentResolverInterface $environmentResolver,
private LoggerInterface $logger,
ConfigFactoryInterface $configFactory
private readonly TimeInterface $time,
private readonly CacheBackendInterface $cache,
private readonly ClientInterface $httpClient,
private readonly EnvironmentResolverInterface $environmentResolver,
private readonly LoggerInterface $logger,
private readonly ConfigFactoryInterface $configFactory
) {
$this->authorization = $configFactory->get('helfi_navigation.api')->get('key');
}

/**
Expand Down Expand Up @@ -186,14 +186,14 @@ public function get(
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function update(string $langcode, array $data) : ApiResponse {
if (!$this->authorization) {
if (!$this->hasAuthorization()) {
throw new ConfigException('Missing "helfi_navigation.api" key setting.');
}

$endpoint = sprintf('%s/%s', static::GLOBAL_MENU_ENDPOINT, $this->environmentResolver->getActiveEnvironment()->getId());
return $this->makeRequest('POST', $endpoint, $langcode, [
'json' => $data,
'headers' => ['Authorization' => sprintf('Basic %s', $this->authorization)],
'headers' => ['Authorization' => sprintf('Basic %s', $this->getAuthorization())],
]);
}

Expand Down Expand Up @@ -261,8 +261,23 @@ public function getUrl(string $type, string $langcode, array $options = []) : st
* @return bool
* Is the system authorized to use secured endpoints.
*/
public function isAuthorized(): bool {
return (bool) $this->authorization;
public function hasAuthorization(): bool {
return (bool) $this->getAuthorization();
}

/**
* Gets the authorization.
*
* @return string|null
* The authorization token.
*/
public function getAuthorization() : ?string {
if (!$this->authorization) {
$this->authorization = $this->configFactory
->get('helfi_navigation.api')
->get('key');
}
return $this->authorization;
}

/**
Expand Down Expand Up @@ -305,7 +320,7 @@ private function makeRequest(
}
$response = $this->httpClient->request($method, $url, $options);

return new ApiResponse(\GuzzleHttp\json_decode($response->getBody()->getContents()));
return new ApiResponse(Utils::jsonDecode($response->getBody()->getContents()));
}
catch (\Exception $e) {
if ($e instanceof GuzzleException) {
Expand All @@ -332,7 +347,7 @@ private function makeRequest(
sprintf('[%s]. Attempted to use mock data, but the mock file "%s" was not found for "%s" endpoint.', $e->getMessage(), basename($fileName), $endpoint)
);
}
return new ApiResponse(\GuzzleHttp\json_decode(file_get_contents($fileName)));
return new ApiResponse(Utils::jsonDecode(file_get_contents($fileName)));
}
// Log the error and re-throw the exception.
$this->logger->error('Request failed with error: ' . $e->getMessage());
Expand Down
9 changes: 6 additions & 3 deletions src/EventSubscriber/RedirectEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ final class RedirectEventSubscriber implements EventSubscriberInterface {
* The config factory service.
*/
public function __construct(
private RedirectRepository $repository,
private ConfigFactoryInterface $configFactory,
private readonly RedirectRepository $repository,
private readonly ConfigFactoryInterface $configFactory,
) {
}

Expand All @@ -35,7 +35,10 @@ public function __construct(
* Gets all available redirects for given link and updates the URL
* to use the redirect destination.
*
* This is required by javascript navigation to build the active trail.
* Mobile navigation uses path to figure out the active trail, something
* like "if (path in menu link === current url path in browser)". Override
* the menu link path to match the path where user actually lands after
* clicking the link.
*
* @param \Drupal\helfi_navigation\Event\MenuTreeBuilderLink $event
* The event to respond to.
Expand Down
13 changes: 3 additions & 10 deletions src/ExternalMenuTreeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ final class ExternalMenuTreeBuilder {
* The request stack.
*/
public function __construct(
private InternalDomainResolver $domainResolver,
private RequestStack $requestStack,
private readonly InternalDomainResolver $domainResolver,
private readonly RequestStack $requestStack,
) {
}

Expand Down Expand Up @@ -145,14 +145,7 @@ private function createLink(
if (!isset($item->parentId)) {
$item->parentId = NULL;
}

if (!isset($item->external)) {
$item->external = $this->domainResolver->isExternal($item->url);
}

if (isset($item->description)) {
$link_definition['description'] = $item->description;
}
$item->external = $this->domainResolver->isExternal($item->url);

if (isset($item->weight)) {
$link_definition['weight'] = $item->weight;
Expand Down
27 changes: 18 additions & 9 deletions src/MainMenuManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
namespace Drupal\helfi_navigation;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Session\AccountSwitcherInterface;
use Drupal\Core\Session\AnonymousUserSession;
use Drupal\Core\Url;
use Drupal\helfi_navigation\Menu\MenuTreeBuilder;
use Drupal\language\ConfigurableLanguageManagerInterface;
Expand All @@ -26,12 +27,15 @@ class MainMenuManager {
* The api manager.
* @param \Drupal\helfi_navigation\Menu\MenuTreeBuilder $menuTreeBuilder
* The menu tree builder.
* @param \Drupal\Core\Session\AccountSwitcherInterface $accountSwitcher
* The account switcher service.
*/
public function __construct(
private ConfigurableLanguageManagerInterface $languageManager,
private ConfigFactoryInterface $config,
private ApiManager $apiManager,
private MenuTreeBuilder $menuTreeBuilder,
private readonly ConfigurableLanguageManagerInterface $languageManager,
private readonly ConfigFactoryInterface $config,
private readonly ApiManager $apiManager,
private readonly MenuTreeBuilder $menuTreeBuilder,
private readonly AccountSwitcherInterface $accountSwitcher,
) {
}

Expand All @@ -41,11 +45,13 @@ public function __construct(
* @param string $langcode
* The langcode.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \InvalidArgumentException
*/
public function sync(string $langcode): bool {
// Sync menu as an anonymous user to make sure no sensitive
// links are synced.
$this->accountSwitcher->switchTo(new AnonymousUserSession());
$response = $this->apiManager->update(
$langcode,
[
Expand All @@ -54,6 +60,8 @@ public function sync(string $langcode): bool {
'menu_tree' => $this->build($langcode),
]
);
$this->accountSwitcher->switchBack();

if (!isset($response->data->status)) {
throw new \InvalidArgumentException('Failed to parse entity published state.');
}
Expand Down Expand Up @@ -95,9 +103,10 @@ public function getSiteName(string $langcode) : string {
*
* @return mixed
* Menu tree.
*
* @throws \InvalidArgumentException
*/
public function build(string $langcode = NULL): array {
$langcode = $langcode ?: $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_INTERFACE)->getId();
public function build(string $langcode): array {
$siteName = $this->getSiteName($langcode);

$instanceUri = Url::fromRoute('<front>', options: [
Expand Down
Loading

0 comments on commit f5d119b

Please sign in to comment.