<?php
namespace OfficeBrain\Bundle\CoreBundle\Manager\Service;

use Symfony\Component\DependencyInjection\ContainerInterface;
class InstanceOperationManager {
	
	/**
	 * 
	 * @var ContainerInterface
	 */
	private $container;
	
	/**
	 * 
	 * @var EntityManager
	 */
	private $em;
	
	/**
	 * 
	 * @param ServiceContainer $container
	 */
	public function __construct(ContainerInterface $container) {
		
		$this->container = $container;
        $this->em = $this->container->get('doctrine.orm.default_entity_manager');
	}
	
	private function initCategory() {
		
		$this->productCategoryMappingRepo = $this->em->getRepository('OfficeBrainProductBundle:ProductCategoryMapping');
		$this->categoryMappingRepo = $this->em->getRepository('OfficeBrainCategoryBundle:CategoryMapping');
	}
	
	/**
	 * 
	 * @param array $catgeoryArray
	 */
	public function loadCategories($categoryArray) {
		
		$this->initCategory();
		
		//Insert in Category Mapping
		foreach ($categoryArray as $category) {
			$this->categoryMappingRepo->insertCategoryMapping($category['category_id'], 0, null);
		}
	}
	
	/**
	 * 
	 * @param array $productCategoryArray
	 */
	public function loadProductCategories($productCategoryArray) {
		
		$this->initCategory();
		
		//Insert in Product Catgeory Mapping
		foreach ($productCategoryArray as $productCategory) {
			$this->productCategoryMappingRepo->insertProductCategoryMapping($productCategory['category_id'], $productCategory['product_id'], $productCategory['country_id']);
		}
	}
}