Skip to content

Commit

Permalink
refactor: simplified code
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Apr 26, 2024
1 parent d2b1efb commit e116a77
Showing 1 changed file with 28 additions and 37 deletions.
65 changes: 28 additions & 37 deletions phpmyfaq/src/phpMyFAQ/Controller/Api/FaqController.php
Expand Up @@ -81,12 +81,11 @@ class FaqController extends AbstractController
)]
public function getByCategoryId(Request $request): JsonResponse
{
$configuration = Configuration::getConfigurationInstance();
$user = CurrentUser::getCurrentUser($configuration);
$user = CurrentUser::getCurrentUser($this->configuration);

[ $currentUser, $currentGroups ] = CurrentUser::getCurrentUserGroupId($user);

$faq = new Faq($configuration);
$faq = new Faq($this->configuration);
$faq->setUser($currentUser);
$faq->setGroups($currentGroups);

Expand Down Expand Up @@ -158,12 +157,11 @@ public function getByCategoryId(Request $request): JsonResponse
)]
public function getById(Request $request): JsonResponse
{
$configuration = Configuration::getConfigurationInstance();
$user = CurrentUser::getCurrentUser($configuration);
$user = CurrentUser::getCurrentUser($this->configuration);

[ $currentUser, $currentGroups ] = CurrentUser::getCurrentUserGroupId($user);

$faq = new Faq($configuration);
$faq = new Faq($this->configuration);
$faq->setUser($currentUser);
$faq->setGroups($currentGroups);

Expand Down Expand Up @@ -226,18 +224,17 @@ public function getById(Request $request): JsonResponse
)]
public function getByTagId(Request $request): JsonResponse
{
$configuration = Configuration::getConfigurationInstance();
$user = CurrentUser::getCurrentUser($configuration);
$user = CurrentUser::getCurrentUser($this->configuration);

[ $currentUser, $currentGroups ] = CurrentUser::getCurrentUserGroupId($user);

$faq = new Faq($configuration);
$faq = new Faq($this->configuration);
$faq->setUser($currentUser);
$faq->setGroups($currentGroups);

$tagId = Filter::filterVar($request->get('tagId'), FILTER_VALIDATE_INT);

$tags = new Tags($configuration);
$tags = new Tags($this->configuration);
$recordIds = $tags->getFaqsByTagId($tagId);

try {
Expand Down Expand Up @@ -282,12 +279,11 @@ public function getByTagId(Request $request): JsonResponse
)]
public function getPopular(): JsonResponse
{
$configuration = Configuration::getConfigurationInstance();
$user = CurrentUser::getCurrentUser($configuration);
$user = CurrentUser::getCurrentUser($this->configuration);

[ $currentUser, $currentGroups ] = CurrentUser::getCurrentUserGroupId($user);

$faq = new Faq($configuration);
$faq = new Faq($this->configuration);
$faq->setUser($currentUser);
$faq->setGroups($currentGroups);

Expand Down Expand Up @@ -334,13 +330,11 @@ public function getPopular(): JsonResponse
)]
public function getLatest(): JsonResponse
{
$jsonResponse = new JsonResponse();
$configuration = Configuration::getConfigurationInstance();
$user = CurrentUser::getCurrentUser($configuration);
$user = CurrentUser::getCurrentUser($this->configuration);

[ $currentUser, $currentGroups ] = CurrentUser::getCurrentUserGroupId($user);

$faq = new Faq($configuration);
$faq = new Faq($this->configuration);
$faq->setUser($currentUser);
$faq->setGroups($currentGroups);

Expand Down Expand Up @@ -392,12 +386,11 @@ public function getLatest(): JsonResponse
)]
public function getSticky(): JsonResponse
{
$configuration = Configuration::getConfigurationInstance();
$user = CurrentUser::getCurrentUser($configuration);
$user = CurrentUser::getCurrentUser($this->configuration);

[ $currentUser, $currentGroups ] = CurrentUser::getCurrentUserGroupId($user);

$faq = new Faq($configuration);
$faq = new Faq($this->configuration);
$faq->setUser($currentUser);
$faq->setGroups($currentGroups);

Expand Down Expand Up @@ -456,16 +449,17 @@ public function getSticky(): JsonResponse
)]
public function list(): JsonResponse
{
$jsonResponse = new JsonResponse();
$configuration = Configuration::getConfigurationInstance();
$user = CurrentUser::getCurrentUser($configuration);
$user = CurrentUser::getCurrentUser($this->configuration);

[ $currentUser, $currentGroups ] = CurrentUser::getCurrentUserGroupId($user);

$faq = new Faq($configuration);
$faq = new Faq($this->configuration);
$faq->setUser($currentUser);
$faq->setGroups($currentGroups);
$faq->getAllRecords(FAQ_SORTING_TYPE_CATID_FAQID, ['lang' => $configuration->getLanguage()->getLanguage()]);
$faq->getAllRecords(
FAQ_SORTING_TYPE_CATID_FAQID,
['lang' => $this->configuration->getLanguage()->getLanguage()]
);
$result = $faq->faqRecords;

if ((is_countable($result) ? count($result) : 0) === 0) {
Expand Down Expand Up @@ -570,22 +564,20 @@ public function create(Request $request): JsonResponse
{
$this->hasValidToken();

$jsonResponse = new JsonResponse();
$configuration = Configuration::getConfigurationInstance();
$user = CurrentUser::getCurrentUser($configuration);
$user = CurrentUser::getCurrentUser($this->configuration);

[ $currentUser, $currentGroups ] = CurrentUser::getCurrentUserGroupId($user);

$data = json_decode($request->getContent(), false, 512, JSON_THROW_ON_ERROR);

$currentLanguage = $configuration->getLanguage()->getLanguage();
$currentLanguage = $this->configuration->getLanguage()->getLanguage();

$category = new Category($configuration, $currentGroups, true);
$category = new Category($this->configuration, $currentGroups, true);
$category->setUser($currentUser);
$category->setGroups($currentGroups);
$category->setLanguage($currentLanguage);

$faq = new Faq($configuration);
$faq = new Faq($this->configuration);
$faq->setUser($currentUser);
$faq->setGroups($currentGroups);

Expand Down Expand Up @@ -646,7 +638,7 @@ public function create(Request $request): JsonResponse

$faqId = $faq->create($faqData);

$faqMetaData = new MetaData($configuration);
$faqMetaData = new MetaData($this->configuration);
$faqMetaData
->setFaqId($faqId)
->setFaqLanguage($languageCode)
Expand Down Expand Up @@ -742,21 +734,20 @@ public function update(Request $request): JsonResponse
{
$this->hasValidToken();

$configuration = Configuration::getConfigurationInstance();
$user = CurrentUser::getCurrentUser($configuration);
$user = CurrentUser::getCurrentUser($this->configuration);

[ $currentUser, $currentGroups ] = CurrentUser::getCurrentUserGroupId($user);

$data = json_decode($request->getContent(), false, 512, JSON_THROW_ON_ERROR);

$currentLanguage = $configuration->getLanguage()->getLanguage();
$currentLanguage = $this->configuration->getLanguage()->getLanguage();

$category = new Category($configuration, $currentGroups, true);
$category = new Category($this->configuration, $currentGroups, true);
$category->setUser($currentUser);
$category->setGroups($currentGroups);
$category->setLanguage($currentLanguage);

$faq = new Faq($configuration);
$faq = new Faq($this->configuration);
$faq->setUser($currentUser);
$faq->setGroups($currentGroups);

Expand Down

0 comments on commit e116a77

Please sign in to comment.