/** @retrun QueueService */ private function route(ElevatorRequest $request) { // Insert current elevator floor as a starting point array_unshift($this->queue, $this->elevator->getCurrentFloor()); // Find the best place and insert source request (where to pick up) $position = $this->insertRequest(0, $request->getSourceFloor()); // Find the best place after picking-up to land $this->insertRequest($position, $request->getTargetFloor()); // Remove current elevator floor after successful routing array_shift($this->queue); return $this; }
/** @return Elevator */ public function addRequest(ElevatorRequest $request) { $this->process(); if (!$this->floorService->getFloorState($request->getSourceFloor())) { throw new Exception("Source floor is under maintenance."); } if (!$this->floorService->getFloorState($request->getTargetFloor())) { throw new Exception("Target floor is under maintenance."); } $this->queue = $this->queueService->addRequest($this->queue, $this, $request); $this->targetFloor = array_shift($this->queue); return $this; }