<?php
namespace OfficeBrain\Bundle\QueueBundle\Manager\Extended;

use OfficeBrain\Bundle\QueueBundle\Manager\AbstractManager\QueueAbstract;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;

class DistributorQueueExtended extends QueueAbstract
{
    protected $container;
    protected $dm;
    
    public function __construct(Container $container, $entityParameters)
    {
        $this->container = $container;
        $this->entityParameters = $entityParameters;
        $this->dm = $container->get('doctrine_mongodb')->getManager();
        $this->repository = $this->dm->getRepository('OfficeBrainQueueBundle:DistributorQueue');
    }
    
    public function mapQueue($action, $data, $type='json')
    {
        $object = $this->request->attributes->get('object');
        $name = $this->request->attributes->get('name');
        
        $formatedData = $this->processData($data, $this->entityParameters[$object], $type);
        $this->setQueue($name, $object, $action, $formatedData);
        
        //parent::processQueue($data, $this->entityParameters[$object], $type);
    }
    
    public function setQueue($name, $object, $action, $args, $msg=null)
    {
        $queue = $this->repository->save($name, $object, $action, $args);
        return $queue;
    }
    
    public function processData($data, $configObject, $type="json")
    {
        $entityORM = new $configObject['entity']();
        $dataFields = $configObject['dataMapping'];
        $erpData = array();
        foreach ($dataFields as $key => $value) {
            $getMethod = null;
            if ($value) {
                $getMethod = 'get'.$value;
            }
            $entityValue = $getMethod ? $entityORM->$getMethod() : null;
            $erpData[$key] = gettype($entityValue) == 'object' ? $entityValue->getId() : $entityValue;
        }
        $resultData = parent::formatData($erpData, $type);
        return $resultData;
    }
}
