Skip to content

Commit

Permalink
Added FormConfigStamp (#367)
Browse files Browse the repository at this point in the history
  • Loading branch information
Toflar authored Oct 14, 2024
1 parent dce6aa4 commit 08aff9a
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/listeners.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
->args([
service(NotificationCenter::class),
service(FileUploadNormalizer::class),
service(ConfigLoader::class),
])
;

Expand Down
5 changes: 5 additions & 0 deletions src/Config/ConfigLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public function loadModule(int $id): ModuleConfig|null
return $this->loadConfig($id, 'tl_module', ModuleConfig::class);
}

public function loadForm(int $id): FormConfig|null
{
return $this->loadConfig($id, 'tl_form', FormConfig::class);
}

/**
* @return array<MessageConfig>
*/
Expand Down
9 changes: 9 additions & 0 deletions src/Config/FormConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Terminal42\NotificationCenterBundle\Config;

class FormConfig extends AbstractConfig
{
}
9 changes: 9 additions & 0 deletions src/EventListener/ProcessFormDataListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@
use Contao\Form;
use Contao\StringUtil;
use Terminal42\NotificationCenterBundle\BulkyItem\FileItem;
use Terminal42\NotificationCenterBundle\Config\ConfigLoader;
use Terminal42\NotificationCenterBundle\NotificationCenter;
use Terminal42\NotificationCenterBundle\Parcel\Stamp\BulkyItemsStamp;
use Terminal42\NotificationCenterBundle\Parcel\Stamp\FormConfigStamp;

#[AsHook('processFormData')]
class ProcessFormDataListener
{
public function __construct(
private readonly NotificationCenter $notificationCenter,
private readonly FileUploadNormalizer $fileUploadNormalizer,
private readonly ConfigLoader $configLoader,
) {
}

Expand Down Expand Up @@ -95,6 +98,12 @@ public function __invoke(array $submittedData, array $formData, array|null $file
$stamps = $stamps->with(new BulkyItemsStamp($bulkyItemVouchers));
}

$formConfig = $this->configLoader->loadForm((int) $form->id);

if (null !== $formConfig) {
$stamps = $stamps->with(new FormConfigStamp($formConfig));
}

$this->notificationCenter->sendNotificationWithStamps((int) $formData['nc_notification'], $stamps);
}
}
20 changes: 20 additions & 0 deletions src/Parcel/Stamp/FormConfigStamp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Terminal42\NotificationCenterBundle\Parcel\Stamp;

use Terminal42\NotificationCenterBundle\Config\FormConfig;

class FormConfigStamp extends AbstractConfigStamp
{
public function __construct(public FormConfig $formConfig)
{
parent::__construct($this->formConfig);
}

public static function fromArray(array $data): self
{
return new self(FormConfig::fromArray($data));
}
}
13 changes: 13 additions & 0 deletions src/Token/TokenCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ public static function fromSerializedArray(array $data): self
return new self($tokens);
}

public function replaceToken(Token $token): self
{
$existing = $this->getByName($token->getName());

if (null !== $existing) {
$this->remove($existing);
}

$this->addToken($token);

return $this;
}

/**
* Provides a fluent interface alternative to add() with a type hint.
*/
Expand Down
17 changes: 17 additions & 0 deletions tests/Token/TokenCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ public function testCollectionHandling(): void
$this->assertNull($tokenCollection->getByName('form_i_do_not_exist'));
}

public function testReplace(): void
{
$tokenCollection = new TokenCollection();
$tokenCollection->addToken(Token::fromValue('test', 'foobar'));
$tokenCollection->addToken(Token::fromValue('test', 'foobar new'));

$this->assertCount(2, $tokenCollection);
$this->assertSame('foobar', $tokenCollection->getByName('test')->getValue(), 'foobar');

$tokenCollection = new TokenCollection();
$tokenCollection->replaceToken(Token::fromValue('test', 'foobar'));
$tokenCollection->replaceToken(Token::fromValue('test', 'foobar new'));

$this->assertCount(1, $tokenCollection);
$this->assertSame('foobar new', $tokenCollection->getByName('test')->getValue(), 'foobar');
}

public function testMerge(): void
{
$tokenCollectionA = new TokenCollection();
Expand Down

0 comments on commit 08aff9a

Please sign in to comment.