Skip to content

Commit 0b619ec

Browse files
committed
ISSUE-345: create subscriber list endpoint
1 parent 624c11a commit 0b619ec

File tree

10 files changed

+193
-10
lines changed

10 files changed

+193
-10
lines changed

config/services.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,3 @@ services:
2121
PhpList\Core\Security\Authentication:
2222
autowire: true
2323
autoconfigure: true
24-
25-
PhpList\Core\Domain\Repository\Messaging\SubscriberListRepository:
26-
autowire: true
27-
autoconfigure: true

config/services/managers.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ services:
1111
PhpList\RestBundle\Service\Manager\SessionManager:
1212
autowire: true
1313
autoconfigure: true
14+
15+
PhpList\RestBundle\Service\Manager\SubscriberListManager:
16+
autowire: true
17+
autoconfigure: true

config/services/normalizers.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ services:
1818
PhpList\RestBundle\Serializer\AdministratorTokenNormalizer:
1919
tags: [ 'serializer.normalizer' ]
2020
autowire: true
21+
22+
PhpList\RestBundle\Serializer\SubscriberListNormalizer:
23+
tags: [ 'serializer.normalizer' ]
24+
autowire: true

src/Controller/ListController.php

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44

55
namespace PhpList\RestBundle\Controller;
66

7-
use PhpList\Core\Domain\Model\Messaging\SubscriberList;
7+
use PhpList\Core\Domain\Model\Subscription\SubscriberList;
88
use PhpList\Core\Domain\Repository\Subscription\SubscriberRepository;
9+
use PhpList\RestBundle\Entity\CreateSubscriberListRequest;
10+
use PhpList\RestBundle\Serializer\SubscriberListNormalizer;
11+
use PhpList\RestBundle\Service\Manager\SubscriberListManager;
12+
use PhpList\RestBundle\Validator\RequestValidator;
913
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
1014
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
11-
use PhpList\Core\Domain\Repository\Messaging\SubscriberListRepository;
15+
use PhpList\Core\Domain\Repository\Subscription\SubscriberListRepository;
1216
use PhpList\Core\Security\Authentication;
1317
use PhpList\RestBundle\Controller\Traits\AuthenticationTrait;
1418
use Symfony\Component\HttpFoundation\JsonResponse;
@@ -34,17 +38,21 @@ class ListController extends AbstractController
3438
private SubscriberListRepository $subscriberListRepository;
3539
private SubscriberRepository $subscriberRepository;
3640
private SerializerInterface $serializer;
41+
private SubscriberListManager $subscriberListManager;
42+
private RequestValidator $validator;
3743

3844
public function __construct(
3945
Authentication $authentication,
4046
SubscriberListRepository $repository,
4147
SubscriberRepository $subscriberRepository,
42-
SerializerInterface $serializer
48+
SerializerInterface $serializer,
49+
RequestValidator $validator
4350
) {
4451
$this->authentication = $authentication;
4552
$this->subscriberListRepository = $repository;
4653
$this->subscriberRepository = $subscriberRepository;
4754
$this->serializer = $serializer;
55+
$this->validator = $validator;
4856
}
4957

