src/Controller/Admin/WebhookController.php line 38

Open in your IDE?
  1. <?php
  2. namespace EADPlataforma\Controller\Admin;
  3. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use EADPlataforma\Entity\Webhook;
  7. use EADPlataforma\Entity\WebhookQueue;
  8. use EADPlataforma\Entity\User;
  9. use EADPlataforma\Enum\WebhookEnum;
  10. use EADPlataforma\Enum\ErrorEnum;
  11. /**
  12.  * @Route(
  13.  *      path          = "/admin/webhook",
  14.  *      schemes         = {"http|https"}
  15.  * )
  16.  * @Cache(
  17.  *      maxage          = "0",
  18.  *      smaxage         = "0",
  19.  *      expires         = "now",
  20.  *      public          = false
  21.  * )
  22.  */
  23. class WebhookController extends AbstractController {
  24.     public function getEntityClass(){
  25.         return Webhook::class;
  26.     }
  27.     /**
  28.      * @Route(
  29.      *      path          = "/list/paginate",
  30.      *      methods       = {"GET"},
  31.      * )
  32.      */
  33.     public function getWebhookPaginate(Request $request) {
  34.         $permission $this->userPermissionUtil->getPermission("appStore""webhook""see");
  35.         if($this->userPermissionUtil->isLow($permission)){
  36.             return $this->eadResponse(nullErrorEnum::PERMISSION);
  37.         }
  38.         $this->requestUtil->setRequest($request)->setData();
  39.         $columns = [ 
  40.             "wh.id"
  41.             "wh.name",
  42.             "wh.type"
  43.             "wh.url"
  44.             "wh.token",
  45.             "DATE_FORMAT(wh.dateDelete, '%Y-%m-%d %H:%i:%s') AS dateDelete",
  46.             "ud.name AS userDelete"
  47.         ];
  48.         $userClass User::class;
  49.         $joins = [
  50.             "{$userClass} AS ud" => [ "LEFT""ud.id = wh.userDelete" ],
  51.         ];
  52.         $type $this->requestUtil->getField('type');
  53.         $orderParam $this->requestUtil->getField('order');
  54.         $searchText $this->requestUtil->getField('searchText');
  55.         $limit $this->requestUtil->getField('limit');
  56.         $offset $this->requestUtil->getField('offset');
  57.         $filter = [];
  58.         $filterDelete $this->requestUtil->getDeletedParam();
  59.         if(!empty($type)){
  60.             $filter["wh.type"] = $type;
  61.         }
  62.         $order = [ "wh.id" => "ASC" ];
  63.         if(!empty($orderParam)){
  64.             $order json_decode($orderParamtrue);
  65.         }
  66.         $webhooks $this->repository->paginate(
  67.             "wh"
  68.             $searchText
  69.             $columns
  70.             $joins
  71.             $filter
  72.             $order
  73.             $limit
  74.             $offset
  75.             $filterDelete
  76.         );
  77.         return $this->eadResponse($webhooks);
  78.     }
  79.     /**
  80.      * @Route(
  81.      *      path          = "/detail/{id}",
  82.      *      methods       = {"GET"},
  83.      *      requirements  = { "id" = "\d+" }
  84.      * )
  85.      */
  86.     public function getWebhook(Request $request) {
  87.         $permission $this->userPermissionUtil->getPermission("appStore""webhook""see");
  88.         if($this->userPermissionUtil->isLow($permission)){
  89.             return $this->eadResponse(nullErrorEnum::PERMISSION);
  90.         }
  91.         $webhookId $request->get('id');
  92.         $webhook $this->repository->findOneBy([
  93.             "id" => $webhookId
  94.         ]);
  95.         if (!$webhook) {
  96.             return $this->eadResponse(nullErrorEnum::NOT_FOUND);
  97.         }
  98.         return $this->eadResponse($webhook->toReturn());
  99.     }
  100.     /**
  101.      * @Route(
  102.      *      path          = "/register",
  103.      *      methods       = {"POST"},
  104.      * )
  105.      */
  106.     public function registerWebhook(Request $request) {
  107.         $permission $this->userPermissionUtil->getPermission("appStore""webhook""create");
  108.         if($this->userPermissionUtil->isLow($permission)){
  109.             return $this->eadResponse(nullErrorEnum::PERMISSION);
  110.         }
  111.         
  112.         $this->requestUtil->setRequest($request)->setData();
  113.         $webhook = new Webhook();
  114.         if($this->requestUtil->issetField('name')){
  115.             $webhook->setName($this->requestUtil->getField('name'));
  116.         }
  117.         if($this->requestUtil->issetField('type')){
  118.             $webhook->setType((int)$this->requestUtil->getField('type'));
  119.         }
  120.         if($this->requestUtil->issetField('url')){
  121.             $webhook->setUrl($this->requestUtil->getField('url'));
  122.         }
  123.         if($this->requestUtil->issetField('token')){
  124.             $webhook->setToken($this->requestUtil->getField('token'));
  125.         }
  126.         $errors $this->validateEntity($webhook);
  127.         if($errors){
  128.             return $this->eadResponse($errorsErrorEnum::FIELD_EMPTY);
  129.         }
  130.         try{
  131.             $cronService $this->generalService->getService('Aws\\AwsEventBridge');
  132.             $cronService->createByTypeApiDestination(WebhookEnum::CRON_WEBHOOK_QUEUE);
  133.             $this->em->persist($webhook);
  134.             $this->em->flush();
  135.             
  136.             $data $webhook->toReturn();
  137.             $this->userLogService->logInsert("webhook"$webhook->getId(), $data);
  138.             $domain $this->configuration->getClient()->getDomainPrimary();
  139.             $domain $this->configuration->getClient()->getDomainPrimary();
  140.             return $this->eadResponse($data);
  141.         }catch(\Exception $e){
  142.             return $this->eadResponse([ "message" => $e->getMessage() ], ErrorEnum::INTERNAL_ERROR);
  143.         }
  144.         return $this->eadResponse([ "success" => ]);
  145.     }
  146.     /**
  147.      * @Route(
  148.      *      path          = "/edit/{id}",
  149.      *      methods       = {"PUT"},
  150.      *      requirements  = { "id" = "\d+" }
  151.      * )
  152.      */
  153.     public function editWebhook(Request $request) {
  154.         $permission $this->userPermissionUtil->getPermission("appStore""webhook""edit");
  155.         if($this->userPermissionUtil->isLow($permission)){
  156.             return $this->eadResponse(nullErrorEnum::PERMISSION);
  157.         }
  158.         $webhookId $request->get('id');
  159.         $webhook $this->repository->findOneBy([
  160.             "id" => $webhookId,
  161.             "deleted" => WebhookEnum::ITEM_NO_DELETED
  162.         ]);
  163.         if (!$webhook) {
  164.             return $this->eadResponse(nullErrorEnum::NOT_FOUND);
  165.         }
  166.         $this->requestUtil->setRequest($request)->setData();
  167.         if($this->requestUtil->issetField('name')){
  168.             $webhook->setName($this->requestUtil->getField('name'));
  169.         }
  170.         /*if($this->requestUtil->issetField('type')){
  171.             $webhook->setType((int)$this->requestUtil->getField('type'));
  172.         }*/
  173.         if($this->requestUtil->issetField('url')){
  174.             $webhook->setUrl($this->requestUtil->getField('url'));
  175.         }
  176.         if($this->requestUtil->issetField('token')){
  177.             $webhook->setToken($this->requestUtil->getField('token'));
  178.         }
  179.         $errors $this->validateEntity($webhook);
  180.         
  181.         if($errors){
  182.             return $this->eadResponse($errorsErrorEnum::FIELD_EMPTY);
  183.         }
  184.         
  185.         $this->em->flush();
  186.         $data $webhook->toReturn();
  187.         $this->userLogService->logUpdate("webhook"$webhook->getId(), $data);
  188.         return $this->eadResponse($data);
  189.     }
  190. }