zend-blog-3-backend
159 строк · 4.6 Кб
1<?php
2
3namespace App\Controller\API;
4
5use App\API\Transformers\UserTransformer;
6use App\Controller\BaseController;
7use App\DTO\ExternalUserDTO;
8use App\Entity\User;
9use App\Event\UserEvent;
10use App\Event\UserExtraEvent;
11use App\Form\UserFormType;
12use App\Repository\UserRepository;
13use App\Service\UserManager;
14use Symfony\Component\EventDispatcher\EventDispatcherInterface;
15use Symfony\Component\HttpFoundation\JsonResponse;
16use Symfony\Component\HttpFoundation\Request;
17use Symfony\Component\HttpFoundation\Response;
18use Symfony\Component\Routing\Annotation\Route;
19use Symfony\Component\Validator\Validator\ValidatorInterface;
20
21/**
22* @Route("/api/users")
23*/
24class UserController extends BaseController
25{
26protected array $errorsPathMap = [
27'children[user].children[username].data' => 'username',
28'children[user].children[displayName].data' => 'displayName',
29'children[user].children[email].data' => 'email',
30'username' => 'username',
31'email' => 'email',
32];
33
34/**
35* @Route("", methods={"GET"})
36*
37* @param Request $request
38* @param UserRepository $repository
39*
40* @return JsonResponse
41*/
42public function findAllAction(Request $request, UserRepository $repository): JsonResponse
43{
44$pagination = $this->paginate(
45$repository->getListQuery(),
46$request->query->get('page', 1)
47);
48
49$result = $this->getDataConverter()
50->getUserArray($pagination);
51
52return new JsonResponse($result);
53}
54
55/**
56* @Route("/{id}", requirements={"id": "\d+"}, methods={"GET"})
57*
58* @param User $entity
59*
60* @return JsonResponse
61*/
62public function findAction(User $entity): JsonResponse
63{
64$result = $this->getDataConverter()
65->getUser($entity);
66
67return new JsonResponse($result);
68}
69
70/**
71* @Route("/{id}", requirements={"id": "\d+"}, methods={"PUT"})
72*
73* @param ValidatorInterface $validator
74* @param EventDispatcherInterface $dispatcher
75* @param Request $request
76* @param User $entity
77*
78* @throws \Psr\Container\ContainerExceptionInterface
79* @throws \Psr\Container\NotFoundExceptionInterface
80*
81* @return JsonResponse
82*/
83public function updateAction(
84ValidatorInterface $validator,
85EventDispatcherInterface $dispatcher,
86Request $request,
87User $entity
88): JsonResponse {
89$form = $this->createObjectForm('user', UserFormType::class, true);
90$form->handleRequest($request);
91
92[$formData, $errors] = $this->handleForm($form);
93if ($errors) {
94return new JsonResponse($errors, Response::HTTP_UNPROCESSABLE_ENTITY);
95}
96
97UserTransformer::reverseTransform($entity, $formData['user']);
98
99$errors = $this->validate($validator, $entity);
100if (count($errors) > 0) {
101return new JsonResponse($errors, Response::HTTP_UNPROCESSABLE_ENTITY);
102}
103
104$this->em->persist($entity);
105$this->em->flush();
106
107$dispatcher->dispatch(new UserEvent($entity));
108
109return new JsonResponse($this->getDataConverter()->getUser($entity));
110}
111
112/**
113* @Route("/external", methods={"POST"})
114*
115* @param ExternalUserDTO $userDTO
116* @param UserManager $userManager
117* @param ValidatorInterface $validator
118* @param EventDispatcherInterface $dispatcher
119* @param Request $request
120*
121* @return JsonResponse
122*/
123public function createExternalAction(
124ExternalUserDTO $userDTO,
125UserManager $userManager,
126ValidatorInterface $validator,
127EventDispatcherInterface $dispatcher,
128Request $request
129): JsonResponse {
130[$user, $foundInfo] = $userManager->findByExternalDTO($userDTO);
131if (!$user) {
132$user = $userManager->createFromExternalDTO($userDTO);
133
134$errors = $this->validate($validator, $user);
135if (count($errors) > 0) {
136return new JsonResponse($errors, Response::HTTP_UNPROCESSABLE_ENTITY);
137}
138
139$this->em->persist($user);
140$this->em->flush();
141}
142
143if (!$foundInfo) {
144$userInfo = $userManager->saveUserExtraInfo(
145$userDTO,
146$user,
147$request->request->get('ipAddress'),
148$request->request->get('userAgent')
149);
150
151$dispatcher->dispatch(new UserExtraEvent($userInfo));
152}
153
154return new JsonResponse(
155$this->getDataConverter()->getUser($user),
156$foundInfo ? Response::HTTP_OK : Response::HTTP_CREATED
157);
158}
159}
160