public function resolveShips(Faction $faction, $args)
 {
     //The old way
     //$ships = $faction->getShips()->toArray();
     //$connection = ConnectionBuilder::connectionFromArray($ships, $args);
     //$connection->sliceSize = count($connection->edges);
     //return $connection;
     /** @var ShipRepository $repository */
     $repository = $this->container->get('doctrine.orm.default_entity_manager')->getRepository('AppBundle:Ship');
     $arrayLength = $repository->countAllByFactionId($faction->getId());
     //--------------------------------------------------------------------------------------------------------------
     //todo move in vendor ?
     $beforeOffset = ConnectionBuilder::getOffsetWithDefault($args['before'], $arrayLength);
     $afterOffset = ConnectionBuilder::getOffsetWithDefault($args['after'], -1);
     $startOffset = max($afterOffset, -1) + 1;
     $endOffset = min($beforeOffset, $arrayLength);
     if (is_numeric($args['first'])) {
         $endOffset = min($endOffset, $startOffset + $args['first']);
     }
     if (is_numeric($args['last'])) {
         $startOffset = max($startOffset, $endOffset - $args['last']);
     }
     $offset = max($startOffset, 0);
     $limit = $endOffset - $startOffset;
     //--------------------------------------------------------------------------------------------------------------
     $ships = $repository->retrieveShipsByFactionId($faction->getId(), $offset, $limit);
     $connection = ConnectionBuilder::connectionFromArraySlice($ships, $args, ['sliceStart' => $offset, 'arrayLength' => $arrayLength]);
     $connection->sliceSize = count($ships);
     return $connection;
 }
 private function loadFaction(ObjectManager $manager)
 {
     $faction = new Faction();
     $faction->fromArray(['id' => '3', 'name' => 'Fake Faction', 'type' => Faction::TYPE_FAKE]);
     $manager->persist($faction);
     $metadata = $manager->getClassMetaData(get_class($faction));
     $metadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_NONE);
     $manager->flush();
     return $faction;
 }
 private function loadFactions(ObjectManager $manager, array $factions)
 {
     foreach ($factions as $data) {
         $ships = $data['ships'];
         unset($data['ships']);
         $faction = new Faction();
         $faction->fromArray($data);
         foreach ($ships as $shipId) {
             $faction->addShip($this->ships[$shipId]);
         }
         $manager->persist($faction);
         $metadata = $manager->getClassMetaData(get_class($faction));
         $metadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_NONE);
         $manager->flush();
     }
 }
 /**
  * @param OutputInterface $output
  */
 private function getLocations(OutputInterface $output)
 {
     $output->writeln('<info>Starting to get Locations</info>');
     $output->writeln('<info>URL: ' . $this->url . 'Locations+Overview</info>');
     $html = file_get_contents($this->url . 'Locations+Overview');
     $crawler = new Crawler($html);
     /* Get Regions by Faction */
     $factionRegions = $crawler->filter('.span_1_of_4');
     foreach ($factionRegions as $domElement) {
         /* @var \DOMElement $domElement */
         $content = trim($domElement->textContent);
         $locations = array_map('trim', explode("\n", $content));
         $output->writeln('<info>Getting Factions</info>');
         $faction = $this->em->getRepository('AppBundle:Faction')->findOneBy(array('name' => $locations[0]));
         $output->writeln('<info>Faction: ' . $locations[0] . '</info>');
         if (!$faction instanceof Faction) {
             $faction = new Faction();
             $faction->setName($locations[0]);
             $this->em->persist($faction);
         }
         unset($locations[0]);
         $output->writeln('<info>Getting Regions</info>');
         foreach ($locations as $r) {
             $output->writeln('<info>Region: ' . $r . '</info>');
             $region = $this->em->getRepository('AppBundle:Region')->findOneBy(array('name' => $r));
             if (!$region instanceof Region) {
                 $region = new Region();
                 $region->setName($r);
                 $region->setFaction($faction);
                 $this->em->persist($region);
             }
         }
     }
     $this->em->flush();
     /* Get Region by Province */
     $provinceRegions = $crawler->filter('table.wiki_table');
     $output->writeln('<info>Getting provinces</info>');
     $provincesArray = array();
     foreach ($provinceRegions->children() as $domElement) {
         $output->writeln('<info>Making a Province Array</info>');
         /* @var \DOMElement $domElement */
         $content = trim($domElement->textContent);
         $contentArray = array_map('trim', explode("\n", $content));
         $index = $contentArray[0];
         unset($contentArray[0]);
         if ($index != 'Province') {
             $provincesArray[$index] = array_map('trim', explode(',', $contentArray[1]));
         }
     }
     $output->writeln('<info>Saving Factions</info>');
     foreach ($provincesArray as $prov => $regions) {
         $output->writeln('<info>' . $prov . '</info>');
         $province = $this->em->getRepository('AppBundle:Province')->findOneBy(array('name' => $prov));
         if (!$province instanceof Province) {
             $province = new Province();
             $province->setName($prov);
             $this->em->persist($province);
         }
         foreach ($regions as $r) {
             $output->writeln('<info>' . $r . '</info>');
             $region = $this->em->getRepository('AppBundle:Region')->findOneBy(array('name' => $r));
             if (!$region instanceof Region) {
                 $region = new Region();
                 $region->setName($r);
             }
             $region->setProvince($province);
             $this->em->persist($region);
         }
     }
     $this->em->flush();
     $output->writeln('<info>We\'re done.</info>');
 }