Skip to content

Commit 85ab9ce

Browse files
committed
ISSUE-337: symfony 6.4
1 parent b687537 commit 85ab9ce

File tree

7 files changed

+180
-142
lines changed

7 files changed

+180
-142
lines changed

composer.json

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
"nette/caching": "^2.5.0|^3.0.0",
3838
"nikic/php-parser": "^4.19.1",
3939
"phpmd/phpmd": "^2.6.0",
40-
"composer/composer": "^1.6.0",
4140
"doctrine/instantiator": "^1.0.5"
4241
},
4342
"autoload": {
@@ -100,40 +99,8 @@
10099
"routes": {
101100
"rest-api": {
102101
"resource": "@PhpListRestBundle/Controller/",
103-
"type": "rest",
104-
"prefix": "/"
105-
}
106-
},
107-
"configuration": {
108-
"fos_rest": {
109-
"format_listener": {
110-
"enabled": true,
111-
"rules": [
112-
{
113-
"path": "^/",
114-
"fallback_format": "json"
115-
},
116-
{
117-
"path": "^/",
118-
"fallback_format": "html"
119-
}
120-
]
121-
},
122-
"view": {
123-
"view_response_listener": {
124-
"enabled": false
125-
}
126-
},
127-
"exception": {
128-
"enabled": true,
129-
"messages": {
130-
"Symfony\\Component\\HttpKernel\\Exception\\BadRequestHttpException": true
131-
}
132-
},
133-
"service": {
134-
"view_handler": "my.secure_view_handler",
135-
"serializer": "fos_rest.serializer.symfony"
136-
}
102+
"type": "attribute",
103+
"prefix": "/api/v2"
137104
}
138105
}
139106
}

config/services.yml

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,37 @@
11
services:
2+
Psr\Container\ContainerInterface:
3+
alias: 'service_container'
4+
25
PhpList\RestBundle\Controller\:
36
resource: '../src/Controller'
47
public: true
58
autowire: true
69
tags: ['controller.service_arguments']
710

11+
# Symfony\Component\Serializer\SerializerInterface:
12+
# autowire: true
13+
# autoconfigure: true
14+
815
my.secure_handler:
9-
class: \PhpList\RestBundle\ViewHandler\SecuredViewHandler
16+
class: \PhpList\RestBundle\ViewHandler\SecuredViewHandler
1017

1118
my.secure_view_handler:
12-
parent: fos_rest.view_handler.default
13-
calls:
14-
- ['registerHandler', [ 'json', ['@my.secure_handler', 'createResponse'] ] ]
19+
parent: fos_rest.view_handler.default
20+
calls:
21+
- ['registerHandler', [ 'json', ['@my.secure_handler', 'createResponse'] ] ]
22+
23+
PhpList\Core\Security\Authentication:
24+
autowire: true
25+
autoconfigure: true
26+
27+
PhpList\Core\Domain\Repository\Messaging\SubscriberListRepository:
28+
autowire: true
29+
autoconfigure: true
30+
31+
PhpList\RestBundle\EventListener\ExceptionListener:
32+
tags:
33+
- { name: kernel.event_listener, event: kernel.exception }
34+
35+
PhpList\RestBundle\EventListener\ResponseListener:
36+
tags:
37+
- { name: kernel.event_listener, event: kernel.response }

src/Controller/ListController.php

Lines changed: 48 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@
44

55
namespace PhpList\RestBundle\Controller;
66

7-
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
8-
use FOS\RestBundle\View\View;
97
use PhpList\Core\Domain\Model\Messaging\SubscriberList;
8+
use PhpList\Core\Domain\Repository\Subscription\SubscriberRepository;
9+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1010
use PhpList\Core\Domain\Repository\Messaging\SubscriberListRepository;
1111
use PhpList\Core\Security\Authentication;
1212
use PhpList\RestBundle\Controller\Traits\AuthenticationTrait;
13+
use Symfony\Component\HttpFoundation\JsonResponse;
1314
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\HttpFoundation\Response;
16+
use Symfony\Component\Routing\Attribute\Route;
17+
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
18+
use Symfony\Component\Serializer\SerializerInterface;
1419

1520
/**
1621
* This controller provides REST API access to subscriber lists.
@@ -22,94 +27,81 @@ class ListController extends AbstractController
2227
{
2328
use AuthenticationTrait;
2429

25-
/**
26-
* @var SubscriberListRepository
27-
*/
2830
private SubscriberListRepository $subscriberListRepository;
31+
private SubscriberRepository $subscriberRepository;
32+
private SerializerInterface $serializer;
2933

