*/ final readonly class CreateStudentProcessor implements ProcessorInterface { public function __construct( private CreateStudentHandler $handler, private ClassRepository $classRepository, private TenantContext $tenantContext, private MessageBusInterface $eventBus, private AuthorizationCheckerInterface $authorizationChecker, private CurrentAcademicYearResolver $academicYearResolver, private Security $security, ) { } /** * @param StudentResource $data */ #[Override] public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): StudentResource { if (!$this->authorizationChecker->isGranted(StudentVoter::CREATE)) { throw new AccessDeniedHttpException('Vous n\'êtes pas autorisé à créer un élève.'); } if (!$this->tenantContext->hasTenant()) { throw new UnauthorizedHttpException('Bearer', 'Tenant non défini.'); } $tenantId = (string) $this->tenantContext->getCurrentTenantId(); $tenantConfig = $this->tenantContext->getCurrentTenantConfig(); $academicYearId = $this->academicYearResolver->resolve('current') ?? throw new BadRequestHttpException('Impossible de résoudre l\'année scolaire courante.'); try { $adminUserId = null; $securityUser = $this->security->getUser(); if ($securityUser instanceof SecurityUser) { $adminUserId = $securityUser->userId(); } $command = new CreateStudentCommand( tenantId: $tenantId, schoolName: $tenantConfig->subdomain, firstName: $data->firstName ?? '', lastName: $data->lastName ?? '', classId: $data->classId ?? '', academicYearId: $academicYearId, email: $data->email, dateNaissance: $data->dateNaissance, studentNumber: $data->studentNumber, parentalConsent: $data->parentalConsent, consentRecordedBy: $adminUserId, ); $user = ($this->handler)($command); // Dispatch domain events foreach ($user->pullDomainEvents() as $event) { $this->eventBus->dispatch($event); } // Build the response from the created user and class info $class = $this->classRepository->get(ClassId::fromString($data->classId ?? '')); $resource = new StudentResource(); $resource->id = (string) $user->id; $resource->firstName = $user->firstName; $resource->lastName = $user->lastName; $resource->email = $user->email !== null ? (string) $user->email : null; $resource->statut = $user->statut->value; $resource->classId = $data->classId; $resource->className = (string) $class->name; $resource->classLevel = $class->level?->value; $resource->studentNumber = $user->studentNumber; $resource->dateNaissance = $user->dateNaissance?->format('Y-m-d'); return $resource; } catch (EmailDejaUtiliseeException $e) { throw new ConflictHttpException($e->getMessage()); } catch (ClasseNotFoundException $e) { throw new NotFoundHttpException($e->getMessage()); } } }