<?php
namespace CodersLab\Lms\UserInterface\Rest\Controller;
use CodersLab\Lms\SharedKernel\Application\Command\UpdateUserLanguage;
use CodersLab\Lms\SharedKernel\Application\IUserRepository;
use CodersLab\Lms\SharedKernel\Application\Response\LmsApiResponse;
use CodersLab\Lms\SharedKernel\Application\SecurityContext;
use CodersLab\Lms\SharedKernel\Domain\Bus\CommandBus;
use CodersLab\Lms\SharedKernel\Domain\IdentityAccess\UserLanguage;
use OpenApi\Annotations as OA;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Annotation\Route;
/**
* @OA\Tag(name="Language")
*/
final class LanguageController
{
public function __construct(
private SessionInterface $session,
private string $primaryLang,
private CommandBus $commandBus,
private SecurityContext $securityContext,
private IUserRepository $userRepository
) {
}
/**
* @Route("/language", name="api_get_lang", methods={"GET"})
* @OA\Response(
* response=200,
* description="Get current language"
* )
*/
public function getLang(): LmsApiResponse
{
return LmsApiResponse::create([
'lang' => $this->userRepository->getById(
(string)$this->securityContext->getLoggedUserId()
)->getUserLanguage()->getValue() ?? $this->primaryLang,
]);
}
/**
* @Route("/language/{lang}", name="api_set_lang", methods={"PUT"})
* @OA\Parameter(name="lang", in="path", @OA\Schema(type="string"))
* @OA\Response(
* response=200,
* description="set current language"
* )
*/
public function setLang(string $lang): LmsApiResponse
{
if ($this->securityContext->isUserLogged()) {
$this->commandBus->dispatch(new UpdateUserLanguage(
(string)$this->securityContext->getLoggedUserId(),
new UserLanguage($lang)
));
}
$this->session->set('_locale', $lang);
return LmsApiResponse::create(['lang' => $this->session->get('_locale')]);
}
}