PHP code example of tbbc / rest-util-bundle

1. Go to this page and download the library: Download tbbc/rest-util-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tbbc / rest-util-bundle example snippets




namespace My\ApiBundle\EventListener;

// ... other use statements
use Tbbc\RestUtil\Error\ErrorResolverInterface;

class RestExceptionListener extends ExceptionListener
{
    private $errorResolver;

    public function __construct(ErrorResolverInterface $errorResolver, $controller, LoggerInterface $logger = null)
    {
        $this->errorResolver = $errorResolver;
        parent::__construct($controller, $logger);
    }

    /**
     * @param GetResponseForExceptionEvent $event
     * @return void
     */
    public function onKernelException(GetResponseForExceptionEvent $event)
    {
        static $handling;

        if (true === $handling) {
            return;
        }

        $exception = $event->getException();
        $error = $this->errorResolver->resolve($exception);
        if (null == $error) {
            return;
        }

        $handling = true;

        $response = new Response(json_encode($error->toArray()), $error->getHttpStatusCode(), array(
            'Content-Type' => 'application/json'
        ));

        $event->setResponse($response);
    }

    public static function getSubscribedEvents()
    {
        return array(
            KernelEvents::EXCEPTION => array('onKernelException', 10),
        );
    }
}



namespace My\ApiBundle\Controller;

// ... other use statements
use Symfony\Component\Security\Core\AccessDeniedException;
use Tbbc\RestUtilBundle\Error\Exception\FormErrorException;
use My\ApiBundle\Exception\CustomException;

class PostCommentsController extends Controller
{
    public function postCommentsAction($postId)
    {
        // ... fetch $post with $postId

        if (!$this->get('security.context')->isGranted('COMMENT', $post)) {
            throw new AccessDeniedException('Access denied');
        }

        $commentResource = new CommentResource();
        $form = $this->createNamedForm('', new CommentResourceType(), $commentResource);
        $form->bind($this->getRequest());

        if (!$form->isValid()) {
            throw new FormErrorException($form);
        }

        // another error
        if (....) {
            throw new CustomException('Something bad just happened!');
        }

        // ... save comment or whatever
    }
}



namespace My\ApiBundle\Error\Factory;

use Tbbc\RestUtil\Error\Error;
use Tbbc\RestUtil\Error\ErrorFactoryInterface;
use Tbbc\RestUtil\Error\Mapping\ExceptionMappingInterface;
use My\ApiBundle\Exception\CustomException;

class CustomErrorFactory implements ErrorFactoryInterface
{
    /**
     * {@inheritDoc}
     */
    public function getIdentifier()
    {
        return 'my_api_custom';
    }

    /**
     * {@inheritDoc}
     */
    public function createError(\Exception $exception, ExceptionMappingInterface $mapping)
    {
        if (!$this->supportsException($exception)) {
            return null;
        }

        $errorMessage = $mapping->getErrorMessage();
        if (empty($errorMessage)) {
            $errorMessage = $exception->getMessage();
        }

        $extendedMessage = $exception->getMoreExtendedMessage();
        // Or whatever you need to do with your exception here

        return new Error($mapping->getHttpStatusCode(), $mapping->getErrorCode(), $errorMessage,
            $extendedMessage, $mapping->getErrorMoreInfoUrl());
    }

    /**
     * {@inheritDoc}
     */
    public function supportsException(\Exception $exception)
    {
        return $exception instanceof CustomException;
    }
}