Skip to content

Commit

Permalink
3208: Changed to only show allowed recipients in slide creation flow
Browse files Browse the repository at this point in the history
  • Loading branch information
tuj committed Dec 19, 2024
1 parent b6c052b commit e05ab3f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 21 deletions.
5 changes: 4 additions & 1 deletion src/Feed/SourceType/Colibo/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ public function __construct(
* An array of recipient ID's to filter by
* @param array $publishers
* An array of publisher ID's to filter by
* @param int $pageSize
* Number of elements to retrieve
*
* @return mixed
*/
public function getFeedEntriesNews(FeedSource $feedSource, array $recipients = [], array $publishers = []): mixed
public function getFeedEntriesNews(FeedSource $feedSource, array $recipients = [], array $publishers = [], int $pageSize = 10): mixed
{
try {
$client = $this->getApiClient($feedSource);
Expand All @@ -55,6 +57,7 @@ public function getFeedEntriesNews(FeedSource $feedSource, array $recipients = [
'Id' => $publisher,
'Type' => 'Group'
], $publishers),
'pageSize' => $pageSize,
],
];

Expand Down
59 changes: 39 additions & 20 deletions src/Feed/SourceType/Colibo/ColiboFeedType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use FeedIo\Feed\Item;
use FeedIo\Feed\Node\Category;
use Psr\Cache\CacheItemInterface;
use Symfony\Component\BrowserKit\Exception\JsonException;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Uid\Ulid;
Expand All @@ -38,28 +39,28 @@ public function __construct(

public function getAdminFormOptions(FeedSource $feedSource): array
{
$feedEntryRecipients = $this->feedService->getFeedSourceConfigUrl($feedSource, 'recipients');
$feedEntryPublishers = $this->feedService->getFeedSourceConfigUrl($feedSource, 'publishers');
$feedEntryRecipients = $this->feedService->getFeedSourceConfigUrl($feedSource, 'allowed-recipients');

return [
[
'key' => 'colibo-feed-type-publishers-selector',
'key' => 'colibo-feed-type-recipient-selector',
'input' => 'multiselect-from-endpoint',
'endpoint' => $feedEntryRecipients,
'name' => 'recipients',
'label' => 'Modtagergrupper',
'label' => 'Grupper',
'helpText' => 'Vælg hvilke grupper, der skal hentes nyheder fra.',
'formGroupClasses' => 'mb-3',
],
[
'key' => 'colibo-feed-type-publishers-selector',
'input' => 'multiselect-from-endpoint',
'endpoint' => $feedEntryPublishers,
'name' => 'publishers',
'label' => 'Afsendergrupper',
'helpText' => 'Vælg afsendergrupper for at begrænse hvilke afsenderes nyheder, der vises fra modtagergruppen. Hvis den ikke er valgt vises alle nyheder fra modtagergruppen.',
'key' => 'colibo-feed-type-page-size',
'input' => 'input',
'type' => 'number',
'name' => 'page_size',
'label' => 'Antal nyheder',
'defaultValue' => '5',
'helpText' => 'Vælg hvor mange nyheder der maksimalt skal hentes.',
'formGroupClasses' => 'mb-3',
]
],
];
}

Expand All @@ -73,15 +74,15 @@ public function getData(Feed $feed): array
'entries' => [],
];

$recipients = $configuration['recipients'] ?? null;
$recipients = $configuration['recipients'] ?? [];
$publishers = $configuration['publishers'] ?? [];
$pageSize = isset($configuration['page_size']) ? (int) $configuration['page_size'] : 10;

if (null === $recipients) {
if (empty($recipients)) {

Check failure on line 81 in src/Feed/SourceType/Colibo/ColiboFeedType.php

View workflow job for this annotation

GitHub Actions / Psalm (PHP 8.3)

RiskyTruthyFalsyComparison

src/Feed/SourceType/Colibo/ColiboFeedType.php:81:13: RiskyTruthyFalsyComparison: Operand of type array<never, never>|mixed contains type mixed, which can be falsy and truthy. This can cause possibly unexpected behavior. Use strict comparison instead. (see https://psalm.dev/356)
return $result;
}

$entries = $this->apiClient->getFeedEntriesNews($feed->getFeedSource(), $recipients, $publishers);

$entries = $this->apiClient->getFeedEntriesNews($feed->getFeedSource(), $recipients, $publishers, $pageSize);

Check failure on line 85 in src/Feed/SourceType/Colibo/ColiboFeedType.php

View workflow job for this annotation

GitHub Actions / Psalm (PHP 8.3)

PossiblyNullArgument

src/Feed/SourceType/Colibo/ColiboFeedType.php:85:57: PossiblyNullArgument: Argument 1 of App\Feed\SourceType\Colibo\ApiClient::getFeedEntriesNews cannot be null, possibly null value provided (see https://psalm.dev/078)

foreach ($entries as $entry) {
$item = new Item();
Expand Down Expand Up @@ -118,7 +119,12 @@ public function getData(Feed $feed): array
$item->setAuthor($author);

if ($entry->fields->galleryItems !== null) {
$galleryItems = json_decode($entry->fields->galleryItems, true, 512, JSON_THROW_ON_ERROR);
try {
$galleryItems = json_decode($entry->fields->galleryItems, true, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException) {
$galleryItems = [];
}

foreach ($galleryItems as $galleryItem) {
$media = new Item\Media();

Expand Down Expand Up @@ -148,8 +154,12 @@ public function getData(Feed $feed): array
public function getConfigOptions(Request $request, FeedSource $feedSource, string $name): ?array
{
switch ($name) {
case 'allowed-recipients':
$allowedIds = $feedSource->getSecrets()['allowed_recipients'] ?? [];
$allGroupOptions = $this->getConfigOptions($request, $feedSource, 'recipients');

return array_values(array_filter($allGroupOptions, fn(ConfigOption $group) => in_array($group->value, $allowedIds)));
case 'recipients':
case 'publishers':
$id = self::getIdKey($feedSource);

/** @var CacheItemInterface $cacheItem */
Expand All @@ -175,7 +185,6 @@ public function getConfigOptions(Request $request, FeedSource $feedSource, strin
}

return $groups;

default:
return null;
}
Expand All @@ -194,12 +203,16 @@ public function getRequiredSecrets(): array
'client_secret' => [
'type' => 'string'
],
'allowed_recipients' => [
'type' => 'string_array',
'exposeValue' => true,
]
];
}

public function getRequiredConfiguration(): array
{
return ['recipients', 'publishers'];
return ['recipients', 'page_size'];
}

public function getSupportedFeedOutputType(): string
Expand All @@ -223,8 +236,14 @@ public function getSchema(): array
'client_secret' => [
'type' => 'string',
],
'allowed_recipients' => [
'type' => 'array',
'items' => [
'type' => 'string',
],
],
],
'required' => ['api_base_uri', 'client_id', 'client_secret'],
'required' => ['api_base_uri', 'client_id', 'client_secret', 'allowed_recipients'],
];
}

Expand Down

0 comments on commit e05ab3f

Please sign in to comment.