<?php
// src/AppBundle/DataFixtures/ORM/LoadData.php

namespace AppBundle\DataFixtures\ORM;

use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use AppBundle\Entity\Sector;
use AppBundle\Entity\Canton;
use AppBundle\Entity\User;


class LoadData extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface
{
	 /**
	 * @var ContainerInterface
	 */
	private $container;
  
	public function setContainer(ContainerInterface $container = null)
	{
		$this->container = $container;
	}

	
	  // Dans l'argument de la méthode load, l'objet $manager est l'EntityManager
	public function load(ObjectManager $manager)
	{
		$cantons = array(
			'Valais',
			'Jura',
			'Vaud',
			'Berne',
			'Genève',
			'Fribourg',
			'Neuchâtel'
		);
		
		$secteurs = array(
			'Biotech',
			'Medtech',
			'Service',
			'Nutrition',
			'Pharma',
			'Cosmétique',
			'Fondation/Association',
			'Incubateur',
			'Uni/Hôp',
			'Investisseur'
		);
		
		
		$userManager = $this->container->get('fos_user.user_manager');
		$admin = new User();
		  $admin->setUsername("admin");
		  $admin->setPlainPassword('admin');
		  $admin->setEmail("admin@admin.fr");
		  $admin->setEnabled(true);
		  $admin->addRole('ROLE_SUPER_ADMIN');
		  // On la persiste
		  $userManager->updateUser($admin, true);
		  
		  $manager->persist($admin);
		
		$i=0;
		foreach ($cantons as $name) {
		  // On crée la catégorie
		  $canton = new Canton();
		  $canton->setMarkerId($i);
		  $canton->setName($name);
	
		  // On la persiste
		  $manager->persist($canton);
		  $this->addReference('c'.$i, $canton);
		  $i++;
		}
		$i=0;
		foreach ($secteurs as $name) {
		  // On crée la catégorie
		  $sector = new Sector();
		  $sector->setMarkerId($i);
		  $sector->setName($name);
	
		  // On la persiste
		  $manager->persist($sector);
		  $this->addReference('s'.$i, $sector);
		  $i++;
		}
	
		// On déclenche l'enregistrement de toutes les data
		$manager->flush();
			
	}

    public function getOrder()
    {
        // the order in which fixtures will be loaded
        // the lower the number, the sooner that this fixture is loaded
        return 1;
    }
 }

