public function toArray()
 {
     $out = array('job' => $this->job->toArray(), 'mostRecentEventCollection' => array(), 'overallStatus' => $this->getOverallStatus());
     foreach ($this->mostRecentEventCollection as $key => $statusEvent) {
         $out['mostRecentEventCollection'][$key] = $statusEvent->toArray();
     }
     return $out;
 }
 /**
  *
  * @param Job $job            
  * @param string $componentId            
  * @param string $status            
  * @param string $createdAt            
  * @param string $detailedMessage            
  * @throws InvalidArgumentException
  */
 public function __construct(Job $job, $eventId, $componentId, $status, $createdAt, $detailedMessage)
 {
     $this->jobId = $job->getJobId();
     if (!in_array($componentId, $job->getComponents())) {
         throw new InvalidArgumentException(self::EXC_INVALID_COMPONENT_ID_PROVDED_MESSAGE, self::EXC_INVALID_COMPONENT_ID_PROVIDED_CODE);
     }
     $this->componentId = $componentId;
     $validStatusArr = array(0 => self::STATUS_QUEUED, 1 => self::STATUS_IN_PROGRESS, 2 => self::STATUS_FAILED, 3 => self::STATUS_COMPLETED);
     if (!in_array($status, $validStatusArr)) {
         throw new InvalidArgumentException(self::EXC_INVALID_STATUS_PROVIDED_MESSAGE, self::EXC_INVALID_STATUS_PROVIDED_CODE);
     }
     $this->status = $status;
     $this->detailedMessage = (string) $detailedMessage;
     $this->createdAt = $createdAt;
     $this->eventId = $eventId;
 }
 /**
  *
  * {@inheritdoc}
  *
  * @see \ImmutableStateStatusTracker\StorageAdapterInterface::createJob()
  */
 public function createJob(array $componentList)
 {
     $jobId = uniqid($this->entropyPrefixString, true);
     $job = new Job($jobId, $componentList, time());
     $path = $this->path . DIRECTORY_SEPARATOR . $job->getJobId() . ".json";
     $out = file_put_contents($path, serialize($job), LOCK_EX);
     if ($out === false) {
         throw new StorageAdapterException(self::PERMISSION_PROBLEM_MESSAGE . $path, self::PERMISSION_PROBLEM_CODE);
     }
     return $job;
 }
 /**
  *
  * {@inheritdoc}
  *
  * @see \ImmutableStateStatusTracker\StorageAdapterInterface::fetchStatusEvents()
  */
 public function fetchStatusEvents(Job $job)
 {
     $dJob = $this->_em->find('\\ImmutableStateStatusTracker\\StorageAdapter\\Doctrine\\Job', $job->getJobId());
     if (!$job instanceof Job) {
         throw new StorageAdapterException(self::EXC_JOB_NOT_FOUND_MESSAGE, self::EXC_JOB_NOT_FOUND_CODE);
     }
     $results = $this->_em->getRepository('\\ImmutableStateStatusTracker\\StorageAdapter\\Doctrine\\StatusEvent')->findBy(array('_job' => $dJob));
     $out = array();
     /**
      *
      * @var DoctrineStatusEvent $dStatusEvent
      */
     foreach ($results as $dStatusEvent) {
         $statusEvent = new StatusEvent($job, $dStatusEvent->getStatusId(), $dStatusEvent->getComponentId(), $dStatusEvent->getStatus(), $dStatusEvent->getCreatedAt(), $dStatusEvent->getDetailedMessage());
         $out[] = $statusEvent;
     }
     return $out;
 }
 protected function calculateOverallJobStatusFromMostRecentEventCollection(Job $job, array $mostRecentEventCollection)
 {
     $countQueued = 0;
     $countComplete = 0;
     $countInProgress = 0;
     $countFailed = 0;
     foreach ($mostRecentEventCollection as $statusEvent) {
         /**
          *
          * @var StatusEvent $statusEvent
          */
         switch ($statusEvent->getStatus()) {
             case StatusEvent::STATUS_QUEUED:
                 $countQueued++;
                 break;
             case StatusEvent::STATUS_IN_PROGRESS:
                 $countInProgress++;
                 break;
             case StatusEvent::STATUS_COMPLETED:
                 $countComplete++;
                 break;
             case StatusEvent::STATUS_FAILED:
                 $countFailed++;
                 break;
             default:
                 $countFailed++;
         }
     }
     // fail early!
     if ($countFailed >= 1) {
         if ($countFailed == count($job->getComponents())) {
             return CalculatedJobStatus::STATUS_FAILED;
         }
         if ($countFailed + $countComplete == count($job->getComponents())) {
             return CalculatedJobStatus::STATUS_FAILED;
         }
         return CalculatedJobStatus::STATUS_FAILED_AND_STARTED;
     }
     // if things are only queued but not processing
     if ($countQueued >= 1 && $countInProgress === 0) {
         return CalculatedJobStatus::STATUS_QUEUED;
     }
     // if things are in-progress
     if ($countInProgress >= 1 || $countComplete < count($job->getComponents())) {
         return CalculatedJobStatus::STATUS_IN_PROGRESS;
     }
     // if things are complete but not in-progress
     if ($countComplete === count($job->getComponents()) && $countInProgress === 0) {
         return CalculatedJobStatus::STATUS_COMPLETED;
     }
     throw new CalculatingJobStatusException("An edge case was hit when calculating job status.");
 }