3034
/**
3135
* @param Authentication $authentication
3236
* @param SubscriberListRepository $repository
37+
* @param SubscriberRepository $subscriberRepository
38+
* @param SerializerInterface $serializer
3339
*/
34-
public function __construct(Authentication $authentication, SubscriberListRepository $repository)
35-
{
40+
public function __construct(
41+
Authentication $authentication,
42+
SubscriberListRepository $repository,
43+
SubscriberRepository $subscriberRepository,
44+
SerializerInterface $serializer
45+
) {
3646
$this->authentication = $authentication;
3747
$this->subscriberListRepository = $repository;
48+
$this->subscriberRepository = $subscriberRepository;
49+
$this->serializer = $serializer;
3850
}
3951

40-
/**
41-
* Gets a list of all subscriber lists.
42-
*
43-
* @param Request $request
44-
*
45-
* @return View
46-
*/
47-
public function cgetAction(Request $request): View
52+
#[Route('/lists', name: 'get_lists', methods: ['GET'])]
53+
public function getLists(Request $request): JsonResponse
4854
{
4955
$this->requireAuthentication($request);
56+
$data = $this->subscriberListRepository->findAll();
57+
$json = $this->serializer->serialize($data, 'json', [
58+
AbstractNormalizer::GROUPS => 'SubscriberList',
59+
]);
5060

51-
return View::create()->setData($this->subscriberListRepository->findAll());
61+
return new JsonResponse($json, Response::HTTP_OK, [], true);
5262
}
5363

54-
/**
55-
* Gets a subscriber list.
56-
*
57-
* @param Request $request
58-
* @param SubscriberList $list
59-
*
60-
* @return View
61-
*/
62-
public function getAction(Request $request, SubscriberList $list): View
64+
#[Route('/lists/{id}', name: 'get_list', methods: ['GET'])]
65+
public function getList(Request $request, SubscriberList $list): JsonResponse
6366
{
6467
$this->requireAuthentication($request);
68+
$json = $this->serializer->serialize($list, 'json', [
69+
AbstractNormalizer::GROUPS => 'SubscriberList',
70+
]);
6571

66-
return View::create()->setData($list);
72+
return new JsonResponse($json, Response::HTTP_OK, [], true);
6773
}
6874

69-
/**
70-
* Deletes a subscriber list.
71-
*
72-
* @param Request $request
73-
* @param SubscriberList $list
74-
*
75-
* @return View
76-
*/
77-
public function deleteAction(Request $request, SubscriberList $list): View
75+
#[Route('/lists/{id}', name: 'delete_list', methods: ['DELETE'])]
76+
public function deleteList(Request $request, SubscriberList $list): JsonResponse
7877
{
7978
$this->requireAuthentication($request);
8079

8180
$this->subscriberListRepository->remove($list);
8281

83-
return View::create();
82+
return new JsonResponse(null, Response::HTTP_OK, [], true);
8483
}
8584

86-
/**
87-
* Gets a list of all subscribers (members) of a subscriber list.
88-
*
89-
* @param Request $request
90-
* @param SubscriberList $list
91-
*
92-
* @return View
93-
*/
94-
public function getMembersAction(Request $request, SubscriberList $list): View
85+
#[Route('/lists/{id}/members', name: 'get_subscriber_from_list', methods: ['GET'])]
86+
public function getListMembers(Request $request, SubscriberList $list): JsonResponse
9587
{
9688
$this->requireAuthentication($request);
9789

98-
return View::create()->setData($list->getSubscribers());
90+
$subscribers = $this->subscriberRepository->findSubscribersBySubscribedList($list->getId());
91+
92+
$json = $this->serializer->serialize($subscribers, 'json', [
93+
AbstractNormalizer::GROUPS => 'SubscriberListMembers',
94+
]);
95+
96+
return new JsonResponse($json, Response::HTTP_OK, [], true);
9997
}
10098

101-
/**
102-
* Gets the total number of subscribers of a list.
103-
*
104-
* @param Request $request
105-
* @param SubscriberList $list
106-
*
107-
* @return View
108-
*/
109-
public function getSubscribersCountAction(Request $request, SubscriberList $list): View
99+
#[Route('/lists/{id}/count', name: 'get_subscribers_count_from_list', methods: ['GET'])]
100+
public function getSubscribersCount(Request $request, SubscriberList $list): JsonResponse
110101
{
111102
$this->requireAuthentication($request);
103+
$json = $this->serializer->serialize(count($list->getSubscribers()), 'json');
112104

113-
return View::create()->setData(count($list->getSubscribers()));
105+
return new JsonResponse($json, Response::HTTP_OK, [], true);
114106
}
115107
}

src/Controller/SessionController.php

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,91 +5,85 @@
55
namespace PhpList\RestBundle\Controller;
66

