Dave Jarvis' Repositories

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

class HtmlTagRenderer implements TagRenderer {
  private string $repoSafeName;

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

  public function renderTagItem(
    string $name,
    string $sha,
    string $targetSha,
    ?string $prevTargetSha,
    int $timestamp,
    string $message,
    string $author
  ): void {
    $filesUrl = (new UrlBuilder())
      ->withRepo( $this->repoSafeName )
      ->withAction( 'tree' )
      ->withHash( $name )
      ->build();

    $commitUrl = (new UrlBuilder())
      ->withRepo( $this->repoSafeName )
      ->withAction( 'commit' )
      ->withHash( $targetSha )
      ->build();

    if( $prevTargetSha ) {
      $diffUrl = (new UrlBuilder())
        ->withRepo( $this->repoSafeName )
        ->withAction( 'compare' )
        ->withHash( $targetSha )
        ->withName( $prevTargetSha )
        ->build();
    } else {
      $diffUrl = $commitUrl;
    }

    echo '<tr>';
    echo '<td class="tag-name">';
    echo '<a href="' . $filesUrl . '"><i class="fas fa-tag"></i> ' .
         htmlspecialchars( $name ) . '</a>';
    echo '</td>';
    echo '<td class="tag-message">';

    echo ($message !== '') ? htmlspecialchars( strtok( $message, "\n" ) ) :
      '<span style="color: #484f58; font-style: italic;">No description</span>';

    echo '</td>';
    echo '<td class="tag-author">' . htmlspecialchars( $author ) . '</td>';
    echo '<td class="tag-time">';
    $this->renderTime( $timestamp );
    echo '</td>';
    echo '<td class="tag-hash">';
    echo '<a href="' . $diffUrl . '" class="commit-hash">' .
         substr( $sha, 0, 7 ) . '</a>';
    echo '</td>';
    echo '</tr>';
  }

  public function renderTime( int $timestamp ): void {
    if( !$timestamp ) {
      echo 'never';
      return;
    }

    $diff = time() - $timestamp;

    if( $diff < 5 ) {
      echo 'just now';
      return;
    }

    $tokens = [
      31536000 => 'year',
      2592000 => 'month',
      604800 => 'week',
      86400 => 'day',
      3600 => 'hour',
      60 => 'minute',
      1 => 'second'
    ];

    foreach( $tokens as $unit => $text ) {
      if( $diff < $unit ) {
        continue;
      }

      $num = floor( $diff / $unit );

      echo $num . ' ' . $text . ($num > 1 ? 's' : '') . ' ago';
      return;
    }
  }
}