src\Mobile\BranchesController.php line 72

Open in your IDE?
  1. <?php
  2. namespace App\Mobile;
  3. use App\Entity\Branch;
  4. use App\Entity\SystemUser;
  5. use Doctrine\Persistence\ManagerRegistry;
  6. use Doctrine\Persistence\ObjectManager;
  7. use JMS\Serializer\SerializationContext;
  8. use JMS\Serializer\SerializerBuilder;
  9. use Psr\Log\LoggerInterface;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. class BranchesController extends AbstractController {
  15.     private ObjectManager $em;
  16.     public function __construct(ManagerRegistry $managerRegistry)
  17.     {
  18.         $this->em $managerRegistry->getManager();
  19.     }
  20.     /**
  21.      * @Route("/branches", methods={"POST"}, name="systemBranches")
  22.      * @param LoggerInterface $logger
  23.      * @param Request $request
  24.      * @return Response
  25.      */
  26.     public function branches(LoggerInterface $loggerRequest $request): Response
  27.     {
  28.         $em $this->em;
  29.         $data $request->getContent();
  30.         $logger->info("-------------------------------------------------");
  31.         $logger->info($data);
  32.         $logger->info("-------------------------------------------------");
  33.         $branches json_decode($data);
  34.         foreach ($branches as $item) {
  35.             $branch = new Branch();
  36.             $branch->setBranchCode($item->branch_code);
  37.             $branch->setPhone($item->phone);
  38.             $branch->setEmail($item->email);
  39.             $branch->setBranchName($item->branch_name);
  40.             $branch->setContactPerson($item->person);
  41.             $branch->setActive($item->active);
  42.             $branch->setCreatedAt(new \DateTime());
  43.             $em->persist($branch);
  44.         }
  45. //
  46.         $em->flush();
  47.         return new Response('OK'Response::HTTP_OK);
  48.     }
  49.     /**
  50.      * @Route("/branches", methods={"GET"}, name="getSystemBranches")
  51.      * @param LoggerInterface $logger
  52.      * @param Request $request
  53.      * @return Response
  54.      */
  55.     public function getBranches(LoggerInterface $loggerRequest $request): Response {
  56.         $em $this->em;
  57.         $branches $em->getRepository(Branch::class)->findBy([
  58.             'active' => true
  59.         ]);
  60.         $context = new SerializationContext();
  61.         $context->setSerializeNull(true);
  62.         $serializer SerializerBuilder::create()->build();
  63.         $data $serializer->serialize($branches'json'$context);
  64.         return new Response($dataResponse::HTTP_OK, [
  65.             'content-type'=>'application/json'
  66.         ]);
  67.     }
  68. }