コード例 #1
0
ファイル: ShowFixtures.php プロジェクト: dstansby/camdram
 /**
  * {@inheritDoc}
  */
 public function load(ObjectManager $manager)
 {
     $file = __DIR__ . '/../../Resources/data/shows.yml';
     $plays = Yaml::parse(file_get_contents($file));
     $max = count($plays) - 1;
     mt_srand(microtime(true));
     $start_date = new \DateTime('-6 months');
     $end_date = new \DateTime('+18 months');
     $diff = $end_date->diff($start_date, true);
     $total_weeks = $diff->days / 7;
     $this->person_repo = $manager->getRepository('ActsCamdramBundle:Person');
     $this->people_ids = array_map(function ($val) {
         return $val['id'];
     }, $this->person_repo->createQueryBuilder('p')->select('p.id')->getQuery()->getArrayResult());
     $this->venue_repo = $manager->getRepository('ActsCamdramBundle:Venue');
     $this->venue_ids = array_map(function ($val) {
         return $val['id'];
     }, $this->venue_repo->createQueryBuilder('v')->select('v.id')->getQuery()->getArrayResult());
     $this->society_repo = $manager->getRepository('ActsCamdramBundle:Society');
     $this->society_ids = array_map(function ($val) {
         return $val['id'];
     }, $this->society_repo->createQueryBuilder('s')->select('s.id')->getQuery()->getArrayResult());
     for ($i = 0; $i < 250; $i++) {
         $show = new Show();
         $play = $plays[mt_rand(0, $max)];
         $show->setName($play['name']);
         $show->setDescription($play['description']);
         $show->setAuthor($play['author']);
         $show->setCategory($play['category']);
         $show->setAuthorisedBy($this->getReference('adminuser'));
         $this->allocateSociety($show);
         $start = clone $start_date;
         $start->modify('+' . mt_rand(0, $total_weeks) . ' weeks');
         $manager->persist($show);
         //Use a random number to decide what sort of show it is...
         $type = mt_rand(0, 8);
         switch ($type) {
             case 0:
             case 1:
             case 2:
             case 3:
             case 4:
                 //A week-long run of a show
                 $offset = mt_rand(1, 3);
                 $start->modify('+' . $offset . ' days');
                 $end = clone $start;
                 $end->modify('+' . mt_rand(3, 7 - $offset) . ' days');
                 $p = $this->generatePerformance($show, $start, $end);
                 $this->allocateVenue($show);
                 $show->addPerformance($p);
                 $manager->persist($p);
                 break;
             case 5:
                 //A one-night show
                 $start->modify('+' . mt_rand(0, 6) . ' days');
                 $p = $this->generatePerformance($show, $start, $start);
                 $manager->persist($p);
                 $this->allocateVenue($show);
                 $show->addPerformance($p);
                 break;
             case 6:
                 //a muli-venue tour
                 $num_perfs = mt_rand(2, 9);
                 $start->modify('+' . mt_rand(0, 5) . ' days');
                 for ($j = 0; $j <= $num_perfs; $j++) {
                     $start->modify('+' . mt_rand(1, 3) . ' days');
                     $p = $this->generatePerformance($show, clone $start, clone $start);
                     $p->setShow($show);
                     $this->allocateVenue($p);
                     $show->addPerformance($p);
                     $manager->persist($p);
                 }
                 break;
             case 7:
             case 8:
                 //a multi-week show
                 $offset = mt_rand(1, 3);
                 $start->modify('+' . $offset . ' days');
                 $end = clone $start;
                 $end->modify('+' . mt_rand(3, 7 - $offset) . ' days');
                 $this->allocateVenue($show);
                 for ($j = 0; $j <= 2; $j++) {
                     $start->modify('+1 week');
                     $end->modify('+1 week');
                     $p = $this->generatePerformance($show, clone $start, clone $end);
                     $p->setShow($show);
                     $show->addPerformance($p);
                     $manager->persist($p);
                 }
                 break;
         }
         $roles = $this->generateRoles($play['characters'], $play['category'] == 'musical');
         foreach ($roles as $role) {
             $show->addRole($role);
             $role->setShow($show);
             $manager->persist($role);
         }
         if (mt_rand(0, 1) == 1) {
             $this->addAuditions($manager, $show);
         }
         if (mt_rand(0, 1) == 1) {
             $this->addApplications($manager, $show);
         }
         if (mt_rand(0, 1) == 1) {
             $this->addTechieAdverts($manager, $show);
         }
     }
     $manager->flush();
 }