Skip to content

Subscriber history endpoint #153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 27, 2025
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
8 changes: 8 additions & 0 deletions config/services/services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
services:
PhpList\RestBundle\Subscription\Service\SubscriberService:
autowire: true
autoconfigure: true

PhpList\RestBundle\Subscription\Service\SubscriberHistoryService:
autowire: true
autoconfigure: true
6 changes: 5 additions & 1 deletion src/Common/Service/Provider/PaginatedDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ public function getPaginatedList(
throw new RuntimeException('Repository not found');
}

$items = $repository->getFilteredAfterId($pagination->afterId, $pagination->limit, $filter);
$items = $repository->getFilteredAfterId(
lastId: $pagination->afterId,
limit: $pagination->limit,
filter: $filter,
);
$total = $repository->count();

$normalizedItems = array_map(
Expand Down
136 changes: 114 additions & 22 deletions src/Subscription/Controller/SubscriberController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@
use OpenApi\Attributes as OA;
use PhpList\Core\Domain\Identity\Model\PrivilegeFlag;
use PhpList\Core\Domain\Subscription\Model\Subscriber;
use PhpList\Core\Domain\Subscription\Service\Manager\SubscriberManager;
use PhpList\Core\Security\Authentication;
use PhpList\RestBundle\Common\Controller\BaseController;
use PhpList\RestBundle\Common\Validator\RequestValidator;
use PhpList\RestBundle\Subscription\Request\CreateSubscriberRequest;
use PhpList\RestBundle\Subscription\Request\UpdateSubscriberRequest;
use PhpList\RestBundle\Subscription\Serializer\SubscriberNormalizer;
use PhpList\RestBundle\Subscription\Service\SubscriberService;
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Attribute\Route;

/**
Expand All @@ -30,19 +28,16 @@
#[Route('/subscribers', name: 'subscriber_')]
class SubscriberController extends BaseController
{
private SubscriberManager $subscriberManager;
private SubscriberNormalizer $subscriberNormalizer;
private SubscriberService $subscriberService;

public function __construct(
Authentication $authentication,
RequestValidator $validator,
SubscriberManager $subscriberManager,
SubscriberNormalizer $subscriberNormalizer,
SubscriberService $subscriberService,
) {
parent::__construct($authentication, $validator);
$this->authentication = $authentication;
$this->subscriberManager = $subscriberManager;
$this->subscriberNormalizer = $subscriberNormalizer;
$this->subscriberService = $subscriberService;
}

#[Route('', name: 'create', methods: ['POST'])]
Expand Down Expand Up @@ -98,12 +93,9 @@ public function createSubscriber(Request $request): JsonResponse

/** @var CreateSubscriberRequest $subscriberRequest */
$subscriberRequest = $this->validator->validate($request, CreateSubscriberRequest::class);
$subscriber = $this->subscriberManager->createSubscriber($subscriberRequest->getDto());
$subscriberData = $this->subscriberService->createSubscriber($subscriberRequest);

return $this->json(
$this->subscriberNormalizer->normalize($subscriber, 'json'),
Response::HTTP_CREATED
);
return $this->json($subscriberData, Response::HTTP_CREATED);
}

#[Route('/{subscriberId}', name: 'update', requirements: ['subscriberId' => '\d+'], methods: ['PUT'])]
Expand Down Expand Up @@ -171,9 +163,9 @@ public function updateSubscriber(
}
/** @var UpdateSubscriberRequest $updateSubscriberRequest */
$updateSubscriberRequest = $this->validator->validate($request, UpdateSubscriberRequest::class);
$subscriber = $this->subscriberManager->updateSubscriber($updateSubscriberRequest->getDto());
$subscriberData = $this->subscriberService->updateSubscriber($updateSubscriberRequest);

return $this->json($this->subscriberNormalizer->normalize($subscriber, 'json'), Response::HTTP_OK);
return $this->json($subscriberData, Response::HTTP_OK);
}

