<commit_before><?php

/*
* This file is part of the GTheronRestBundle package.
*
* (c) <NAME> <<EMAIL>>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
 */

namespace GTheron\RestBundle\Controller;

use FOS\RestBundle\Controller\FOSRestController;
use GTheron\RestBundle\Annotation\ResourceControllerAnnotation;
use GTheron\RestBundle\Model\LinkableResourceInterface;
use GTheron\RestBundle\Model\ResourceInterface;
use GTheron\RestBundle\Roles;
use GTheron\RestBundle\Security\SecurityResourceManagerInterface;
use GTheron\RestBundle\Service\ResourceManager;
use GTheron\RestBundle\Service\ResourceManagerInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;

/**
 * ResourceController
 *
 * @package GTheron\RestBundle\Controller;
 * @author <NAME> <<EMAIL>>
*/
abstract class ResourceController extends FOSRestController
{
    //TODO refactor those constants somewhere better
    const HTTP_METHOD_GET = 'GET';
    const HTTP_METHOD_POST = 'POST';
    const HTTP_METHOD_PUT = 'PUT';
    const HTTP_METHOD_PATCH = 'PATCH';
    const HTTP_METHOD_DELETE = 'DELETE';
    const HTTP_METHOD_LINK = 'LINK';
    const HTTP_METHOD_UNLINK = 'UNLINK';

    /**
     * Returns a standard collection view for the controller's Resource
     *
     * @throws AccessDeniedHttpException
     * @return Response
     */
    protected function getResourceCollectionView()
    {
        $rm = $this->getResourceManager();

        $resourceClass = $this->getResourceClass();
        $resource = new $resourceClass();
        $this->checkAuthorization(Roles::VIEW_ALL, $resource);

        $resources = $rm->findAll($resource);

        return $this->handleView($this->view($resources));
    }

    /**
     * Returns a standard view for a resource
     *
     * @param string $uid
     * @return Response
     */
    protected function getResourceView($uid)
    {
        $resourceClass = $this->getResourceClass();
        $resource = $this->getAuthorizedResource(Roles::VIEW, $resourceClass, $uid);

        return $this->handleView($this->view($resource));
    }

    /**
     * Returns a standard view after treating a POST request on a resource
     *
     * @param Request $request
     * @return Response
     */
    protected function postResourceView(Request $request)
    {
        $resourceClass = $this->getResourceClass();
        $resource = new $resourceClass();
        $this->checkAuthorization(Roles::CREATE, $resource);
        return $this->processResource($resource, $request, true);
    }

    /**
     * Returns a standard view after treating a PUT request on a resource
     *
     * @param Request $request
     * @param $uid
     * @return Response
     */
    protected function putResourceView(Request $request, $uid)
    {
        $resourceClass = $this->getResourceClass();
        $resource = $this->getAuthorizedResource(Roles::EDIT, $resourceClass, $uid);
        return $this->processResource($resource, $request, false);
    }

    /**
     * Returns a standard view after treating a DELETE request on a resource
     *
     * @param $uid
     * @return Response
     */
    protected function deleteResourceView($uid)
    {
        $resourceClass = $this->getResourceClass();
        $resource = $this->getAuthorizedResource(Roles::DELETE, $resourceClass, $uid);

        $rm = $this->getResourceManager();<commit_msg>as per marcverney's suggestion, changed [action]ResourceView to clearer [action]ResourceResponse<commit_after><?php

/*
* This file is part of the GTheronRestBundle package.
*
* (c) <NAME> <<EMAIL>>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
 */

namespace GTheron\RestBundle\Controller;

use FOS\RestBundle\Controller\FOSRestController;
use GTheron\RestBundle\Annotation\ResourceControllerAnnotation;
use GTheron\RestBundle\Model\LinkableResourceInterface;
use GTheron\RestBundle\Model\ResourceInterface;
use GTheron\RestBundle\Roles;
use GTheron\RestBundle\Security\SecurityResourceManagerInterface;
use GTheron\RestBundle\Service\ResourceManager;
use GTheron\RestBundle\Service\ResourceManagerInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;

/**
 * ResourceController
 *
 * @package GTheron\RestBundle\Controller;
 * @author <NAME> <<EMAIL>>
*/
abstract class ResourceController extends FOSRestController
{
    //TODO refactor those constants somewhere better
    const HTTP_METHOD_GET = 'GET';
    const HTTP_METHOD_POST = 'POST';
    const HTTP_METHOD_PUT = 'PUT';
    const HTTP_METHOD_PATCH = 'PATCH';
    const HTTP_METHOD_DELETE = 'DELETE';
    const HTTP_METHOD_LINK = 'LINK';
    const HTTP_METHOD_UNLINK = 'UNLINK';

    /**
     * Returns a standard collection view for the controller's Resource
     *
     * @throws AccessDeniedHttpException
     * @return Response
     */
    protected function cgetResourceResponse()
    {
        $rm = $this->getResourceManager();

        $resourceClass = $this->getResourceClass();
        $resource = new $resourceClass();
        $this->checkAuthorization(Roles::VIEW_ALL, $resource);

        $resources = $rm->findAll($resource);

        return $this->handleView($this->view($resources));
    }

    /**
     * Returns a standard view for a resource
     *
     * @param string $uid
     * @return Response
     */
    protected function getResourceResponse($uid)
    {
        $resourceClass = $this->getResourceClass();
        $resource = $this->getAuthorizedResource(Roles::VIEW, $resourceClass, $uid);

        return $this->handleView($this->view($resource));
    }

    /**
     * Returns a standard view after treating a POST request on a resource
     *
     * @param Request $request
     * @return Response
     */
    protected function postResourceResponse(Request $request)
    {
        $resourceClass = $this->getResourceClass();
        $resource = new $resourceClass();
        $this->checkAuthorization(Roles::CREATE, $resource);
        return $this->processResource($resource, $request, true);
    }

    /**
     * Returns a standard view after treating a PUT request on a resource
     *
     * @param Request $request
     * @param $uid
     * @return Response
     */
    protected function putResourceResponse(Request $request, $uid)
    {
        $resourceClass = $this->getResourceClass();
        $resource = $this->getAuthorizedResource(Roles::EDIT, $resourceClass, $uid);
        return $this->processResource($resource, $request, false);
    }

    /**
     * Returns a standard view after treating a DELETE request on a resource
     *
     * @param $uid
     * @return Response
     */
    protected function deleteResourceResponse($uid)
    {
        $resourceClass = $this->getResourceClass();
        $resource = $this->getAuthorizedResource(Roles::DELETE, $resourceClass, $uid);

        $rm = $this->getResourceManager();
{'fraction_non_alphanumeric': 0.1093547551179062, 'fraction_numerical': 0.0, 'mean_word_length': 3.6584188292094146, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4506454', 'n_tokens_mistral': 2108, 'n_tokens_neox': 2087, 'n_words': 503}