<?php
namespace App\Controller\Admin;
use App\Entity\Contribution;
use App\Repository\ContributionRepository;
use FOS\RestBundle\Routing\ClassResourceInterface;
use FOS\RestBundle\View\View;
use FOS\RestBundle\View\ViewHandlerInterface;
use Psr\Container\ContainerInterface;
use Sulu\Bundle\ContactBundle\Entity\ContactRepositoryInterface;
use Sulu\Component\Rest\AbstractRestController;
use Sulu\Component\Rest\ListBuilder\Doctrine\DoctrineListBuilderFactoryInterface;
use Sulu\Component\Rest\ListBuilder\Metadata\FieldDescriptorFactoryInterface;
use Sulu\Component\Rest\ListBuilder\PaginatedRepresentation;
use Sulu\Component\Rest\RestController;
use Sulu\Component\Rest\RestHelperInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class ContributionController extends AbstractRestController implements ClassResourceInterface
{
/**
* @var ViewHandlerInterface
*/
private $viewHandler;
/**
* @var FieldDescriptorFactoryInterface
*/
private $fieldDescriptorFactory;
/**
* @var DoctrineListBuilderFactoryInterface
*/
private $listBuilderFactory;
/**
* @var RestHelperInterface
*/
private $restHelper;
/** @var ContributionRepository */
private $repository;
public function __construct(
ViewHandlerInterface $viewHandler,
FieldDescriptorFactoryInterface $fieldDescriptorFactory,
DoctrineListBuilderFactoryInterface $listBuilderFactory,
RestHelperInterface $restHelper,
ContributionRepository $repository,
ContainerInterface $container
) {
$this->viewHandler = $viewHandler;
$this->fieldDescriptorFactory = $fieldDescriptorFactory;
$this->listBuilderFactory = $listBuilderFactory;
$this->restHelper = $restHelper;
$this->repository = $repository;
parent::__construct($this->viewHandler);
}
public function cgetAction(): Response
{
$fieldDescriptors = $this->fieldDescriptorFactory->getFieldDescriptors(Contribution::RESOURCE_KEY);
$listBuilder = $this->listBuilderFactory->create(Contribution::class);
$this->restHelper->initializeListBuilder($listBuilder, $fieldDescriptors);
$max=10;
$contributions = $this->repository->allContributions($max);
foreach ($contributions as &$contribution) {
if (isset($contribution['data'])) {
$contribution['data'] = $this->convertData($contribution['data']);
}
}
$listRepresentation = new PaginatedRepresentation(
$contributions,
Contribution::RESOURCE_KEY,
1,
$max,
count($contributions)
);
return $this->viewHandler->handle(View::create($listRepresentation));
}
public function getAction(int $id, Request $request): Response
{
$entity = $this->load($id, $request);
if (!$entity) {
throw new NotFoundHttpException();
}
return $this->handleView($this->view($entity));
}
public function postAction(Request $request): Response
{
$entity = new Contribution();
$data = $request->request->all();
$entity->setJournal($data['journal']);
$entity->setAuthor($data['author']);
$entity->setData($data['data']);
$entity->setApproved($data['approved']);
$this->repository->save($entity);
return $this->handleView($this->view($entity));
}
public function putAction(int $id, Request $request): Response
{
$entity = $this->load($id, $request);
if (empty($entity)) {
throw new NotFoundHttpException();
}
$data = $request->request->all();
$entity->setJournal($data['journal']);
$entity->setAuthor($data['author']);
$entity->setData($data['data']);
$entity->setApproved($data['approved']);
$this->repository->save($entity);
return $this->handleView($this->view($entity));
}
protected function load(int $id, Request $request): ?Contribution
{
return $this->repository->findOneById($id);
}
protected function convertData($data)
{
$dataString = '';
foreach ($data as $key => $value) {
$dataString .= "{$key}: {$value}, ";
}
$dataString = rtrim($dataString, ', '); // Supprimer la virgule finale
return $dataString;
}
}