/**
  * Import Projects
  */
 private function importProjects()
 {
     $this->output->write(sprintf('%-30s', 'Importing projects'));
     $previewProjects = $this->previewService->getProjects();
     $created = 0;
     $updated = 0;
     foreach ($previewProjects as $previewProject) {
         /**
          * @var Project $project
          */
         $project = $this->entityManager->getRepository('ProjectPreviewProjectBundle:Project')->find($previewProject->id);
         if (is_null($project)) {
             $project = new Project();
             $project->setId($previewProject->id);
             $created++;
         } else {
             $updated++;
         }
         $project->setName($previewProject->name);
         $project->setArchived($previewProject->archived);
         $this->entityManager->persist($project);
     }
     $this->output->writeln(sprintf('done (total: % 4d, created: % 4d, updated: % 4d)', $created + $updated, $created, $updated));
     $this->entityManager->flush();
 }
 /**
  * Import Users
  */
 private function importUsers()
 {
     $this->output->write(sprintf('%-30s', 'Importing users'));
     $previewUsers = $this->previewService->getUsers();
     $created = 0;
     $updated = 0;
     foreach ($previewUsers as $previewUser) {
         if (!strlen($previewUser->email)) {
             continue;
         }
         /**
          * @var User $user
          */
         $user = $this->entityManager->getRepository('ProjectPreviewUserBundle:User')->find($previewUser->id);
         if (is_null($user)) {
             $user = new User();
             $user->setId($previewUser->id);
             $user->setPassword('toto');
             $created++;
         } else {
             $updated++;
         }
         $user->setEmail($previewUser->email);
         $user->setUsername($previewUser->email);
         $user->setLastname($previewUser->lastname);
         $user->setFirstname($previewUser->firstname);
         if (isset($previewUser->avatar->urls->avatar_xs)) {
             $user->setAvatar($previewUser->avatar->urls->avatar_xs);
         }
         $this->entityManager->persist($user);
     }
     $this->output->writeln(sprintf('done (total: % 4d, created: % 4d, updated: % 4d)', $created + $updated, $created, $updated));
     $this->entityManager->flush();
 }
 /**
  * @return array
  */
 private function getProjects()
 {
     $list = $this->api->getProjects();
     $projects = [];
     foreach ($list as $project) {
         $projects[$project->name] = $project->id;
     }
     return $projects;
 }
 /**
  * @param Ticket $ticket
  */
 private function importTicketActivities(Ticket $ticket)
 {
     $activities = $this->previewService->getTicketActivity($ticket->getProject()->getId(), $ticket->getId());
     $activitiesDb = $ticket->getActivities();
     foreach ($activities as $activity) {
         if ($activity->spread_identifier != 'UserChangeStateIssue') {
             continue;
         }
         $create = true;
         foreach ($activitiesDb as $activityDb) {
             if ($activity->spread_identifier == $activityDb->getAction() && $activity->created == $activityDb->getCreated()->getTimestamp() && $activity->value == $activityDb->getValue()) {
                 $create = false;
                 break;
             }
         }
         if ($create == true) {
             $author = $this->entityManager->getRepository('ProjectPreviewUserBundle:User')->find($activity->author->id);
             $activityNew = new Activity();
             $activityNew->setAuthor($author);
             $activityNew->setCreated(new \DateTime('@' . $activity->created));
             $activityNew->setAction($activity->spread_identifier);
             $activityNew->setValue($activity->value);
             $activityNew->setTicket($ticket);
             $this->entityManager->persist($activityNew);
         }
         if ($ticket->getStatus() == 'Fermé' && $activity->spread_identifier == 'UserChangeStateIssue') {
             $ticket->setClosed(new \DateTime('@' . $activity->created));
             $this->entityManager->persist($ticket);
         }
     }
 }