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

namespace CampaignChain\CoreBundle\EntityService;

use Doctrine\ORM\EntityManager;
use Symfony\Component\DependencyInjection\ContainerInterface;

class HookService
{
    protected $em;
    protected $container;
    protected $view = 'default';


    public function __construct(EntityManager $em, ContainerInterface $container)
    {
        $this->em = $em;
        $this->container = $container;
    }

    public function setView($view){
        $this->view = $view;
    }

    public function getHooks($bundleName, $configIdentifier){
        $bundle = $this->em->getRepository('CampaignChainCoreBundle:Bundle')->findOneByName($bundleName);
        switch($bundle->getType()){
            case 'campaignchain-campaign':
                $repositoryName = 'CampaignChainCoreBundle:CampaignModule';
                break;
            case 'campaignchain-milestone':
                $repositoryName = 'CampaignChainCoreBundle:MilestoneModule';
                break;
            case 'campaignchain-activity':
                $repositoryName = 'CampaignChainCoreBundle:ActivityModule';
                break;
            default:
                // TODO: Throw exception.
                die('No repository defined for bundle type "'.$bundle->getType().'"');
                break;
        }

        $config = $this->em->getRepository($repositoryName)->findOneBy(array(
            'bundle' => $bundle,
            'identifier' => $configIdentifier,
        ));

        return $config->getHooks()[$this->view];
    }

    // TODO: Do we have to pass the $repository object or could we use $this->em just as well?
    public function processHooks($bundleName, $configIdentifier, $entity, $data, $new = false)
    {
        $hooks = $this->getHooks($bundleName, $configIdentifier);

        if(is_array($hooks) && count($hooks)){
            foreach($hooks as $identifier => $active){
                if($active){
                    $hookModule = $this->em->getRepository('CampaignChainCoreBundle:Hook')->findOneByIdentifier($identifier);
                    $hookService = $this->container->get($hookModule->getServices()['entity']);

                    // If the entity is new and the hook is of type "trigger", then define it for the entity.
                    if($new && $hookModule->getType() == 'trigger'){
                        $entity->setTriggerHook($hookModule);
                    }

                    if(is_array($data)){
                        $hookDataArray = $data['campaignchain_hook_'.str_replace('-', '_', $identifier)];
                        $hook = $hookService->arrayToObject($hookDataArray);
                    } elseif(is_object($data) && get_class($data) == 'Symfony\Component\Form\Form'){
                        $hook = $data->get('campaignchain_hook_'.str_replace('-', '_', $identifier))->getData();
                    }

                    $entity = $hookService->processHook($entity, $hook);
                }
            }
        }

        // Post process entity per campaign.
        if(
            strpos(get_class($entity), 'CoreBundle\Entity\Campaign') === false &&
            $entity->getCampaign()->getCampaignModule()->getServices() &&
            is_array($entity->getCampaign()->getCampaignModule()->getServices())
        ){
            $campaignModuleServices = $entity->getCampaign()->getCampaignModule()->getServices();
            if(isset($campaignModuleServices['hook'])) {
                $campaignModuleService = $this->container->get($campaignModuleServices['hook']);
                $entity = $campaignModuleService->processAction($entity);
            }
        }

        return $entity;
    }

