src/Controller/DigiEntityController.php line 34

Open in your IDE?
  1. <?php
  2. // src/Controller/DigiEntityController.php
  3. namespace App\Controller;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Doctrine\Persistence\ManagerRegistry;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Symfony\Component\Validator\Validator\ValidatorInterface;
  12. use Symfony\Contracts\HttpClient\HttpClientInterface;
  13. use Symfony\Component\HttpClient\HttpOptions;
  14. use Symfony\Component\DependencyInjection\ContainerInterface;
  15. use App\Repository\EntityRepository;
  16. use App\Repository\DigiEntityRepository;
  17. /**
  18.  * @Route("/entity", name="entity_")
  19.  */
  20. abstract class DigiEntityController extends EntityController
  21. {
  22.     /** @var DigiEntityRepository $repository */
  23.     protected ?EntityRepository $repository;
  24.     protected HttpClientInterface $client;
  25.     public function __construct(ContainerInterface $containerEntityManagerInterface $emHttpClientInterface $client)
  26.     {
  27.         parent::__construct($container$em);
  28.         
  29.         $this->client $client;
  30.         $this->repository::__setClient($client);
  31.     }
  32.     
  33.     /**
  34.     * @Route("/api/list", name="api_list")
  35.     */
  36.     public function APIListAction() :Response
  37.     {
  38.         $data $this->repository->api_list();
  39.         //var_dump($api_response);
  40.         if (false) foreach($data as $row) {
  41.             var_dump($row);
  42.             echo '<hr />';
  43.             //json_encode($row);
  44.         }
  45.         
  46.         return new JsonResponse(['r'=>true'data'=>$data]);
  47.     }
  48.     /**
  49.     * @Route("/api/sync_all", name="api_sync_all")
  50.     */
  51.     public function APISyncAllAction(Request $request)
  52.     {
  53.         $options=[];
  54.         $options['fromlast'] = !empty($request->query->get('fromlast'));
  55.         $limit $request->query->get('limit');
  56.         if (!is_numeric($limit) || (int)$limit != $limit || $limit<0)
  57.             $limit $this->repository::API_SYNC_LIMIT;
  58.         $page $request->query->get('page');
  59.         if (!is_numeric($page) || (int)$page != $page || $page<0)
  60.             $page 0;
  61.         //var_dump($page, $limit); die();
  62.         
  63.         $data $this->repository->api_sync_all($this->repository::API_QUERY_LIST$limit$page$options);
  64.         return new JsonResponse(['r'=>true'data'=>$data]);
  65.     }
  66.     
  67.     /**
  68.     * @Route("/api/read", name="api_read")
  69.     */
  70.     public function APIReadAction(Request $requestint $digi_id=NULL) :Response
  71.     {
  72.         $digi_id $request->query->get('digi_id');
  73.         $data $this->repository->api_read($this->repository::API_QUERY_ID$digi_id);
  74.         return new JsonResponse(['r'=>true'data'=>$data]);
  75.     }
  76.     
  77.     /**
  78.     * @Route("/api/sync", name="api_sync")
  79.     */
  80.     public function APISyncAction(Request $requestint $digi_id=NULL) :Response
  81.     {
  82.         $digi_id $request->query->get('digi_id');
  83.         $data $this->repository->api_sync($this->repository::API_QUERY_ID$digi_id);
  84.         //var_dump($data); die();
  85.         return new JsonResponse(['r'=>true'data'=>$data]);
  86.     }
  87.     
  88.     /**
  89.     * @Route("/api/{id}/save", name="api_save")
  90.     */
  91.     public function APISaveAction(Request $requestint $id) :Response
  92.     {
  93.         //$id = $request->query->get('id');
  94.         //var_dump($id); die();
  95.         $data $this->repository->api_save($this->repository::API_QUERY_SAVE$id);
  96.         
  97.         return new JsonResponse(['r'=>true'redirect'=>$this->generateUrl(static::REF.'_read', ['id'=>$id]), 'data'=>$data]);
  98.     }
  99.     
  100.     /**
  101.     * @Route("/api/{id}/update", name="api_update")
  102.     */
  103.     public function APIUpdateAction(Request $requestint $id) :Response
  104.     {
  105.         $data $this->repository->api_update($this->repository::API_QUERY_UPDATE$id);
  106.         return new JsonResponse(['r'=>true'data'=>$data]);
  107.     }
  108.     
  109.     /**
  110.     * @Route("/api/delete", name="api_delete")
  111.     */
  112.     public function APIDeleteAction(Request $requestint $digi_id=NULL) :Response
  113.     {
  114.         $digi_id $request->query->get('digi_id');
  115.         $data $this->repository->api_delete($this->repository::API_QUERY_DELETE$digi_id);
  116.         return new JsonResponse(['r'=>true'redirect'=>'''data'=>$data]);
  117.     }
  118.     /* PARAMS */
  119.     
  120.     public function params()
  121.     {
  122.         parent::params();
  123.         $this->params['api'] = [
  124.             'id' => $this->repository::API_ID,
  125.             'name' => $this->repository::API_NAME,
  126.             'map' => $this->repository::API_MAP,
  127.         ];
  128.         $this->params['route'] = array_merge($this->params['route'], [
  129.             'api_read' => static::REF.'_api_read',
  130.             'api_sync' => static::REF.'_api_sync',
  131.             'api_update' => static::REF.'_api_update',
  132.             'api_delete' => static::REF.'_api_delete',
  133.         ]);
  134.     }
  135. }