src/Controller/Admin/ContributionController.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Admin;
  3. use App\Entity\Contribution;
  4. use App\Repository\ContributionRepository;
  5. use FOS\RestBundle\Routing\ClassResourceInterface;
  6. use FOS\RestBundle\View\View;
  7. use FOS\RestBundle\View\ViewHandlerInterface;
  8. use Psr\Container\ContainerInterface;
  9. use Sulu\Bundle\ContactBundle\Entity\ContactRepositoryInterface;
  10. use Sulu\Component\Rest\AbstractRestController;
  11. use Sulu\Component\Rest\ListBuilder\Doctrine\DoctrineListBuilderFactoryInterface;
  12. use Sulu\Component\Rest\ListBuilder\Metadata\FieldDescriptorFactoryInterface;
  13. use Sulu\Component\Rest\ListBuilder\PaginatedRepresentation;
  14. use Sulu\Component\Rest\RestController;
  15. use Sulu\Component\Rest\RestHelperInterface;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  19. class ContributionController extends AbstractRestController implements ClassResourceInterface
  20. {
  21.     /**
  22.      * @var ViewHandlerInterface
  23.      */
  24.     private $viewHandler;
  25.     /**
  26.      * @var FieldDescriptorFactoryInterface
  27.      */
  28.     private $fieldDescriptorFactory;
  29.     /**
  30.      * @var DoctrineListBuilderFactoryInterface
  31.      */
  32.     private $listBuilderFactory;
  33.     /**
  34.      * @var RestHelperInterface
  35.      */
  36.     private $restHelper;
  37.     /** @var ContributionRepository */
  38.     private $repository;
  39.     public function __construct(
  40.         ViewHandlerInterface $viewHandler,
  41.         FieldDescriptorFactoryInterface $fieldDescriptorFactory,
  42.         DoctrineListBuilderFactoryInterface $listBuilderFactory,
  43.         RestHelperInterface $restHelper,
  44.         ContributionRepository $repository,
  45.         ContainerInterface $container
  46.     ) {
  47.         $this->viewHandler $viewHandler;
  48.         $this->fieldDescriptorFactory $fieldDescriptorFactory;
  49.         $this->listBuilderFactory $listBuilderFactory;
  50.         $this->restHelper $restHelper;
  51.         $this->repository $repository;
  52.         parent::__construct($this->viewHandler);
  53.     }
  54.     public function cgetAction(): Response
  55.     {
  56.         $fieldDescriptors $this->fieldDescriptorFactory->getFieldDescriptors(Contribution::RESOURCE_KEY);
  57.         $listBuilder $this->listBuilderFactory->create(Contribution::class);
  58.         $this->restHelper->initializeListBuilder($listBuilder$fieldDescriptors);
  59.         $max=10;
  60.         $contributions $this->repository->allContributions($max);
  61.         foreach ($contributions as &$contribution) {
  62.             if (isset($contribution['data'])) {
  63.                 $contribution['data'] = $this->convertData($contribution['data']);
  64.             }
  65.         }
  66.         $listRepresentation = new PaginatedRepresentation(
  67.             $contributions,
  68.             Contribution::RESOURCE_KEY,
  69.             1,
  70.             $max,
  71.             count($contributions)
  72.         );
  73.         return $this->viewHandler->handle(View::create($listRepresentation));
  74.     }
  75.     public function getAction(int $idRequest $request): Response
  76.     {
  77.         $entity $this->load($id$request);
  78.         if (!$entity) {
  79.             throw new NotFoundHttpException();
  80.         }
  81.         return $this->handleView($this->view($entity));
  82.     }
  83.     public function postAction(Request $request): Response
  84.     {
  85.         $entity = new Contribution();
  86.         $data $request->request->all();
  87.         $entity->setJournal($data['journal']);
  88.         $entity->setAuthor($data['author']);
  89.         $entity->setData($data['data']);
  90.         $entity->setApproved($data['approved']);
  91.         $this->repository->save($entity);
  92.         return $this->handleView($this->view($entity));
  93.     }
  94.     public function putAction(int $idRequest $request): Response
  95.     {
  96.         $entity $this->load($id$request);
  97.         if (empty($entity)) {
  98.             throw new NotFoundHttpException();
  99.         }
  100.         $data $request->request->all();
  101.         $entity->setJournal($data['journal']);
  102.         $entity->setAuthor($data['author']);
  103.         $entity->setData($data['data']);
  104.         $entity->setApproved($data['approved']);
  105.         $this->repository->save($entity);
  106.         return $this->handleView($this->view($entity));
  107.     }
  108.     protected function load(int $idRequest $request): ?Contribution
  109.     {
  110.         return $this->repository->findOneById($id);
  111.     }
  112.     protected function convertData($data)
  113.     {
  114.         $dataString '';
  115.         foreach ($data as $key => $value) {
  116.             $dataString .= "{$key}{$value}, ";
  117.         }
  118.         $dataString rtrim($dataString', '); // Supprimer la virgule finale
  119.         return $dataString;
  120.     }
  121. }