Knp\Bundle\KnpBundlesBundle\Travis\Travis::update PHP Method

update() public method

Updates repo based on status from travis.
public update ( Bundle $bundle ) : boolean
$bundle Knp\Bundle\KnpBundlesBundle\Entity\Bundle
return boolean
    public function update(Bundle $bundle)
    {
        $this->output->write(' Travis status:');
        $response = $this->browser->get('http://travis-ci.org/' . $bundle->getOwnerName() . '/' . $bundle->getName() . '.json');
        $status = json_decode($response->getContent(), true);
        if (JSON_ERROR_NONE === json_last_error()) {
            $lastBuildAt = new \DateTime();
            $lastBuildAt->setTimestamp(strtotime($status['last_build_finished_at']));
            // Only execute if date of last build is older than last update of bundle
            if ($lastBuildAt < $bundle->getUpdatedAt()) {
                $state = Activity::STATE_UNKNOWN;
                if (0 === $status['last_build_status']) {
                    $bundle->setTravisCiBuildStatus(true);
                    $state = Activity::STATE_OPEN;
                    $this->output->write(' success');
                } elseif (1 === $status['last_build_status']) {
                    $bundle->setTravisCiBuildStatus(false);
                    $state = Activity::STATE_CLOSED;
                    $this->output->write(' failed');
                }
                if (Activity::STATE_UNKNOWN !== $state) {
                    $activity = new Activity();
                    $activity->setType(Activity::ACTIVITY_TYPE_TRAVIS_BUILD);
                    $activity->setState($state);
                    $activity->setBundle($bundle);
                    return true;
                }
            } else {
                $this->output->write(' skipped');
                return true;
            }
        }
        $bundle->setTravisCiBuildStatus(null);
        $this->output->write(' error');
        return false;
    }

Usage Example

 /**
  * Callback called from RabbitMQ to update a bundle
  *
  * @param AMQPMessage $msg serialized Message
  */
 public function execute(AMQPMessage $msg)
 {
     // Retrieve informations from the message
     $message = json_decode($msg->body, true);
     if (!isset($message['bundle_id'])) {
         if ($this->logger) {
             $this->logger->err('Bundle id is missing : skip message');
         }
         return;
     }
     $bundles = $this->em->getRepository('Knp\\Bundle\\KnpBundlesBundle\\Entity\\Bundle');
     // Retrieve Bundle from database
     /* @var $bundle Bundle */
     if (!($bundle = $bundles->find($message['bundle_id']))) {
         if ($this->logger) {
             $this->logger->warn(sprintf('Unable to retrieve bundle #%d', $message['bundle_id']));
         }
         return;
     }
     if (isset($message['action']) && 'remove' == $message['action']) {
         if ($this->logger) {
             $this->logger->warn(sprintf('Bundle "%s" will be removed', $bundle->getName()));
         }
         $this->removeBundle($bundle);
         return;
     }
     if ($this->logger) {
         $this->logger->info(sprintf('Retrieved bundle %s', $bundle->getName()));
     }
     $keywordRepo = $this->em->getRepository('Knp\\Bundle\\KnpBundlesBundle\\Entity\\Keyword');
     $scoreRepo = $this->em->getRepository('Knp\\Bundle\\KnpBundlesBundle\\Entity\\Score');
     for ($i = 0; $i < self::MAX_GITHUB_TRIALS; $i++) {
         try {
             if (!$this->githubRepoApi->update($bundle)) {
                 if ($this->logger) {
                     $this->logger->warn(sprintf('Update of "%s" failed', $bundle->getName()));
                 }
                 return;
             }
             $this->indexer->indexBundle($bundle);
             $this->updateContributors($bundle);
             $this->updateKeywords($bundle, $keywordRepo);
             $this->updateScore($bundle, $scoreRepo);
             if ($bundle->getUsesTravisCi()) {
                 $this->travis->update($bundle);
             }
         } catch (ApiLimitExceedException $e) {
             if ($this->logger) {
                 $this->logger->err(sprintf('Bundle %s got a %s for trial %s', $bundle->getName(), $e->getMessage(), $i + 1));
             }
             sleep(60 * ($i + 1));
             continue;
         } catch (\Exception $e) {
             if ($this->logger) {
                 $this->logger->err('[' . get_class($e) . ' / ' . $e->getFile() . ':' . $e->getLine() . '] ' . $e->getMessage());
             }
         }
         break;
     }
 }