Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/treetrek.git
<?php
require_once __DIR__ . '/CommitRenderer.php';
require_once __DIR__ . '/../model/UrlBuilder.php';

class HtmlCommitRenderer implements CommitRenderer {
  private string $repoSafeName;

  public function __construct( string $repoSafeName ) {
    $this->repoSafeName = $repoSafeName;
  }

  public function render(
    string $sha,
    string $message,
    string $author,
    int    $date
  ): void {
    $msg = htmlspecialchars( explode( "\n", $message )[0] );
    $url = (new UrlBuilder())
      ->withRepo( $this->repoSafeName )
      ->withAction( 'commit' )
      ->withHash( $sha )
      ->build();

    echo '<div class="commit-row">';
    echo '<a href="' . $url . '" class="sha">' .
         substr( $sha, 0, 7 ) . '</a>';
    echo '<span class="message">' . $msg . '</span>';
    echo '<span class="meta">' . htmlspecialchars( $author ) .
         ' &bull; ' . date( 'Y-m-d', $date ) . '</span>';
    echo '</div>';
  }

  public function renderTime( int $timestamp ): void {
    $tokens = [
      31536000 => 'year', 2592000 => 'month', 604800 => 'week',
      86400 => 'day', 3600 => 'hour', 60 => 'minute', 1 => 'second'
    ];
    $diff = $timestamp ? time() - $timestamp : null;
    $result = 'never';

    if( $diff && $diff >= 5 ) {
      foreach( $tokens as $unit => $text ) {
        if( $diff < $unit ) continue;
        $num = floor( $diff / $unit );
        $result = $num . ' ' . $text . ($num > 1 ? 's' : '') . ' ago';
        break;
      }
    } elseif( $diff ) {
      $result = 'just now';
    }

    echo $result;
  }
}