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
7 changes: 0 additions & 7 deletions src/Domain/Messaging/Model/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,4 @@ public function getListMessages(): Collection
{
return $this->listMessages;
}

public function getSubscriberLists(): Collection
{
return $this->listMessages->map(
fn(ListMessage $lm) => $lm->getList()
);
}
}
13 changes: 13 additions & 0 deletions src/Domain/Messaging/Repository/MessageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpList\Core\Domain\Common\Repository\Interfaces\PaginatableRepositoryInterface;
use PhpList\Core\Domain\Messaging\Model\Filter\MessageFilter;
use PhpList\Core\Domain\Messaging\Model\Message;
use PhpList\Core\Domain\Subscription\Model\SubscriberList;

class MessageRepository extends AbstractRepository implements PaginatableRepositoryInterface
{
Expand Down Expand Up @@ -50,4 +51,16 @@ public function getFilteredAfterId(int $lastId, int $limit, ?FilterRequestInterf
->getQuery()
->getResult();
}

/** @return Message[] */
public function getMessagesByList(SubscriberList $list): array
{
return $this->createQueryBuilder('m')
->join('m.listMessages', 'lm')
->join('lm.subscriberList', 'l')
->where('l = :list')
->setParameter('list', $list)
->getQuery()
->getResult();
}
}
20 changes: 19 additions & 1 deletion src/Domain/Messaging/Service/Manager/ListMessageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,26 @@
use PhpList\Core\Domain\Messaging\Model\ListMessage;
use PhpList\Core\Domain\Messaging\Model\Message;
use PhpList\Core\Domain\Messaging\Repository\ListMessageRepository;
use PhpList\Core\Domain\Messaging\Repository\MessageRepository;
use PhpList\Core\Domain\Subscription\Model\SubscriberList;
use PhpList\Core\Domain\Subscription\Repository\SubscriberListRepository;

class ListMessageManager
{
private ListMessageRepository $listMessageRepository;
private MessageRepository $messageRepository;
private SubscriberListRepository $subscriberListRepository;
private EntityManagerInterface $entityManager;

public function __construct(
ListMessageRepository $listMessageRepository,
EntityManagerInterface $entityManager
MessageRepository $messageRepository,
SubscriberListRepository $subscriberListRepository,
EntityManagerInterface $entityManager,
) {
$this->listMessageRepository = $listMessageRepository;
$this->messageRepository = $messageRepository;
$this->subscriberListRepository = $subscriberListRepository;
$this->entityManager = $entityManager;
}

Expand Down Expand Up @@ -76,4 +84,14 @@ public function removeAllListAssociationsForMessage(Message $message): void
{
$this->listMessageRepository->removeAllListAssociationsForMessage($message);
}

public function getMessagesByList(SubscriberList $list): array
{
return $this->messageRepository->getMessagesByList($list);
}

public function getListByMessage(Message $message): array
{
return $this->subscriberListRepository->getListsByMessage($message);
}
}
12 changes: 0 additions & 12 deletions src/Domain/Subscription/Model/SubscriberList.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ public function getName(): string
public function setName(string $name): self
{
$this->name = $name;

return $this;
}

Expand Down Expand Up @@ -146,7 +145,6 @@ public function isPublic(): bool
public function setPublic(bool $public): self
{
$this->public = $public;

return $this;
}

Expand All @@ -158,7 +156,6 @@ public function getCategory(): string
public function setCategory(string $category): self
{
$this->category = $category;

return $this;
}

Expand All @@ -170,7 +167,6 @@ public function getOwner(): ?Administrator
public function setOwner(Administrator $owner): self
{
$this->owner = $owner;

return $this;
}

Expand Down Expand Up @@ -223,19 +219,11 @@ public function getUpdatedAt(): ?DateTime
public function updateUpdatedAt(): DomainModel
{
$this->updatedAt = new DateTime();

return $this;
}

public function getListMessages(): Collection
{
return $this->listMessages;
}

public function getMessages(): Collection
{
return $this->listMessages->map(
fn(ListMessage $lm) => $lm->getMessage()
);
}
}
13 changes: 13 additions & 0 deletions src/Domain/Subscription/Repository/SubscriberListRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpList\Core\Domain\Common\Repository\CursorPaginationTrait;
use PhpList\Core\Domain\Common\Repository\Interfaces\PaginatableRepositoryInterface;
use PhpList\Core\Domain\Identity\Model\Administrator;
use PhpList\Core\Domain\Messaging\Model\Message;
use PhpList\Core\Domain\Subscription\Model\SubscriberList;

/**
Expand All @@ -32,4 +33,16 @@ public function findWithSubscription($id)
->getQuery()
->getOneOrNullResult();
}

/** @return SubscriberList[] */
public function getListsByMessage(Message $message): array
{
return $this->createQueryBuilder('l')
->join('l.listMessages', 'lm')
->join('lm.message', 'm')
->where('m = :message')
->setParameter('message', $message)
->getQuery()
->getResult();
}
}
18 changes: 10 additions & 8 deletions src/Domain/Subscription/Service/Provider/SubscriberProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@

use PhpList\Core\Domain\Messaging\Model\Message;
use PhpList\Core\Domain\Subscription\Model\Subscriber;
use PhpList\Core\Domain\Subscription\Model\SubscriberList;
use PhpList\Core\Domain\Subscription\Repository\SubscriberListRepository;
use PhpList\Core\Domain\Subscription\Repository\SubscriberRepository;

