[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/harbormaster/storage/build/ -> HarbormasterBuildArtifact.php (source)

   1  <?php
   2  
   3  final class HarbormasterBuildArtifact extends HarbormasterDAO
   4    implements PhabricatorPolicyInterface {
   5  
   6    protected $buildTargetPHID;
   7    protected $artifactType;
   8    protected $artifactIndex;
   9    protected $artifactKey;
  10    protected $artifactData = array();
  11  
  12    private $buildTarget = self::ATTACHABLE;
  13  
  14    const TYPE_FILE = 'file';
  15    const TYPE_HOST = 'host';
  16    const TYPE_URI = 'uri';
  17  
  18    public static function initializeNewBuildArtifact(
  19      HarbormasterBuildTarget $build_target) {
  20      return id(new HarbormasterBuildArtifact())
  21        ->setBuildTargetPHID($build_target->getPHID());
  22    }
  23  
  24    public function getConfiguration() {
  25      return array(
  26        self::CONFIG_SERIALIZATION => array(
  27          'artifactData' => self::SERIALIZATION_JSON,
  28        ),
  29        self::CONFIG_COLUMN_SCHEMA => array(
  30          'artifactType' => 'text32',
  31          'artifactIndex' => 'bytes12',
  32          'artifactKey' => 'text255',
  33        ),
  34        self::CONFIG_KEY_SCHEMA => array(
  35          'key_artifact' => array(
  36            'columns' => array('artifactType', 'artifactIndex'),
  37            'unique' => true,
  38          ),
  39          'key_garbagecollect' => array(
  40            'columns' => array('artifactType', 'dateCreated'),
  41          ),
  42        ),
  43      ) + parent::getConfiguration();
  44    }
  45  
  46    public function attachBuildTarget(HarbormasterBuildTarget $build_target) {
  47      $this->buildTarget = $build_target;
  48      return $this;
  49    }
  50  
  51    public function getBuildTarget() {
  52      return $this->assertAttached($this->buildTarget);
  53    }
  54  
  55    public function setArtifactKey($build_phid, $build_gen, $key) {
  56      $this->artifactIndex =
  57        PhabricatorHash::digestForIndex($build_phid.$build_gen.$key);
  58      $this->artifactKey = $key;
  59      return $this;
  60    }
  61  
  62    public function getObjectItemView(PhabricatorUser $viewer) {
  63      $data = $this->getArtifactData();
  64      switch ($this->getArtifactType()) {
  65        case self::TYPE_FILE:
  66          $handle = id(new PhabricatorHandleQuery())
  67            ->setViewer($viewer)
  68            ->withPHIDs($data)
  69            ->executeOne();
  70  
  71          return id(new PHUIObjectItemView())
  72            ->setObjectName(pht('File'))
  73            ->setHeader($handle->getFullName())
  74            ->setHref($handle->getURI());
  75        case self::TYPE_HOST:
  76          $leases = id(new DrydockLeaseQuery())
  77            ->setViewer($viewer)
  78            ->withIDs(array($data['drydock-lease']))
  79            ->execute();
  80          $lease = $leases[$data['drydock-lease']];
  81  
  82          return id(new PHUIObjectItemView())
  83            ->setObjectName(pht('Drydock Lease'))
  84            ->setHeader($lease->getID())
  85            ->setHref('/drydock/lease/'.$lease->getID());
  86        case self::TYPE_URI:
  87          return id(new PHUIObjectItemView())
  88            ->setObjectName($data['name'])
  89            ->setHeader($data['uri'])
  90            ->setHref($data['uri']);
  91        default:
  92          return null;
  93      }
  94    }
  95  
  96    public function loadDrydockLease() {
  97      if ($this->getArtifactType() !== self::TYPE_HOST) {
  98        throw new Exception(
  99          '`loadDrydockLease` may only be called on host artifacts.');
 100      }
 101  
 102      $data = $this->getArtifactData();
 103  
 104      // FIXME: Is there a better way of doing this?
 105      // TODO: Policy stuff, etc.
 106      $lease = id(new DrydockLease())->load(
 107        $data['drydock-lease']);
 108      if ($lease === null) {
 109        throw new Exception('Associated Drydock lease not found!');
 110      }
 111      $resource = id(new DrydockResource())->load(
 112        $lease->getResourceID());
 113      if ($resource === null) {
 114        throw new Exception('Associated Drydock resource not found!');
 115      }
 116      $lease->attachResource($resource);
 117  
 118      return $lease;
 119    }
 120  
 121    public function loadPhabricatorFile() {
 122      if ($this->getArtifactType() !== self::TYPE_FILE) {
 123        throw new Exception(
 124          '`loadPhabricatorFile` may only be called on file artifacts.');
 125      }
 126  
 127      $data = $this->getArtifactData();
 128  
 129      // The data for TYPE_FILE is an array with a single PHID in it.
 130      $phid = $data['filePHID'];
 131  
 132      $file = id(new PhabricatorFileQuery())
 133        ->setViewer(PhabricatorUser::getOmnipotentUser())
 134        ->withPHIDs(array($phid))
 135        ->executeOne();
 136      if ($file === null) {
 137        throw new Exception('Associated file not found!');
 138      }
 139      return $file;
 140    }
 141  
 142    public function release() {
 143      switch ($this->getArtifactType()) {
 144        case self::TYPE_HOST:
 145          $this->releaseDrydockLease();
 146          break;
 147      }
 148    }
 149  
 150    public function releaseDrydockLease() {
 151      $lease = $this->loadDrydockLease();
 152      $resource = $lease->getResource();
 153      $blueprint = $resource->getBlueprint();
 154  
 155      if ($lease->isActive()) {
 156        $blueprint->releaseLease($resource, $lease);
 157      }
 158    }
 159  
 160  
 161  /* -(  PhabricatorPolicyInterface  )----------------------------------------- */
 162  
 163  
 164    public function getCapabilities() {
 165      return array(
 166        PhabricatorPolicyCapability::CAN_VIEW,
 167      );
 168    }
 169  
 170    public function getPolicy($capability) {
 171      return $this->getBuildTarget()->getPolicy($capability);
 172    }
 173  
 174    public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
 175      return $this->getBuildTarget()->hasAutomaticCapability(
 176        $capability,
 177        $viewer);
 178    }
 179  
 180    public function describeAutomaticCapability($capability) {
 181      return pht(
 182        'Users must be able to see a buildable to see its artifacts.');
 183    }
 184  
 185  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1