src/Controller/ConfigurationController.php line 87

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Command\ConfigurationPreparationCommand;
  4. use App\Entity\App\Configuration;
  5. use App\Form\Type\SingleConfigType;
  6. use App\Service\ApiService;
  7. use Doctrine\Persistence\ManagerRegistry;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  12. /**
  13.  * Pobieranie danych konfiguracyjnych
  14.  *
  15.  * Zwracane są jedynie pola konfiguracyjne, które mają ustawioną flagę **isApiAvailable** na true.
  16.  *
  17.  * @see Configuration Encja konfiguracyjna
  18.  * @see ConfigurationPreparationCommand Uzupełnianie bazy konfiguracyjnej o wymagane dane
  19.  * @see \AF\AdminBundle\Controller\ConfigurationController Zarządzanie konfiguracją (CMS)
  20.  */
  21. class ConfigurationController extends AbstractController
  22. {
  23.     /**
  24.      * Wstrzykiwanie serwisów
  25.      *
  26.      * @param ManagerRegistry $registry Obsługa bazy danych
  27.      */
  28.     public function __construct(private ManagerRegistry $registry)
  29.     {
  30.     }
  31.     /**
  32.      * Get list of configurations or return one configuration information
  33.      *
  34.      * @api {get} /api/v1/public/configurations Lista kategorii
  35.      * @apiDescription <a href="#api-Konfiguracja-GetApiV1PublicConfigurationsSlug"> Zobacz szczegóły konfiguracji </a>
  36.      * @apiVersion 1.0.0
  37.      * @apiGroup Konfiguracja
  38.      * @apiSuccess {Integer} id Id
  39.      * @apiSuccess {String} name Nazwa kategorii
  40.      * @apiSuccess {Integer} type Typ
  41.      * @apiSuccess {String} iso Język ("pl" / "en" / "de" )
  42.      * @apiError {Array} messages Opisy błędów
  43.      * @apiExample Success 200
  44.      *  HTTP/1.1 200 OK
  45.      * [
  46.      *  {
  47.      *      "id": 9
  48.      *      "name": "Relaks i regeneracja",
  49.      *      "value": "/uploads/general/yoga_schedule_7.pdf",
  50.      *      "iso": "pl"
  51.      *  }
  52.      * ]
  53.      */
  54.     /**
  55.      * Pobieranie udostępnionych pól konfiguracyjnych
  56.      *
  57.      * @api {get} /api/v1/public/configurations/:slug Szczegóły konfiguracji
  58.      * @apiDescription <b> Uwaga! </b> Ten endpoint nie zwraca dodatkowych nagłówków. <br/>
  59.      *  Zwracane są tylko te wartości, które w bazie mają zaznaczone "isApiAvailable" = 1. <br/>
  60.      *  Zamiast slugu możemy podać nazwę interesującego nas wpisu, np. yoga_schedule <br/>
  61.      *  Dokumentacja API może wprowadzać w błąd, dlatego dla wyjaśnienia
  62.      * @apiSampleRequest /api/v1/public/configurations/
  63.      * @apiVersion 1.0.0
  64.      * @apiGroup Konfiguracja
  65.      * @apiSuccess {Integer} id Id
  66.      * @apiSuccess {String} name Nazwa kategorii
  67.      * @apiSuccess {Integer} type Typ
  68.      * @apiSuccess {String} iso Język ("pl" / "en" / "de" )
  69.      * @apiError {Array} messages Opisy błędów
  70.      * @apiExample Success 200
  71.      *  HTTP/1.1 200 OK
  72.      *  {
  73.      *      "id": 9
  74.      *      "name": "Relaks i regeneracja",
  75.      *      "value": "/uploads/general/yoga_schedule_7.pdf",
  76.      *      "iso": "pl"
  77.      *  }
  78.      *
  79.      * @param ApiService $apiService Obsługa danych zwracanych na front
  80.      * @param string|null $slug Zawężenie wyszukiwania do jednej opcji
  81.      * @return JsonResponse
  82.      */
  83.     public function configurations(ApiService $apiServicestring $slug null): JsonResponse
  84.     {
  85.         $filters = [
  86.             'isApiAvailable' => true,
  87.             'iso' => $apiService->getClientLanguage(),
  88.         ];
  89.         if ( $slug !== null ) {
  90.             $filters['name'] = $slug;
  91.         }
  92.         $configurations $this->registry->getRepository('App\Entity\App\Configuration')->findBy($filters);
  93.         if ( $configurations === null || count($configurations) === ) {
  94.             return new JsonResponse('Żądany zasób nie został znaleziony.'400);
  95.         }
  96.         $resp = [];
  97.         foreach ($configurations as $config) {
  98.             $resp[] = [
  99.                 'id' => $config->getId(),
  100.                 'name' => $config->getName(),
  101.                 'value' => $config->getValue(),
  102.                 'iso' => $config->getIso()
  103.             ];
  104.         }
  105.         if ($slug !== null && count($resp) === 1) {
  106.             return new JsonResponse($resp[0]);
  107.         }
  108.         return new JsonResponse($resp);
  109.     }
  110.     /**
  111.      * Pobieranie plików
  112.      *
  113.      * Do każdego pliku dodawany jest timestamp, dlatego nie są one trzymane w cache'u.
  114.      * Obsługa została wprowadzona, ponieważ w CMS, podczas aktualizacji pliku, obrazek się podmieniał w bazie
  115.      * danych, ale na froncie nadal był wczytywany stary plik.
  116.      *
  117.      * @see SingleConfigType Nadpisanie funkcji do pobierania plików konfiguracyjnych w paczce AdminBundle
  118.      *
  119.      * @param string $filename Nazwa pliku
  120.      * @return RedirectResponse
  121.      */
  122.     public function fileRedirect(string $filename): RedirectResponse
  123.     {
  124.         $configuration $this->registry->getRepository('App\Entity\App\Configuration')
  125.             ->createQueryBuilder('f')
  126.             ->where('f.valueType = :valueType')
  127.             ->andWhere('f.value LIKE :name')
  128.             ->setParameters([
  129.                 'valueType' => 'file',
  130.                 'name' => '%'.$filename
  131.             ])
  132.             ->getQuery()
  133.             ->getResult();
  134.         if ( count($configuration) === ) {
  135.             throw new NotFoundHttpException();
  136.         }
  137.         return $this->redirect($configuration[0]->getValue().'?v='.(new \DateTime())->getTimestamp());
  138.     }
  139. }