Skip to content
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
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ In either case, configure Doctrine's interface mapping to deal with your custom
doctrine:
orm:
resolve_target_entities:
\Webfactory\NewsletterRegistrationBundle\Entity\NewsletterInterface: '\AppBundle\Newsletter\Entity\Newsletter'
\Webfactory\NewsletterRegistrationBundle\Entity\CategoryInterface: '\AppBundle\Newsletter\Entity\Category'
```

Side node: The templates and example above assume that you want to keep your Newsletter classes inside a Newsletter
Expand Down Expand Up @@ -116,15 +116,15 @@ implementations:
# src/services.yml

services:
AppBundle\Newsletter\Entity\NewsletterRepository:
AppBundle\Newsletter\Entity\CategoryRepository:
factory:
- '@doctrine.orm.entity_manager'
- 'getRepository'
arguments:
- 'AppBundle\Newsletter\Entity\Newsletter'
- 'AppBundle\Newsletter\Entity\Category'

Webfactory\NewsletterRegistrationBundle\Entity\NewsletterRepositoryInterface:
alias: 'AppBundle\Newsletter\Entity\NewsletterRepository'
Webfactory\NewsletterRegistrationBundle\Entity\CategoryRepositoryInterface:
alias: 'AppBundle\Newsletter\Entity\CategoryRepository'

AppBundle\Newsletter\Entity\PendingOptInRepository:
factory:
Expand Down
11 changes: 11 additions & 0 deletions Resources/app-class-templates/Category.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace AppBundle\Newsletter\Entity;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: CategoryRepository::class)]
#[ORM\Table(name: 'wfd_newsletterCategory')]
class Category extends \Webfactory\NewsletterRegistrationBundle\Entity\Category
{
}
7 changes: 7 additions & 0 deletions Resources/app-class-templates/CategoryRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace AppBundle\Newsletter\Entity;

class CategoryRepository extends \Webfactory\NewsletterRegistrationBundle\Entity\CategoryRepository
{
}
11 changes: 0 additions & 11 deletions Resources/app-class-templates/Newsletter.php

This file was deleted.

7 changes: 0 additions & 7 deletions Resources/app-class-templates/NewsletterRepository.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
<source>edit.registration.updated</source>
<target>Ihre Registrierung wurde aktualisiert.</target>
</trans-unit>
<trans-unit id="AGtLjHG" resname="edit.registration.updated.no.newsletters.chosen">
<source>edit.registration.updated.no.newsletters.chosen</source>
<trans-unit id="AGtLjHG" resname="edit.registration.updated.no.categories.chosen">
<source>edit.registration.updated.no.categories.chosen</source>
<target>Ihre Newsletter-Abos wurden gelöscht, aber Ihre Registrierungsdaten sind (wie Ihre Email-Adresse) sind weitertin in unserer Datenbank gespeichert. Wenn Sie diese auch löschen möchten, nutzen Sie bitte den untenstehende Knopf.</target>
</trans-unit>
<trans-unit id="T83i0qx" resname="delete.registration.success">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
<source>edit.registration.updated</source>
<target>Your newsletter registration was updated.</target>
</trans-unit>
<trans-unit id="AGtLjHG" resname="edit.registration.updated.no.newsletters.chosen">
<source>edit.registration.updated.no.newsletters.chosen</source>
<trans-unit id="AGtLjHG" resname="edit.registration.updated.no.categories.chosen">
<source>edit.registration.updated.no.categories.chosen</source>
<target>All your newsletter subscriptions have been deleted, but your registration data (like your email address) is still saved in our database. If you would like to delete that data too, please delete your registration with the button below.</target>
</trans-unit>
<trans-unit id="T83i0qx" resname="delete.registration.success">
Expand Down
4 changes: 2 additions & 2 deletions src/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@ public function editRegistration(string $uuid, Request $request, FlashBagAwareSe

if ($editForm->isSubmitted() && $editForm->isValid()) {
$this->editRegistrationTask->editRegistration($recipient);
$messageKey = \count($recipient->getNewsletters()) > 0
$messageKey = \count($recipient->getCategories()) > 0
? 'edit.registration.updated'
: 'edit.registration.updated.no.newsletters.chosen';
: 'edit.registration.updated.no.categories.chosen';
$session->getFlashBag()->add(
'success',
$this->translator->trans($messageKey, [], 'webfactory-newsletter-registration')
Expand Down
4 changes: 2 additions & 2 deletions src/DependencyInjection/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ services:

Webfactory\NewsletterRegistrationBundle\StartRegistration\Type:
arguments:
- '@Webfactory\NewsletterRegistrationBundle\Entity\NewsletterRepositoryInterface'
- '@Webfactory\NewsletterRegistrationBundle\Entity\CategoryRepositoryInterface'
- '@Webfactory\NewsletterRegistrationBundle\Entity\PendingOptInFactory'
tags: ['form.type']

Expand Down Expand Up @@ -44,7 +44,7 @@ services:

Webfactory\NewsletterRegistrationBundle\EditRegistration\Type:
arguments:
- '@Webfactory\NewsletterRegistrationBundle\Entity\NewsletterRepositoryInterface'
- '@Webfactory\NewsletterRegistrationBundle\Entity\CategoryRepositoryInterface'
tags: ['form.type']

Webfactory\NewsletterRegistrationBundle\DeleteRegistration\Type:
Expand Down
16 changes: 8 additions & 8 deletions src/EditRegistration/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormBuilderInterface;
use Webfactory\NewsletterRegistrationBundle\Entity\NewsletterRepositoryInterface;
use Webfactory\NewsletterRegistrationBundle\Entity\CategoryRepositoryInterface;

class Type extends AbstractType
{
use TypeHasNewslettersElementTrait;
use TypeHasCategoriesElementTrait;

public const ELEMENT_NEWSLETTERS = 'newsletters';
public const ELEMENT_CATEGORIES = 'categories';

public function __construct(NewsletterRepositoryInterface $newsletterRepository)
public function __construct(CategoryRepositoryInterface $categoryRepository)
{
$this->newsletterRepository = $newsletterRepository;
$this->categoryRepository = $categoryRepository;
}

public function buildForm(FormBuilderInterface $builder, array $options): void
{
$this->addNewslettersElementToForm($builder, false);
$this->addCategoriesElementToForm($builder, false);

// We need at least one element in addition to the newsletters above, so that Symfony recognizes the form being
// submitted even if no newsletters where chosen.
// We need at least one element in addition to the categories above, so that Symfony recognizes the form being
// submitted even if no categories where chosen.
$builder->add('hidden', HiddenType::class, ['mapped' => false]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints\Choice;
use Webfactory\NewsletterRegistrationBundle\Entity\NewsletterRepositoryInterface;
use Webfactory\NewsletterRegistrationBundle\Entity\CategoryRepositoryInterface;

trait TypeHasNewslettersElementTrait
trait TypeHasCategoriesElementTrait
{
protected NewsletterRepositoryInterface $newsletterRepository;
protected CategoryRepositoryInterface $categoryRepository;

protected function addNewslettersElementToForm(FormBuilderInterface $builder, bool $recipientHasToChooseAtLeastOne)
protected function addCategoriesElementToForm(FormBuilderInterface $builder, bool $recipientHasToChooseAtLeastOne)
{
// add newsletter choices, if there is more than one
$choices = $this->newsletterRepository->findVisible();
// add category choices, if there is more than one
$choices = $this->categoryRepository->findVisible();
if (\count($choices) < 2) {
return;
}
Expand All @@ -25,10 +25,10 @@ protected function addNewslettersElementToForm(FormBuilderInterface $builder, bo
}

$builder->add(
self::ELEMENT_NEWSLETTERS,
self::ELEMENT_CATEGORIES,
ChoiceType::class,
[
'label' => 'Newsletters',
'label' => 'Categories',
'multiple' => true,
'expanded' => true,
'choices' => $choices,
Expand Down
4 changes: 2 additions & 2 deletions src/Entity/Newsletter.php → src/Entity/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Doctrine\ORM\Mapping as ORM;

#[ORM\MappedSuperclass]
abstract class Newsletter implements NewsletterInterface
abstract class Category implements CategoryInterface
{
#[ORM\Id]
#[ORM\GeneratedValue]
Expand All @@ -18,7 +18,7 @@ abstract class Newsletter implements NewsletterInterface
#[ORM\Column(type: 'boolean', nullable: false, options: ['default' => true])]
protected bool $visible;

/** Used for sorting amongst other Newsletters. */
/** Used for sorting amongst other Categories. */
#[ORM\Column(type: 'integer', nullable: false, options: ['default' => 0])]
protected int $rank;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Webfactory\NewsletterRegistrationBundle\Entity;

interface NewsletterInterface
interface CategoryInterface
{
public function getName(): string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Doctrine\ORM\EntityRepository;

abstract class NewsletterRepository extends EntityRepository implements NewsletterRepositoryInterface
abstract class CategoryRepository extends EntityRepository implements CategoryRepositoryInterface
{
public function findVisible(): array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Webfactory\NewsletterRegistrationBundle\Entity;

interface NewsletterRepositoryInterface
interface CategoryRepositoryInterface
{
/**
* @return NewsletterInterface[]
* @return CategoryInterface[]
*/
public function findVisible(): array;
}
16 changes: 8 additions & 8 deletions src/Entity/PendingOptIn.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ abstract class PendingOptIn implements PendingOptInInterface
protected DateTimeImmutable $registrationDate;

/**
* @var Collection<int, NewsletterInterface>
* @var Collection<int, CategoryInterface>
*/
#[ORM\ManyToMany(targetEntity: NewsletterInterface::class)]
#[ORM\ManyToMany(targetEntity: CategoryInterface::class)]
#[ORM\JoinTable(
joinColumns: [new ORM\JoinColumn(referencedColumnName: 'uuid', onDelete: 'CASCADE')],
inverseJoinColumns: [new ORM\JoinColumn(onDelete: 'CASCADE')]
)]
protected Collection $newsletters;
protected Collection $categories;

public static function fromRegistrationFormData(array $formData): ?PendingOptInInterface
{
Expand All @@ -48,20 +48,20 @@ public static function fromRegistrationFormData(array $formData): ?PendingOptInI
return new static(
null,
$emailAddress,
$formData[StartRegistrationType::ELEMENT_NEWSLETTERS] ?? []
$formData[StartRegistrationType::ELEMENT_CATEGORIES] ?? []
);
}

public function __construct(
?string $uuid,
EmailAddress $emailAddress,
array $newsletters = [],
array $categories = [],
?DateTimeImmutable $registrationDate = null
) {
$this->uuid = $uuid ?: Uuid::uuid4()->toString();
$this->emailAddress = $emailAddress;
$this->emailAddressHash = $emailAddress->getHash();
$this->newsletters = new ArrayCollection($newsletters);
$this->categories = new ArrayCollection($categories);
$this->registrationDate = $registrationDate ?: new DateTimeImmutable();
}

Expand All @@ -84,9 +84,9 @@ public function setEmailAddressIfItMatchesStoredHash(EmailAddress $emailAddress)
$this->emailAddress = $emailAddress;
}

public function getNewsletters(): array
public function getCategories(): array
{
return $this->newsletters->toArray();
return $this->categories->toArray();
}

public function getRegistrationDate(): DateTimeImmutable
Expand Down
4 changes: 2 additions & 2 deletions src/Entity/PendingOptInInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public function getEmailAddress(): EmailAddress;
public function setEmailAddressIfItMatchesStoredHash(EmailAddress $email): void;

/**
* @return NewsletterInterface[]
* @return CategoryInterface[]
*/
public function getNewsletters(): array;
public function getCategories(): array;

public function getRegistrationDate(): DateTimeImmutable;

Expand Down
20 changes: 10 additions & 10 deletions src/Entity/Recipient.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,33 @@ abstract class Recipient implements RecipientInterface
protected DateTimeImmutable $optInDate;

/**
* @var Collection<int, NewsletterInterface>
* @var Collection<int, CategoryInterface>
*/
#[ORM\ManyToMany(targetEntity: NewsletterInterface::class)]
#[ORM\ManyToMany(targetEntity: CategoryInterface::class)]
#[ORM\JoinTable(
joinColumns: [new ORM\JoinColumn(onDelete: 'CASCADE')],
inverseJoinColumns: [new ORM\JoinColumn(onDelete: 'CASCADE')]
)]
protected Collection $newsletters;
protected Collection $categories;

public static function fromPendingOptIn(PendingOptInInterface $pendingOptIn): RecipientInterface
{
return new static(
$pendingOptIn->getUuid(),
$pendingOptIn->getEmailAddress(),
$pendingOptIn->getNewsletters()
$pendingOptIn->getCategories()
);
}

public function __construct(
?string $uuid,
EmailAddress $emailAddress,
array $newsletters = [],
array $categories = [],
?DateTimeImmutable $optInDate = null
) {
$this->uuid = $uuid ?: Uuid::uuid4()->toString();
$this->emailAddress = $emailAddress->getEmailAddress();
$this->newsletters = new ArrayCollection($newsletters);
$this->categories = new ArrayCollection($categories);
$this->optInDate = $optInDate ?: new DateTimeImmutable();
}

Expand All @@ -75,13 +75,13 @@ public function getOptInDate(): DateTimeImmutable
return $this->optInDate;
}

public function getNewsletters(): array
public function getCategories(): array
{
return $this->newsletters->toArray();
return $this->categories->toArray();
}

public function setNewsletters(array $newsletters): void
public function setCategories(array $categories): void
{
$this->newsletters = new ArrayCollection($newsletters);
$this->categories = new ArrayCollection($categories);
}
}
8 changes: 4 additions & 4 deletions src/Entity/RecipientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ public function getUuid(): string;
public function getEmailAddress(): EmailAddress;

/**
* @return NewsletterInterface[]
* @return CategoryInterface[]
*/
public function getNewsletters(): array;
public function getCategories(): array;

/**
* @param NewsletterInterface[] $newsletters
* @param CategoryInterface[] $categories
*/
public function setNewsletters(array $newsletters): void;
public function setCategories(array $categories): void;
}
Loading
Loading