class SubscriberProvider
{
private SubscriberRepository $subscriberRepository;
private SubscriberListRepository $subscriberListRepository;

public function __construct(SubscriberRepository $subscriberRepository)
{
public function __construct(
SubscriberRepository $subscriberRepository,
SubscriberListRepository $subscriberListRepository,
) {
$this->subscriberRepository = $subscriberRepository;
$this->subscriberListRepository = $subscriberListRepository;
}

/**
Expand All @@ -26,13 +30,11 @@ public function __construct(SubscriberRepository $subscriberRepository)
*/
public function getSubscribersForMessage(Message $message): array
{
$listIds = $message->getSubscriberLists()
->map(fn(SubscriberList $list) => $list->getId())
->toArray();
$lists = $this->subscriberListRepository->getListsByMessage($message);

$subscribers = [];
foreach ($listIds as $listId) {
$listSubscribers = $this->subscriberRepository->getSubscribersBySubscribedListId($listId);
foreach ($lists as $list) {
$listSubscribers = $this->subscriberRepository->getSubscribersBySubscribedListId($list->getId());
foreach ($listSubscribers as $subscriber) {
$subscribers[$subscriber->getId()] = $subscriber;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
use PhpList\Core\Domain\Messaging\Model\ListMessage;
use PhpList\Core\Domain\Messaging\Model\Message;
use PhpList\Core\Domain\Messaging\Repository\ListMessageRepository;
use PhpList\Core\Domain\Messaging\Repository\MessageRepository;
use PhpList\Core\Domain\Messaging\Service\Manager\ListMessageManager;
use PhpList\Core\Domain\Subscription\Model\SubscriberList;
use PhpList\Core\Domain\Subscription\Repository\SubscriberListRepository;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

Expand All @@ -26,8 +28,10 @@ protected function setUp(): void
$this->entityManager = $this->createMock(EntityManagerInterface::class);

$this->manager = new ListMessageManager(
$this->listMessageRepository,
$this->entityManager
listMessageRepository: $this->listMessageRepository,
messageRepository: $this->createMock(MessageRepository::class),
subscriberListRepository: $this->createMock(SubscriberListRepository::class),
entityManager: $this->entityManager
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,34 @@
use PhpList\Core\Domain\Messaging\Model\Message;
use PhpList\Core\Domain\Subscription\Model\Subscriber;
use PhpList\Core\Domain\Subscription\Model\SubscriberList;
use PhpList\Core\Domain\Subscription\Repository\SubscriberListRepository;
use PhpList\Core\Domain\Subscription\Repository\SubscriberRepository;
use PhpList\Core\Domain\Subscription\Service\Provider\SubscriberProvider;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class SubscriberProviderTest extends TestCase
{
private SubscriberRepository|MockObject $subscriberRepository;
private SubscriberRepository&MockObject $subscriberRepository;
private SubscriberListRepository&MockObject $subscriberListRepository;
private SubscriberProvider $subscriberProvider;

protected function setUp(): void
{
$this->subscriberRepository = $this->createMock(SubscriberRepository::class);
$this->subscriberListRepository = $this->createMock(SubscriberListRepository::class);

$this->subscriberProvider = new SubscriberProvider($this->subscriberRepository);
$this->subscriberProvider = new SubscriberProvider(
$this->subscriberRepository,
$this->subscriberListRepository,
);
}

public function testGetSubscribersForMessageWithNoListsReturnsEmptyArray(): void
{
$message = $this->createMock(Message::class);
$message->method('getId')->willReturn(123);
$message->method('getSubscriberLists')->willReturn(new ArrayCollection());
$this->subscriberListRepository->method('getListsByMessage')->willReturn([]);

$this->subscriberRepository
->expects($this->never())
Expand All @@ -48,7 +54,9 @@ public function testGetSubscribersForMessageWithOneListButNoSubscribersReturnsEm

$subscriberList = $this->createMock(SubscriberList::class);
$subscriberList->method('getId')->willReturn(456);
$message->method('getSubscriberLists')->willReturn(new ArrayCollection([$subscriberList]));
$this->subscriberListRepository
->method('getListsByMessage')
->willReturn([$subscriberList]);

$this->subscriberRepository
->expects($this->once())
Expand All @@ -69,7 +77,9 @@ public function testGetSubscribersForMessageWithOneListAndSubscribersReturnsSubs

$subscriberList = $this->createMock(SubscriberList::class);
$subscriberList->method('getId')->willReturn(456);
$message->method('getSubscriberLists')->willReturn(new ArrayCollection([$subscriberList]));
$this->subscriberListRepository
->method('getListsByMessage')
->willReturn([$subscriberList]);

$subscriber1 = $this->createMock(Subscriber::class);
$subscriber1->method('getId')->willReturn(1);
Expand Down Expand Up @@ -99,7 +109,9 @@ public function testGetSubscribersForMessageWithMultipleListsReturnsUniqueSubscr
$subscriberList1->method('getId')->willReturn(456);
$subscriberList2 = $this->createMock(SubscriberList::class);
$subscriberList2->method('getId')->willReturn(789);
$message->method('getSubscriberLists')->willReturn(new ArrayCollection([$subscriberList1, $subscriberList2]));
$this->subscriberListRepository
->method('getListsByMessage')
->willReturn([$subscriberList1, $subscriberList2]);

$subscriber1 = $this->createMock(Subscriber::class);
$subscriber1->method('getId')->willReturn(1);
Expand Down
Loading