5058
#[Route('', name: 'get_lists', methods: ['GET'])]
@@ -340,4 +348,71 @@ public function getSubscribersCount(
340348

341349
return new JsonResponse($json, Response::HTTP_OK, [], true);
342350
}
351+
352+
#[Route('', name: 'create_list', methods: ['POST'])]
353+
#[OA\Post(
354+
path: '/lists',
355+
description: 'Returns created list.',
356+
summary: 'Create a subscriber list.',
357+
requestBody: new OA\RequestBody(
358+
description: 'Pass parameters to create a new subscriber list.',
359+
required: true,
360+
content: new OA\JsonContent(
361+
required: ['name'],
362+
properties: [
363+
new OA\Property(property: 'name', type: 'string', format: 'string', example: 'News'),
364+
new OA\Property(property: 'description', type: 'string', example: 'News (and some fun stuff)'),
365+
new OA\Property(property: 'list_position', type: 'number', example: 12),
366+
new OA\Property(property: 'public', type: 'boolean', example: true),
367+
new OA\Property(property: 'owner', type: 'number', example: 12),
368+
]
369+
)
370+
),
371+
tags: ['lists'],
372+
parameters: [
373+
new OA\Parameter(
374+
name: 'session',
375+
description: 'Session ID obtained from authentication',
376+
in: 'header',
377+
required: true,
378+
schema: new OA\Schema(
379+
type: 'string'
380+
)
381+
)
382+
],
383+
responses: [
384+
new OA\Response(
385+
response: 201,
386+
description: 'Success',
387+
content: new OA\JsonContent(
388+
type: 'object',
389+
example: [
390+
'name' => 'News',
391+
'description' => 'News (and some fun stuff)',
392+
'creation_date' => '2016-06-22T15:01:17+00:00',
393+
'list_position' => 12,
394+
'subject_prefix' => 'phpList',
395+
'public' => true,
396+
'category' => 'news',
397+
'id' => 1
398+
]
399+
)
400+
),
401+
new OA\Response(
402+
response: 403,
403+
description: 'Failure',
404+
content: new OA\JsonContent(ref: '#/components/schemas/UnauthorizedResponse')
405+
)
406+
]
407+
)]
408+
public function createList(Request $request, SubscriberListNormalizer $normalizer): JsonResponse
409+
{
410+
$this->requireAuthentication($request);
411+
412+
/** @var CreateSubscriberListRequest $subscriberListRequest */
413+
$subscriberListRequest = $this->validator->validate($request, CreateSubscriberListRequest::class);
414+
$data = $this->subscriberListManager->createSubscriberList($subscriberListRequest);
415+
416+
return new JsonResponse($normalizer->normalize($data), Response::HTTP_CREATED, [], true);
417+
}
343418
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\RestBundle\Entity;
6+
7+
use Symfony\Component\Validator\Constraints as Assert;
8+
9+
class CreateSubscriberListRequest implements RequestInterface
10+
{
11+
#[Assert\NotBlank]
12+
#[Assert\NotNull]
13+
public string $name;
14+
15+
#[Assert\NotBlank]
16+
public bool $public;
17+
18+
#[Assert\NotBlank]
19+
public int $listPosition;
20+
21+
#[Assert\NotBlank]
22+
public int $owner;
23+
24+
#[Assert\NotBlank]
25+
#[Assert\NotNull]
26+
public string $description;
27+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\RestBundle\Serializer;
6+
7+
use PhpList\Core\Domain\Model\Subscription\SubscriberList;
8+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
9+
10+
class SubscriberListNormalizer implements NormalizerInterface
11+
{
12+
/**
13+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
14+
*/
15+
public function normalize($object, string $format = null, array $context = []): array
16+
{
17+
if (!$object instanceof SubscriberList) {
18+
return [];
19+
}
20+
21+
return [
22+
'id' => $object->getId(),
23+
'name' => $object->getName(),
24+
'creation_date' => $object->getCreationDate()->format('Y-m-d\TH:i:sP'),
25+
'description' => $object->getDescription(),
26+
'list_position' => $object->getListPosition(),
27+
'subject_prefix' => $object->getSubjectPrefix(),
28+
'public' => $object->isPublic(),
29+
'category' => $object->getCategory(),
30+
];
31+
}
32+
33+
/**
34+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
35+
*/
36+
public function supportsNormalization($data, string $format = null): bool
37+
{
38+
return $data instanceof SubscriberList;
39+
}
40+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\RestBundle\Service\Manager;
6+
7+
use Doctrine\ORM\EntityManagerInterface;
8+
use PhpList\Core\Domain\Model\Subscription\SubscriberList;
9+
use PhpList\Core\Domain\Repository\Subscription\SubscriberListRepository;
10+
use PhpList\RestBundle\Entity\CreateSubscriberListRequest;
11+
12+
class SubscriberListManager
13+
{
14+
private SubscriberListRepository $subscriberListRepository;
15+
private EntityManagerInterface $entityManager;
16+
17+
public function __construct(
18+
SubscriberListRepository $subscriberListRepository,
19+
EntityManagerInterface $entityManager
20+
) {
21+
$this->subscriberListRepository = $subscriberListRepository;
22+
$this->entityManager = $entityManager;
23+
}
24+
25+
public function createSubscriberList(CreateSubscriberListRequest $subscriberListRequest): SubscriberList
26+
{
27+
$subscriberList = (new SubscriberList())
28+
->setName($subscriberListRequest->name)
29+
->setDescription($subscriberListRequest->description)
30+
->setListPosition($subscriberListRequest->listPosition)
31+
->setPublic($subscriberListRequest->public);
32+
33+
$this->subscriberListRepository->save($subscriberList);
34+
35+
return $subscriberList;
36+
}
37+
}

tests/Integration/Controller/Fixtures/SubscriberListFixture.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Doctrine\Bundle\FixturesBundle\Fixture;
99
use Doctrine\Persistence\ObjectManager;
1010
use PhpList\Core\Domain\Model\Identity\Administrator;
11-
use PhpList\Core\Domain\Model\Messaging\SubscriberList;
11+
use PhpList\Core\Domain\Model\Subscription\SubscriberList;
1212
use PhpList\Core\TestingSupport\Traits\ModelTestTrait;
1313
use RuntimeException;
1414

tests/Integration/Controller/Fixtures/SubscriptionFixture.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use DateTime;
88
use Doctrine\Bundle\FixturesBundle\Fixture;
99
use Doctrine\Persistence\ObjectManager;
10-
use PhpList\Core\Domain\Model\Messaging\SubscriberList;
10+
use PhpList\Core\Domain\Model\Subscription\SubscriberList;
1111
use PhpList\Core\Domain\Model\Subscription\Subscriber;
1212
use PhpList\Core\Domain\Model\Subscription\Subscription;
1313
use PhpList\Core\TestingSupport\Traits\ModelTestTrait;

tests/Integration/Controller/ListControllerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace PhpList\RestBundle\Tests\Integration\Controller;
66

7-
use PhpList\Core\Domain\Repository\Messaging\SubscriberListRepository;
7+
use PhpList\Core\Domain\Repository\Subscription\SubscriberListRepository;
88
use PhpList\RestBundle\Controller\ListController;
99
use PhpList\RestBundle\Tests\Integration\Controller\Fixtures\AdministratorFixture;
1010
use PhpList\RestBundle\Tests\Integration\Controller\Fixtures\AdministratorTokenFixture;

0 commit comments

Comments
 (0)