#[Route('/{subscriberId}', name: 'get_one', requirements: ['subscriberId' => '\d+'], methods: ['GET'])]
Expand Down Expand Up @@ -221,11 +213,111 @@ public function getSubscriber(Request $request, int $subscriberId): JsonResponse
{
$this->requireAuthentication($request);

$subscriber = $this->subscriberManager->getSubscriber($subscriberId);
$subscriberData = $this->subscriberService->getSubscriber($subscriberId);

return $this->json($this->subscriberNormalizer->normalize($subscriber), Response::HTTP_OK);
return $this->json($subscriberData, Response::HTTP_OK);
}

#[Route('/{subscriberId}/history', name: 'history', requirements: ['subscriberId' => '\d+'], methods: ['GET'])]
#[OA\Get(
path: '/api/v2/subscribers/{subscriberId}/history',
description: '🚧 **Status: Beta** – This method is under development. Avoid using in production. ',
summary: 'Get subscriber event history',
tags: ['subscribers'],
parameters: [
new OA\Parameter(
name: 'php-auth-pw',
description: 'Session key obtained from login',
in: 'header',
required: true,
schema: new OA\Schema(type: 'string')
),
new OA\Parameter(
name: 'subscriberId',
description: 'Subscriber ID',
in: 'path',
required: true,
schema: new OA\Schema(type: 'integer')
),
new OA\Parameter(
name: 'after_id',
description: 'Page number (pagination)',
in: 'query',
required: false,
schema: new OA\Schema(type: 'integer', default: 1)
),
new OA\Parameter(
name: 'limit',
description: 'Max items per page',
in: 'query',
required: false,
schema: new OA\Schema(type: 'integer', default: 25)
),
new OA\Parameter(
name: 'ip',
description: 'Filter by IP address',
in: 'query',
required: false,
schema: new OA\Schema(type: 'string')
),
new OA\Parameter(
name: 'date_from',
description: 'Filter by date (format: Y-m-d)',
in: 'query',
required: false,
schema: new OA\Schema(type: 'string', format: 'date')
),
new OA\Parameter(
name: 'summery',
description: 'Filter by summary text',
in: 'query',
required: false,
schema: new OA\Schema(type: 'string')
)
],
responses: [
new OA\Response(
response: 200,
description: 'Paginated list of subscriber events',
content: new OA\JsonContent(
properties: [
new OA\Property(
property: 'items',
type: 'array',
items: new OA\Items(ref: '#/components/schemas/SubscriberHistory')
),
new OA\Property(property: 'pagination', ref: '#/components/schemas/CursorPagination')
],
type: 'object'
)
),
new OA\Response(
response: 403,
description: 'Unauthorized',
content: new OA\JsonContent(ref: '#/components/schemas/UnauthorizedResponse')
),
new OA\Response(
response: 404,
description: 'Not Found',
content: new OA\JsonContent(ref: '#/components/schemas/NotFoundErrorResponse')
)
]
)]
public function getSubscriberHistory(
Request $request,
#[MapEntity(mapping: ['subscriberId' => 'id'])] ?Subscriber $subscriber = null,
): JsonResponse {
$this->requireAuthentication($request);

$historyData = $this->subscriberService->getSubscriberHistory($request, $subscriber);

return $this->json(
data: $historyData,
status: Response::HTTP_OK,
);
}


#[Route('/{subscriberId}', name: 'delete', requirements: ['subscriberId' => '\d+'], methods: ['DELETE'])]
#[OA\Delete(
path: '/api/v2/subscribers/{subscriberId}',
Expand Down Expand Up @@ -278,7 +370,7 @@ public function deleteSubscriber(
if (!$subscriber) {
throw $this->createNotFoundException('Subscriber not found.');
}
$this->subscriberManager->deleteSubscriber($subscriber);
$this->subscriberService->deleteSubscriber($subscriber);

return $this->json(null, Response::HTTP_NO_CONTENT);
}
Expand Down Expand Up @@ -323,9 +415,9 @@ public function setSubscriberAsConfirmed(Request $request): Response
return new Response('<h1>Missing confirmation code.</h1>', 400);
}

