<commit_before><?php

namespace Enneite\SwaggerBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;

/**
 * Class AbstractController.
 */
class SwaggerApiController extends Controller
{
    /**
     * @param array $data
     * @param int   $status
     *
     * @return JsonResponse
     */
    protected function sendJsonResponse(array $data, $status = 200)
    {
        $response = new JsonResponse($data);
        $response->setStatusCode($status);

        return $response;
    }

    /**
     * @param Request $request
     *
     * @throws \InvalidArgumentException
     *
     * @return array
     */
    protected function getJsonContent(Request $request)
    {
        $content = json_decode($request->getContent(), true);
        if (!is_array($content)) {
            throw new \InvalidArgumentException('bad json flow!');
        }

        return $content;
    }

    /**
     * @param string|\Symfony\Component\Form\FormTypeInterface $type
     * @param null                                             $data
     * @param array                                            $options
     *
     * @return \Symfony\Component\Form\Form
     */
    public function createForm($type, $data = null, array $options = array())
    {
        $options = $this->upgradeFormOptions($options);

        return $this->container->get('form.factory')->create($type, $data, $options);
    }

    /**
     * Get request data with control for required field
     *
     *
     * @param Request $request
     * @param $dataName
     * @param $type
     * @param bool $required
     * @param null $defaultValue
     * @return null
     * @throws \InvalidArgumentException
     */
    protected function getRequestData(Request $request, $dataName, $type, $required = true, $defaultValue = null)
    {
        $types = array('json', 'query', 'files', 'request');

        if ($type == 'json') {
            $json = $this->getJsonContent($request);
            if (isset($json[$dataName])) {
                return $json[$dataName];
            }
        } elseif (in_array($type,$types)) {
            if ($request->$type->has($dataName)) {
                if($request->$type->get($dataName) == null && $required == true && $type == 'files') {
                    throw new \InvalidArgumentException($dataName . ' file is empty ');
                } else {
                    return $request->$type->get($dataName);
                }
            }
        } else {
            throw new \InvalidArgumentException('unknown type ' . $type);
        }

        if ($required) {
            throw new \InvalidArgumentException('parameter ' . $dataName . ' is missing in request!');
        } else {
            return $defaultValue;
        }
    }

    /**
     * no csrf token protection for REST API.
     *
     * @param $options
     *
     * @return mixed
     */
    protected function upgradeFormOptions($options)
    {
        $res = $options;
        unset($res['crsf_protection']);
        $res['csrf_protection'] = false;

        return $res;
    }

    /**
     * @param \Exception $e
     *
     * @return JsonResponse
     */
    protected function sendInternalError(\Exception $e)
    {
        $resp = $this->sendJsonResponse(array('error' => $e->getMessage(), 'class' => get_class($e)), 500); //var_dump($e->getMessage());
        return $resp;
    }
}
<commit_msg>add @todo in SwaggerApibundle::getRequestdata phpDocs<commit_after><?php

namespace Enneite\SwaggerBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;

/**
 * Class AbstractController.
 */
class SwaggerApiController extends Controller
{
    /**
     * @param array $data
     * @param int   $status
     *
     * @return JsonResponse
     */
    protected function sendJsonResponse(array $data, $status = 200)
    {
        $response = new JsonResponse($data);
        $response->setStatusCode($status);

        return $response;
    }

    /**
     * @param Request $request
     *
     * @throws \InvalidArgumentException
     *
     * @return array
     */
    protected function getJsonContent(Request $request)
    {
        $content = json_decode($request->getContent(), true);
        if (!is_array($content)) {
            throw new \InvalidArgumentException('bad json flow!');
        }

        return $content;
    }

    /**
     * @param string|\Symfony\Component\Form\FormTypeInterface $type
     * @param null                                             $data
     * @param array                                            $options
     *
     * @return \Symfony\Component\Form\Form
     */
    public function createForm($type, $data = null, array $options = array())
    {
        $options = $this->upgradeFormOptions($options);

        return $this->container->get('form.factory')->create($type, $data, $options);
    }

    /**
     * Get request data with control for required field
     * @todo : add unit test
     *
     * @param Request $request
     * @param $dataName
     * @param $type
     * @param bool $required
     * @param null $defaultValue
     * @return null
     * @throws \InvalidArgumentException
     */
    protected function getRequestData(Request $request, $dataName, $type, $required = true, $defaultValue = null)
    {
        $types = array('json', 'query', 'files', 'request');

        if ($type == 'json') {
            $json = $this->getJsonContent($request);
            if (isset($json[$dataName])) {
                return $json[$dataName];
            }
        } elseif (in_array($type,$types)) {
            if ($request->$type->has($dataName)) {
                if($request->$type->get($dataName) == null && $required == true && $type == 'files') {
                    throw new \InvalidArgumentException($dataName . ' file is empty ');
                } else {
                    return $request->$type->get($dataName);
                }
            }
        } else {
            throw new \InvalidArgumentException('unknown type ' . $type);
        }

        if ($required) {
            throw new \InvalidArgumentException('parameter ' . $dataName . ' is missing in request!');
        } else {
            return $defaultValue;
        }
    }

    /**
     * no csrf token protection for REST API.
     *
     * @param $options
     *
     * @return mixed
     */
    protected function upgradeFormOptions($options)
    {
        $res = $options;
        unset($res['crsf_protection']);
        $res['csrf_protection'] = false;

        return $res;
    }

    /**
     * @param \Exception $e
     *
     * @return JsonResponse
     */
    protected function sendInternalError(\Exception $e)
    {
        $resp = $this->sendJsonResponse(array('error' => $e->getMessage(), 'class' => get_class($e)), 500); //var_dump($e->getMessage());
        return $resp;
    }
}

{'fraction_non_alphanumeric': 0.1348553601815088, 'fraction_numerical': 0.0017016449234259785, 'mean_word_length': 1.991094147582697, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1346952', 'n_tokens_mistral': 1933, 'n_tokens_neox': 1881, 'n_words': 426}