<commit_before><?php

namespace AppBundle\Controller;

use AppBundle\Entity as EntityDir;
use AppBundle\Service\CsvUploader;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use \Doctrine\Common\Util\Debug as doctrineDebug;
use Symfony\Component\HttpFoundation\Response;

/**
 * @Route("/casrec")
 */
class CasRecController extends RestController
{
    /**
     * @Route("/truncate")
     * @Method({"DELETE"})
     */
    public function truncateTable(Request $request)
    {
        $this->denyAccessUnlessGranted(EntityDir\User::ROLE_ADMIN);

        $em = $this->getEntityManager();
        $em->getConnection()->query('TRUNCATE TABLE casrec');

        return ['truncated'=>true];
    }

    /**
     * Bulk insert
     * To call multiple times in chunks of maximum 10k records, otherwise failing deu to memory reasons.
     * Currently used from admin area via a ajax uploader and multiple requests (after an initial truncate)
     *
     * @Route("/bulk-add")
     * @Method({"POST"})
     */
    public function addBulk(Request $request)
    {
        $this->denyAccessUnlessGranted(EntityDir\User::ROLE_ADMIN);

        ini_set('memory_limit', '1024M');

        $data = CsvUploader::decompressData($request->getContent());

        $ret = $this->get('casrec_service')->addBulk($data);

        return $ret;
    }


    /**
     * Verify Deputy & Client last names, Postcode, and Case Number
     *
     * @Route("/verify")
     * @Method({"POST"})
     */
    public function verify(Request $request)
    {
        $clientData = $this->deserializeBodyContent($request);
        $user = $this->getUser();

        $casrecVerified = $this->container->get('opg_digideps.casrec_verification_service')
            ->validate ( $clientData['case_number']
                       , $clientData['lastname']
                       , $user->getLastname()
                       , $user->getAddressPostcode()
            );

        return ['verified' => $casrecVerified];
    }

    /**
     * @Route("/count")
     * @Method({"GET"})
     */
    public function userCount()
    {
        $this->denyAccessUnlessGranted(EntityDir\User::ROLE_ADMIN);

        $qb = $this->getDoctrine()->getManager()->createQueryBuilder();
        $qb->select('count(c.id)');
        $qb->from('AppBundle\Entity\CasRec', 'c');

        $count = $qb->getQuery()->getSingleScalarResult();

        return $count;
    }

    /**
     * Return CSV file created on the fly
     *
     * @Route("/stats.csv")
     * @Method({"GET"})
     */
    public function getStatsCsv(Request $request)
    {
        $this->denyAccessUnlessGranted(EntityDir\User::ROLE_ADMIN);

        // create CSV if not added by the cron, or the "regenerated" is added
        if (!file_exists(EntityDir\CasRec::STATS_FILE_PATH) || $request->get('regenerate')) {
            $this->get('casrec_service')->saveCsv(EntityDir\CasRec::STATS_FILE_PATH);
        }

        $response = new Response();
        $response->setContent(readfile(EntityDir\CasRec::STATS_FILE_PATH));

        return $response;<commit_msg>DDPB-1720 fix readfile output<commit_after><?php

namespace AppBundle\Controller;

use AppBundle\Entity as EntityDir;
use AppBundle\Service\CsvUploader;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Request;
use \Doctrine\Common\Util\Debug as doctrineDebug;
use Symfony\Component\HttpFoundation\Response;

/**
 * @Route("/casrec")
 */
class CasRecController extends RestController
{
    /**
     * @Route("/truncate")
     * @Method({"DELETE"})
     */
    public function truncateTable(Request $request)
    {
        $this->denyAccessUnlessGranted(EntityDir\User::ROLE_ADMIN);

        $em = $this->getEntityManager();
        $em->getConnection()->query('TRUNCATE TABLE casrec');

        return ['truncated'=>true];
    }

    /**
     * Bulk insert
     * To call multiple times in chunks of maximum 10k records, otherwise failing deu to memory reasons.
     * Currently used from admin area via a ajax uploader and multiple requests (after an initial truncate)
     *
     * @Route("/bulk-add")
     * @Method({"POST"})
     */
    public function addBulk(Request $request)
    {
        $this->denyAccessUnlessGranted(EntityDir\User::ROLE_ADMIN);

        ini_set('memory_limit', '1024M');

        $data = CsvUploader::decompressData($request->getContent());

        $ret = $this->get('casrec_service')->addBulk($data);

        return $ret;
    }


    /**
     * Verify Deputy & Client last names, Postcode, and Case Number
     *
     * @Route("/verify")
     * @Method({"POST"})
     */
    public function verify(Request $request)
    {
        $clientData = $this->deserializeBodyContent($request);
        $user = $this->getUser();

        $casrecVerified = $this->container->get('opg_digideps.casrec_verification_service')
            ->validate ( $clientData['case_number']
                       , $clientData['lastname']
                       , $user->getLastname()
                       , $user->getAddressPostcode()
            );

        return ['verified' => $casrecVerified];
    }

    /**
     * @Route("/count")
     * @Method({"GET"})
     */
    public function userCount()
    {
        $this->denyAccessUnlessGranted(EntityDir\User::ROLE_ADMIN);

        $qb = $this->getDoctrine()->getManager()->createQueryBuilder();
        $qb->select('count(c.id)');
        $qb->from('AppBundle\Entity\CasRec', 'c');

        $count = $qb->getQuery()->getSingleScalarResult();

        return $count;
    }

    /**
     * Return CSV file created on the fly
     *
     * @Route("/stats.csv")
     * @Method({"GET"})
     */
    public function getStatsCsv(Request $request)
    {
        $this->denyAccessUnlessGranted(EntityDir\User::ROLE_ADMIN);

        // create CSV if not added by the cron, or the "regenerated" is added
        if (!file_exists(EntityDir\CasRec::STATS_FILE_PATH) || $request->get('regenerate')) {
            $this->get('casrec_service')->saveCsv(EntityDir\CasRec::STATS_FILE_PATH);
        }

        $response = new BinaryFileResponse(EntityDir\CasRec::STATS_FILE_PATH);

        return $response;
{'fraction_non_alphanumeric': 0.14462163852407756, 'fraction_numerical': 0.0025015634771732333, 'mean_word_length': 2.798693586698337, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 62, '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': '5822135', 'n_tokens_mistral': 1981, 'n_tokens_neox': 1875, 'n_words': 343}