/**
  * Returns the next pending task.
  *
  * @param string|null $type
  *
  * @return TaskModel|null|false
  */
 public function getNextPendingTask($type = null)
 {
     // If a type was passed, we don't need to actually save it, as it's probably not an actual task-running request.
     if ($type) {
         $pendingTasks = $this->getPendingTasks($type, 1);
         if ($pendingTasks) {
             return $pendingTasks[0];
         }
     } else {
         if (!isset($this->_nextPendingTask)) {
             $taskRecord = TaskRecord::model()->roots()->ordered()->findByAttributes(array('status' => TaskStatus::Pending));
             if ($taskRecord) {
                 $this->_taskRecordsById[$taskRecord->id] = $taskRecord;
                 $this->_nextPendingTask = TaskModel::populateModel($taskRecord);
             } else {
                 $this->_nextPendingTask = false;
             }
         }
         if ($this->_nextPendingTask) {
             return $this->_nextPendingTask;
         }
     }
 }