    public function getHook($identifier)
    {
        $hook = $this->em->getRepository('CampaignChainCoreBundle:Hook')->findOneByIdentifier($identifier);

        if(!$hook){
            throw new \Exception('No hook with identifier '.$identifier);<commit_msg>CampaignChain/campaignchain-ce#78 Undefined index: campaignchain_hook_campaignchain_image error when editing Facebook Activity in Modal<commit_after><?php
/*
 * This file is part of the CampaignChain package.
 *
 * (c) CampaignChain, Inc. <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CampaignChain\CoreBundle\EntityService;

use Doctrine\ORM\EntityManager;
use Symfony\Component\DependencyInjection\ContainerInterface;

class HookService
{
    protected $em;
    protected $container;
    protected $view = 'default';


    public function __construct(EntityManager $em, ContainerInterface $container)
    {
        $this->em = $em;
        $this->container = $container;
    }

    public function setView($view){
        $this->view = $view;
    }

    public function getHooks($bundleName, $configIdentifier){
        $bundle = $this->em->getRepository('CampaignChainCoreBundle:Bundle')->findOneByName($bundleName);
        switch($bundle->getType()){
            case 'campaignchain-campaign':
                $repositoryName = 'CampaignChainCoreBundle:CampaignModule';
                break;
            case 'campaignchain-milestone':
                $repositoryName = 'CampaignChainCoreBundle:MilestoneModule';
                break;
            case 'campaignchain-activity':
                $repositoryName = 'CampaignChainCoreBundle:ActivityModule';
                break;
            default:
                // TODO: Throw exception.
                die('No repository defined for bundle type "'.$bundle->getType().'"');
                break;
        }

        $config = $this->em->getRepository($repositoryName)->findOneBy(array(
            'bundle' => $bundle,
            'identifier' => $configIdentifier,
        ));

        return $config->getHooks()[$this->view];
    }

    // TODO: Do we have to pass the $repository object or could we use $this->em just as well?
    public function processHooks($bundleName, $configIdentifier, $entity, $data, $new = false)
    {
        $hooks = $this->getHooks($bundleName, $configIdentifier);

        if(is_array($hooks) && count($hooks)){
            foreach($hooks as $identifier => $active){
                if($active){
                    $hookModule = $this->em->getRepository('CampaignChainCoreBundle:Hook')->findOneByIdentifier($identifier);
                    $hookService = $this->container->get($hookModule->getServices()['entity']);

                    // If the entity is new and the hook is of type "trigger", then define it for the entity.
                    if($new && $hookModule->getType() == 'trigger'){
                        $entity->setTriggerHook($hookModule);
                    }

                    $hasHookData = false;

                    if(is_array($data)){
                        // Check if data for specific Hook is available.
                        if(isset($data['campaignchain_hook_'.str_replace('-', '_', $identifier)])){
                            $hookDataArray = $data['campaignchain_hook_'.str_replace('-', '_', $identifier)];
                            $hook = $hookService->arrayToObject($hookDataArray);

                            $hasHookData = true;
                        }
                    } elseif(is_object($data) && get_class($data) == 'Symfony\Component\Form\Form'){
                        $hook = $data->get('campaignchain_hook_'.str_replace('-', '_', $identifier))->getData();

                        $hasHookData = true;
                    }

                    if($hasHookData){
                        $entity = $hookService->processHook($entity, $hook);
                    }
                }
            }
        }

        // Post process entity per campaign.
        if(
            strpos(get_class($entity), 'CoreBundle\Entity\Campaign') === false &&
            $entity->getCampaign()->getCampaignModule()->getServices() &&
            is_array($entity->getCampaign()->getCampaignModule()->getServices())
        ){
            $campaignModuleServices = $entity->getCampaign()->getCampaignModule()->getServices();
            if(isset($campaignModuleServices['hook'])) {
                $campaignModuleService = $this->container->get($campaignModuleServices['hook']);
                $entity = $campaignModuleService->processAction($entity);
            }
        }

        return $entity;
    }

    public function getHook($identifier)
    {
        $hook = $this->em->getRepository('CampaignChainCoreBundle:Hook')->findOneByIdentifier($identifier);

        if(!$hook){
            throw new \Exception('No hook with identifier '.$identifier);
{'fraction_non_alphanumeric': 0.13090950022512382, 'fraction_numerical': 0.00022512381809995497, 'mean_word_length': 2.2087396171903215, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 97, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4305781', 'n_tokens_mistral': 2275, 'n_tokens_neox': 2177, 'n_words': 452}