<commit_before><?php
/*
 * This file is part of the BaseApi package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace Onema\BaseApiBundle\EventListener;

use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;

use Doctrine\DBAL\DBALException;
use \PDOException;
use \RuntimeException;

use Onema\BaseApiBundle\Event\ApiProcessEvent;

/**
 * @author  <NAME> <<EMAIL>>
 */
class RepositoryActionListener
{
    private $arguments;
    private $method;
    
    public function __construct($method = null, $arguments = array()) {
        $this->method = $method;
        $this->arguments = $arguments;
    }
    
    /**
     * This method should be called when more than one object is expected. The 
     * collection of data will be moved/converted over to an array. 
     * Doctrine ORM doesn't require this conversion, but Doctrine MongoDB ODM 
     * does.
     * 
     * @param \Onema\BaseApiBundle\Event\ApiProcessEvent $event
     */
    public function onFindCollection(ApiProcessEvent $event)
    {
        $documents = $this->execute($event);
        $collection = $this->convertDocumentCollection($documents);
        $event->setReturnData($collection);
    }
    
    /**
     * This method should be called when a single result is expected. 
     * 
     * @param \Onema\BaseApiBundle\Event\ApiProcessEvent $event
     * @deprecated since version 0.2.0
     */
    public function onFindOne(ApiProcessEvent $event)
    {
        $document = $this->execute($event);
        // No data should return a 404 
        if(empty($documents)) {
            throw new ResourceNotFoundException('Could not find resource', 404);
        }
        $event->setReturnData($document);
    }
    
    
    public function onCall(ApiProcessEvent $event)
    {
        $this->method = $event->getMethod();
        $this->arguments = $event->getArguments();
        
        $document = $this->execute($event);
        $event->setReturnData($document);
    }
    
    /**
     * JMS Serializer doesn't play well with doctrine ODM Cursor objects. this is a 
     * utility method that will put this collection into a siple array. 
     * @param type $documents
     * @return array
     */<commit_msg>Fixed typo<commit_after><?php
/*
 * This file is part of the BaseApi package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace Onema\BaseApiBundle\EventListener;

use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;

use Doctrine\DBAL\DBALException;
use \PDOException;
use \RuntimeException;

use Onema\BaseApiBundle\Event\ApiProcessEvent;

/**
 * @author  <NAME> <<EMAIL>>
 */
class RepositoryActionListener
{
    private $arguments;
    private $method;
    
    public function __construct($method = null, $arguments = array()) {
        $this->method = $method;
        $this->arguments = $arguments;
    }
    
    /**
     * This method should be called when more than one object is expected. The 
     * collection of data will be moved/converted over to an array. 
     * Doctrine ORM doesn't require this conversion, but Doctrine MongoDB ODM 
     * does.
     * 
     * @param \Onema\BaseApiBundle\Event\ApiProcessEvent $event
     */
    public function onFindCollection(ApiProcessEvent $event)
    {
        $documents = $this->execute($event);
        $collection = $this->convertDocumentCollection($documents);
        $event->setReturnData($collection);
    }
    
    /**
     * This method should be called when a single result is expected. 
     * 
     * @param \Onema\BaseApiBundle\Event\ApiProcessEvent $event
     * @deprecated since version 0.2.0
     */
    public function onFindOne(ApiProcessEvent $event)
    {
        $document = $this->execute($event);
        
        // No data should return a 404 
        if(empty($document)) {
            throw new ResourceNotFoundException('Could not find resource', 404);
        }
        else {
            $event->setReturnData($document);
        }
    }
    
    
    public function onCall(ApiProcessEvent $event)
    {
        $this->method = $event->getMethod();
        $this->arguments = $event->getArguments();
        
        $document = $this->execute($event);
        $event->setReturnData($document);
    }
    
    /**
     * JMS Serializer doesn't play well with doctrine ODM Cursor objects. this is a 
     * utility method that will put this collection into a siple array. 
     * @param type $documents
     * @return array
     */
{'fraction_non_alphanumeric': 0.10549717057396928, 'fraction_numerical': 0.003637833468067906, 'mean_word_length': 2.749242424242424, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 41, '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': '2754390', 'n_tokens_mistral': 1389, 'n_tokens_neox': 1349, 'n_words': 384}