77
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
8-
use FOS\RestBundle\View\View;
98
use PhpList\Core\Domain\Model\Identity\Administrator;
109
use PhpList\Core\Domain\Model\Identity\AdministratorToken;
1110
use PhpList\Core\Domain\Repository\Identity\AdministratorRepository;
1211
use PhpList\Core\Domain\Repository\Identity\AdministratorTokenRepository;
1312
use PhpList\Core\Security\Authentication;
1413
use PhpList\RestBundle\Controller\Traits\AuthenticationTrait;
14+
use Symfony\Component\HttpFoundation\JsonResponse;
1515
use Symfony\Component\HttpFoundation\Request;
1616
use Symfony\Component\HttpFoundation\Response;
1717
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
1818
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
1919
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
20+
use Symfony\Component\Routing\Attribute\Route;
21+
use Symfony\Component\Serializer\SerializerInterface;
2022

2123
/**
2224
* This controller provides methods to create and destroy REST API sessions.
2325
*
2426
* @author Oliver Klee <oliver@phplist.com>
27+
* @author Tatevik Grigoryan <tatevik@phplist.com>
2528
*/
2629
class SessionController extends AbstractController
2730
{
2831
use AuthenticationTrait;
2932

30-
/**
31-
* @var AdministratorRepository
32-
*/
3333
private AdministratorRepository $administratorRepository;
34-
35-
/**
36-
* @var AdministratorTokenRepository
37-
*/
3834
private AdministratorTokenRepository $tokenRepository;
35+
private SerializerInterface $serializer;
3936

4037
/**
4138
* @param Authentication $authentication
4239
* @param AdministratorRepository $administratorRepository
4340
* @param AdministratorTokenRepository $tokenRepository
41+
* @param SerializerInterface $serializer
4442
*/
4543
public function __construct(
4644
Authentication $authentication,
4745
AdministratorRepository $administratorRepository,
48-
AdministratorTokenRepository $tokenRepository
46+
AdministratorTokenRepository $tokenRepository,
47+
SerializerInterface $serializer
4948
) {
5049
$this->authentication = $authentication;
5150
$this->administratorRepository = $administratorRepository;
5251
$this->tokenRepository = $tokenRepository;
52+
$this->serializer = $serializer;
5353
}
5454

5555
/**
5656
* Creates a new session (if the provided credentials are valid).
5757
*
58-
* @param Request $request
59-
*
60-
* @return View
61-
*
6258
* @throws UnauthorizedHttpException
6359
*/
64-
public function postAction(Request $request): View
60+
#[Route('/sessions', name: 'create_session', methods: ['POST'])]
61+
public function createSession(Request $request): JsonResponse
6562
{
6663
$this->validateCreateRequest($request);
6764
$administrator = $this->administratorRepository->findOneByLoginCredentials(
68-
$request->get('login_name'),
69-
$request->get('password')
65+
$request->getPayload()->get('login_name'),
66+
$request->getPayload()->get('password')
7067
);
7168
if ($administrator === null) {
7269
throw new UnauthorizedHttpException('', 'Not authorized', null, 1500567098);
7370
}
7471

7572
$token = $this->createAndPersistToken($administrator);
73+
$json = $this->serializer->serialize($token, 'json');
7674

77-
return View::create()->setStatusCode(Response::HTTP_CREATED)->setData($token);
75+
return new JsonResponse($json, Response::HTTP_OK, [], true);
7876
}
7977

8078
/**
8179
* Deletes a session.
8280
*
8381
* This action may only be called for sessions that are owned by the authenticated administrator.
8482
*
85-
* @param Request $request
86-
* @param AdministratorToken $token
87-
*
88-
* @return View
89-
*
9083
* @throws AccessDeniedHttpException
9184
*/
92-
public function deleteAction(Request $request, AdministratorToken $token): View
85+
#[Route('/sessions/{id}', name: 'delete_session', methods: ['DELETE'])]
86+
public function deleteAction(Request $request, AdministratorToken $token): JsonResponse
9387
{
9488
$administrator = $this->requireAuthentication($request);
9589
if ($token->getAdministrator() !== $administrator) {
@@ -98,7 +92,7 @@ public function deleteAction(Request $request, AdministratorToken $token): View
9892

9993
$this->tokenRepository->remove($token);
10094

101-
return View::create();
95+
return new JsonResponse(null, Response::HTTP_OK, [], true);
10296
}
10397

10498
/**
@@ -115,7 +109,7 @@ private function validateCreateRequest(Request $request): void
115109
if ($request->getContent() === '') {
116110
throw new BadRequestHttpException('Empty JSON data', null, 1500559729);
117111
}
118-
if (empty($request->get('login_name')) || empty($request->get('password'))) {
112+
if (empty($request->getPayload()->get('login_name')) || empty($request->getPayload()->get('password'))) {
119113
throw new BadRequestHttpException('Incomplete credentials', null, 1500562647);
120114
}
121115
}

0 commit comments

Comments
 (0)