try {
$this->subscriberManager->markAsConfirmedByUniqueId($uniqueId);
} catch (NotFoundHttpException) {
$subscriber = $this->subscriberService->confirmSubscriber($uniqueId);

if (!$subscriber) {
return new Response('<h1>Subscriber isn\'t found or already confirmed.</h1>', 404);
}

Expand Down
17 changes: 17 additions & 0 deletions src/Subscription/OpenApi/SwaggerSchemasResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,23 @@
new OA\Property(property: 'value', type: 'string', example: 'United States'),
],
)]
#[OA\Schema(
schema: 'SubscriberHistory',
properties: [
new OA\Property(property: 'id', type: 'integer', example: 1),
new OA\Property(property: 'ip', type: 'string', example: '127.0.0.1'),
new OA\Property(
property: 'created_at',
type: 'string',
format: 'date-time',
example: '2022-12-01T10:00:00Z'
),
new OA\Property(property: 'summery', type: 'string', example: 'Added by admin'),
new OA\Property(property: 'detail', type: 'string', example: 'Added with add-email on test'),
new OA\Property(property: 'system_info', type: 'string', example: 'HTTP_USER_AGENT = Mozilla/5.0'),
],
type: 'object'
)]
class SwaggerSchemasResponse
{
}
38 changes: 38 additions & 0 deletions src/Subscription/Serializer/SubscriberHistoryNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace PhpList\RestBundle\Subscription\Serializer;

use PhpList\Core\Domain\Subscription\Model\SubscriberHistory;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

class SubscriberHistoryNormalizer implements NormalizerInterface
{
/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function normalize($object, string $format = null, array $context = []): array
{
if (!$object instanceof SubscriberHistory) {
return [];
}

return [
'id' => $object->getId(),
'ip' => $object->getIp(),
'created_at' => $object->getCreatedAt()->format('Y-m-d\TH:i:sP'),
'summery' => $object->getSummary(),
'detail' => $object->getDetail(),
'system_info' => $object->getSystemInfo(),
];
}

/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function supportsNormalization($data, string $format = null): bool
{
return $data instanceof SubscriberHistory;
}
}
53 changes: 53 additions & 0 deletions src/Subscription/Service/SubscriberHistoryService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace PhpList\RestBundle\Subscription\Service;

use DateTimeImmutable;
use Exception;
use PhpList\Core\Domain\Subscription\Model\Filter\SubscriberHistoryFilter;
use PhpList\Core\Domain\Subscription\Model\Subscriber;
use PhpList\Core\Domain\Subscription\Model\SubscriberHistory;
use PhpList\RestBundle\Common\Service\Provider\PaginatedDataProvider;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Validator\Exception\ValidatorException;

class SubscriberHistoryService
{
public function __construct(
private readonly PaginatedDataProvider $paginatedDataProvider,
private readonly NormalizerInterface $serializer,
) {
}

public function getSubscriberHistory(Request $request, ?Subscriber $subscriber): array
{
if (!$subscriber) {
throw new NotFoundHttpException('Subscriber not found.');
}

try {
$dateFrom = $request->query->get('date_from');
$dateFromFormated = $dateFrom ? new DateTimeImmutable($dateFrom) : null;
} catch (Exception $e) {
throw new ValidatorException('Invalid date format. Use format: Y-m-d');
}

$filter = new SubscriberHistoryFilter(
subscriber: $subscriber,
ip: $request->query->get('ip'),
dateFrom: $dateFromFormated,
summery: $request->query->get('summery'),
);

return $this->paginatedDataProvider->getPaginatedList(
request: $request,
normalizer: $this->serializer,
className: SubscriberHistory::class,
filter: $filter
);
}
}
Loading
Loading