Exemplo n.º 1
0
 /**
  * Start repair a building
  *
  * @param Building $building
  * @param $type
  * @return int
  */
 public function repair(Building $building, $type)
 {
     $amount = $this->buildingRepairPrice->countPrice($building, $type);
     if (!$this->gamerHasEnoughCash->isSatisfiedBy($building->getGamer(), $amount)) {
         throw new NotEnoughCashException();
     }
     if ($this->buildingRepairRepository->countActiveBuildings($building) > 0) {
         throw new BuildingAlreadyRepairing();
     }
     $duration = $this->buildingRepairPrice->getTimeConstant($type) * (100 - $building->getCurrentCondition());
     $buildingRepair = new BuildingRepair();
     $buildingRepair->setBuilding($building);
     $buildingRepair->setStatus(BuildingRepair::STATUS_ACTIVE);
     $buildingRepair->setType($type);
     $buildingRepair->setValidDuration($duration);
     $buildingRepair->setStartedCondition($building->getCurrentCondition());
     $this->buildingRepairRepository->save($buildingRepair);
     $building->setStatus(Building::INACTIVE);
     $this->buildingRepository->update();
     $this->reduceCash->reduce($building->getGamer(), $amount);
     $event = new BuildingRepairStarted($building);
     $this->eventDispatcher->dispatch($event::NAME, $event);
 }
 function it_can_start_repairing_building(GamerHasEnoughCash $gamerHasEnoughCash, Building $building, Gamer $gamer, BuildingRepairRepository $buildingRepairRepository, BuildingRepairPrice $buildingRepairPrice, ReduceCash $reduceCash, EventDispatcher $eventDispatcher, BuildingRepository $buildingRepository)
 {
     $amount = 3600;
     $type = BuildingRepair::TYPE_FAST_ID;
     $buildingRepairPrice->countPrice($building, $type)->willReturn($amount);
     $building->getGamer()->willReturn($gamer);
     $gamerHasEnoughCash->isSatisfiedBy($gamer, $amount)->willReturn(true);
     $buildingRepairRepository->countActiveBuildings($building)->willReturn(0);
     $buildingRepairPrice->getTimeConstant($type)->willReturn(0.8);
     $building->getCurrentCondition()->willReturn(50);
     $building->setStatus(Building::INACTIVE)->shouldBeCalled();
     $buildingRepository->update()->shouldBeCalled();
     $buildingRepairRepository->save(Argument::type('Mm\\AppBundle\\Entity\\BuildingRepair'))->shouldBeCalled();
     $reduceCash->reduce($gamer, $amount)->shouldBeCalled();
     $eventDispatcher->dispatch(BuildingRepairStarted::NAME, Argument::type('Mm\\Model\\Event\\BuildingRepairStarted'))->shouldBeCalled();
     $this->repair($building, $type);
 }