public function handle(\Voetbal\Command\AssociationAdd $command) { $entityManager = Service::getEntityManager(); // check if name not exists in repository $associationRepos = $entityManager->getRepository("Voetbal\\DAO\\Association"); $oDAOAssociation = $associationRepos->findOneBy(array('name' => $command->getName())); if ($oDAOAssociation !== null) { throw new \Exception("de bondsnaam " . $command->getName() . " bestaat al", E_ERROR); } $oAssociation = new Association($command->getName()); $oAssociation->setDescription($command->getDescription()); $oDAOParentAssociation = null; $oParentAssociation = $command->getParent(); if ($oParentAssociation !== null) { $oDAOParentAssociation = $associationRepos->findOneBy(array('name' => $oParentAssociation->getName())); if ($oDAOParentAssociation === null) { throw new \Exception("de hoofdbond bestaat niet", E_ERROR); } } $oAssociation->setParent($oParentAssociation); $oDAOAssociation = new DAOAssociation(); $oDAOAssociation->setName($oAssociation->getName()); $oDAOAssociation->setDescription($oAssociation->getDescription()); $oDAOAssociation->setParent($oDAOParentAssociation); $entityManager->persist($oDAOAssociation); $entityManager->flush(); // return LeagueRepository::add( $oLeague ); return $oAssociation; }
$arrConfig = parse_ini_file(__DIR__ . "/../config/voetbal.ini", true); return static::$entityManager = EntityManager::create($arrConfig["database"], $config); } public static function getBus() { $arrCommandClassToHandlerMap = array("Voetbal\\Command\\AssociationAdd" => new Command\Handler\AssociationAdd(), "Voetbal\\Command\\SeasonAdd" => new Command\Handler\SeasonAdd(), "Voetbal\\Command\\LeagueAdd" => new Command\Handler\LeagueAdd()); $handlerMiddleware = new CommandHandlerMiddleware(new ClassNameExtractor(), new InMemoryLocator($arrCommandClassToHandlerMap), new Inflector()); $lockingMiddleware = new LockingMiddleware(); return new \League\Tactician\CommandBus([$lockingMiddleware, $handlerMiddleware]); } } try { $command = new Command\AssociationAdd(new Association\Name("F.I.F.A.")); $command->setDescription(new Association\Description("wereld voetbalbond")); $oAssociationWorld = Service::getBus()->handle($command); $command = new Command\AssociationAdd(new Association\Name("U.E.F.A.")); $command->setDescription(new Association\Description("europese voetbalbond")); $command->setParent($oAssociationWorld); $oAssociationEurope = Service::getBus()->handle($command); $command = new Command\SeasonAdd(new Season\Name("2016/2017"), new Period(Carbon::create(2016, 9, 1, 0), Carbon::create(2017, 7, 1, 0))); $oSeason = Service::getBus()->handle($command); $command = new Command\LeagueAdd(new League\Name("eredivisie"), new League\Abbreviation("ere")); $oLeague = Service::getBus()->handle($command); } catch (\Exception $e) { echo $e->getMessage() . PHP_EOL; } return; // @TODO competitionadd-command should check if the combination does not exists, entitymanager should be injected in the command $command = new Command\CompetitionAdd($oLeague, $oSeason); $command->putAssociation(); $command->putPromotionRule();