<commit_before><?php

namespace Openl10n\Bundle\ApiBundle\Controller;

use Doctrine\Common\Collections\ArrayCollection;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Request\ParamFetcher;
use FOS\RestBundle\Routing\ClassResourceInterface;
use FOS\RestBundle\View\View;
use Openl10n\Bundle\ApiBundle\Facade\Resource as ResourceFacade;
use Openl10n\Bundle\ApiBundle\Facade\TranslationKey as TranslationKeyFacade;
use Openl10n\Domain\Project\Model\Project;
use Openl10n\Domain\Translation\Application\Action\CreateResourceAction;
use Openl10n\Domain\Translation\Application\Action\ExportTranslationFileAction;
use Openl10n\Domain\Translation\Application\Action\ImportTranslationFileAction;
use Openl10n\Domain\Translation\Application\Action\UpdateResourceAction;
use Openl10n\Domain\Translation\Model\Key;
use Openl10n\Domain\Translation\Model\Resource;
use Openl10n\Value\String\Slug;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class ResourceController extends Controller implements ClassResourceInterface
{
    /**
     * @Rest\QueryParam(name="project", strict=true, nullable=false)
     * @Rest\View
     */
    public function cgetAction(ParamFetcher $paramFetcher)
    {
        $project = $this->findProjectOr404($paramFetcher->get('project'));
        $resources = $this->get('openl10n.repository.resource')->findByProject($project);

        return (new ArrayCollection($resources))->map(function(Resource $resource) {
            return new ResourceFacade($resource);
        });
    }

    /**
     * @Rest\View
     */
    public function getAction($resource)
    {
        $resource = $this->findResourceOr404($resource);

        return new ResourceFacade($resource);
    }

    /**
     * @Rest\View
     */
    public function cpostAction(Request $request)
    {
        $action = new CreateResourceAction();
        $form = $this->get('form.factory')->createNamed('', 'openl10n_create_resource', $action, array(
            'csrf_protection' => false
        ));

        if ($form->handleRequest($request)->isValid()) {
            $resource = $this->get('openl10n.processor.create_resource')->execute($action);
            $url = $this->generateUrl(
                'openl10n_api_get_resource',
                array(
                    'resource' => $resource->getId(),
                ),
                true // absolute
            );

            return new Response('', 201, array('Location' => $url));
        }

        return View::create($form, 400);
    }

    /**
     * @Rest\View
     */
    public function putAction(Request $request, $resource)
    {
        $resource = $this->findResourceOr404($resource);

        $action = new UpdateResourceAction($resource);
        $form = $this->get('form.factory')->createNamed('', 'openl10n_update_resource', $action, array(
            'csrf_protection' => false
        ));

        if ($form->submit($request->request->all(), false)->isValid()) {
            $resource = $this->get('openl10n.processor.update_resource')->execute($action);

            return new Response('', 204);
        }

        return View::create($form, 400);
    }

    /**
     * @Rest\View(statusCode=204)
     */
    public function deleteAction(Request $request, $resource)
    {
        // TODO
    }

    /**
     * @Rest\View
     */
    public function getTranslationsAction(Request $request, $resource)<commit_msg>Return resource response on post<commit_after><?php

namespace Openl10n\Bundle\ApiBundle\Controller;

use Doctrine\Common\Collections\ArrayCollection;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Request\ParamFetcher;
use FOS\RestBundle\Routing\ClassResourceInterface;
use FOS\RestBundle\View\View;
use Openl10n\Bundle\ApiBundle\Facade\Resource as ResourceFacade;
use Openl10n\Bundle\ApiBundle\Facade\TranslationKey as TranslationKeyFacade;
use Openl10n\Domain\Project\Model\Project;
use Openl10n\Domain\Translation\Application\Action\CreateResourceAction;
use Openl10n\Domain\Translation\Application\Action\ExportTranslationFileAction;
use Openl10n\Domain\Translation\Application\Action\ImportTranslationFileAction;
use Openl10n\Domain\Translation\Application\Action\UpdateResourceAction;
use Openl10n\Domain\Translation\Model\Key;
use Openl10n\Domain\Translation\Model\Resource;
use Openl10n\Value\String\Slug;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class ResourceController extends Controller implements ClassResourceInterface
{
    /**
     * @Rest\QueryParam(name="project", strict=true, nullable=false)
     * @Rest\View
     */
    public function cgetAction(ParamFetcher $paramFetcher)
    {
        $project = $this->findProjectOr404($paramFetcher->get('project'));
        $resources = $this->get('openl10n.repository.resource')->findByProject($project);

        return (new ArrayCollection($resources))->map(function(Resource $resource) {
            return new ResourceFacade($resource);
        });
    }

    /**
     * @Rest\View
     */
    public function getAction($resource)
    {
        $resource = $this->findResourceOr404($resource);

        return new ResourceFacade($resource);
    }

    /**
     * @Rest\View
     */
    public function cpostAction(Request $request)
    {
        $action = new CreateResourceAction();
        $form = $this->get('form.factory')->createNamed('', 'openl10n_create_resource', $action, array(
            'csrf_protection' => false
        ));

        if ($form->handleRequest($request)->isValid()) {
            $resource = $this->get('openl10n.processor.create_resource')->execute($action);
            $facade = new ResourceFacade($resource);

            $url = $this->generateUrl(
                'openl10n_api_get_resource',
                ['resource' => $resource->getId()],
                true // absolute
            );

            return View::create($facade, 201, ['Location' => $url]);
        }

        return View::create($form, 400);
    }

    /**
     * @Rest\View
     */
    public function putAction(Request $request, $resource)
    {
        $resource = $this->findResourceOr404($resource);

        $action = new UpdateResourceAction($resource);
        $form = $this->get('form.factory')->createNamed('', 'openl10n_update_resource', $action, array(
            'csrf_protection' => false
        ));

        if ($form->submit($request->request->all(), false)->isValid()) {
            $resource = $this->get('openl10n.processor.update_resource')->execute($action);
            $facade = new ResourceFacade($resource);

            return View::create($facade, 204);
        }

        return View::create($form, 400);
    }

    /**
     * @Rest\View(statusCode=204)
     */
    public function deleteAction(Request $request, $resource)
    {
        // TODO
    }

    /**
     * @Rest\View
     */
    public function getTranslationsAction(Request $request, $resource)
{'fraction_non_alphanumeric': 0.13201274414738884, 'fraction_numerical': 0.016068707577226764, 'mean_word_length': 3.1734104046242773, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 57, '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': '4739773', 'n_tokens_mistral': 2109, 'n_tokens_neox': 2002, 'n_words': 326}