<?php
/**
* @author OfficeBrain 4160 <info@officebrain.com>
*
* Description : Create Queue Command to add in queue of MongoDB
*
**/

namespace OfficeBrain\Bundle\QueueBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Stopwatch\Stopwatch;

class CreateProductViewAPICommand extends ContainerAwareCommand
{
    CONST COMMAND_NAME = 'office_brain_queue:queue:import:product:view:api';
    CONST RECORDS_LIMIT = 10;
    CONST SEPARATOR = "|----";
    
    /**
     * @var Stopwatch
     */
    private $stopWatch;
    private $container;
    
    /**
    * @author Employee Id: 4160
    * Description :
    * command name and add arguments for command
    *
    * @return type
    *
    **/
    protected function configure()
    {
        $this
            ->setName(self::COMMAND_NAME)
            ->setDescription('Import products in product Queue')
            ->addOption('productIds', null, InputOption::VALUE_REQUIRED, 'Ids of Product')
            ->addOption('culture', null, InputOption::VALUE_REQUIRED, 'culture');
    }
    
    public function init()
    {
        $this->container = $this->getContainer();
        $this->stopWatch = new Stopwatch();
    }

    /**
    * @author Employee Id: 4160
    * Description :
    * Execute command to add data in queue
    *
    * @return type
    *
    **/
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $this->init();
        $this->stopWatch->start(self::COMMAND_NAME);
        
        $productIds = $input->getOption('productIds');
        $culture = $input->getOption('culture');
                
        $request = $this->container->get('request');
        $request->attributes->set('object', 'Product');
        
        $queueApiManager = $this->container->get('office_brain_queue_api_manager');
        $productApiManager = $this->container->get('office_brain_product_admin_manager');
        
        $productIds = explode(",", $productIds);
        
        foreach($productIds as $productId) {
            $returnData = $productApiManager->view($productId, 'en_us', true);
            if($returnData['status'] == 'success' && !empty($queueApiManager)) {
                $queueApiManager->mapQueue('create', $returnData['data'][$productId]);
            }
        }
        
        echo "Imported product's records in product queue successfully...";
        $event = $this->stopWatch->stop(self::COMMAND_NAME);
        $output->writeln(" | Memory Usage :- " . $event->getMemory());
        $output->writeln(" | Total Time Taken :- " . $event->getDuration());
    }
}