Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/treetrek.git
<?php
class UrlBuilder {
  private $repo;
  private $action;
  private $hash;
  private $name;
  private $switcher;

  public function withRepo( $repo ) {
    $this->repo = $repo;
    return $this;
  }

  public function withAction( $action ) {
    $this->action = $action;
    return $this;
  }

  public function withHash( $hash ) {
    $this->hash = $hash;
    return $this;
  }

  public function withName( $name ) {
    $this->name = $name;
    return $this;
  }

  public function withSwitcher( $jsValue ) {
    $this->switcher = $jsValue;
    return $this;
  }

  public function build() {
    if( $this->switcher ) {
      return "window.location.href='/repo/' + " . $this->switcher;
    }

    if( !$this->repo ) {
      return '/';
    }

    $url = '/repo/' . $this->repo;

    if( !$this->action && $this->name ) {
      $this->action = 'tree';
    }

    if( $this->action ) {
      $url .= '/' . $this->action;

      if( $this->hash ) {
         $url .= '/' . $this->hash;
      } elseif( in_array( $this->action, ['tree', 'blob', 'raw', 'commits'] ) ) {
         $url .= '/HEAD';
      }
    }

    if( $this->name ) {
      $url .= '/' . ltrim( $this->name, '/' );
    }

    return $url;
  }
}