Path: blob/master/src/applications/drydock/query/DrydockLeaseQuery.php
12256 views
<?php12final class DrydockLeaseQuery extends DrydockQuery {34private $ids;5private $phids;6private $resourcePHIDs;7private $ownerPHIDs;8private $statuses;9private $datasourceQuery;10private $needUnconsumedCommands;11private $minModified;12private $maxModified;1314public function withIDs(array $ids) {15$this->ids = $ids;16return $this;17}1819public function withPHIDs(array $phids) {20$this->phids = $phids;21return $this;22}2324public function withResourcePHIDs(array $phids) {25$this->resourcePHIDs = $phids;26return $this;27}2829public function withOwnerPHIDs(array $phids) {30$this->ownerPHIDs = $phids;31return $this;32}3334public function withStatuses(array $statuses) {35$this->statuses = $statuses;36return $this;37}3839public function withDatasourceQuery($query) {40$this->datasourceQuery = $query;41return $this;42}4344public function withDateModifiedBetween($min_epoch, $max_epoch) {45$this->minModified = $min_epoch;46$this->maxModified = $max_epoch;47return $this;48}4950public function needUnconsumedCommands($need) {51$this->needUnconsumedCommands = $need;52return $this;53}5455public function newResultObject() {56return new DrydockLease();57}5859protected function willFilterPage(array $leases) {60$resource_phids = array_filter(mpull($leases, 'getResourcePHID'));61if ($resource_phids) {62$resources = id(new DrydockResourceQuery())63->setParentQuery($this)64->setViewer($this->getViewer())65->withPHIDs(array_unique($resource_phids))66->execute();67$resources = mpull($resources, null, 'getPHID');68} else {69$resources = array();70}7172foreach ($leases as $key => $lease) {73$resource = null;74if ($lease->getResourcePHID()) {75$resource = idx($resources, $lease->getResourcePHID());76if (!$resource) {77$this->didRejectResult($lease);78unset($leases[$key]);79continue;80}81}82$lease->attachResource($resource);83}8485return $leases;86}8788protected function didFilterPage(array $leases) {89if ($this->needUnconsumedCommands) {90$commands = id(new DrydockCommandQuery())91->setViewer($this->getViewer())92->setParentQuery($this)93->withTargetPHIDs(mpull($leases, 'getPHID'))94->withConsumed(false)95->execute();96$commands = mgroup($commands, 'getTargetPHID');9798foreach ($leases as $lease) {99$list = idx($commands, $lease->getPHID(), array());100$lease->attachUnconsumedCommands($list);101}102}103104return $leases;105}106107protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {108$where = parent::buildWhereClauseParts($conn);109110if ($this->resourcePHIDs !== null) {111$where[] = qsprintf(112$conn,113'resourcePHID IN (%Ls)',114$this->resourcePHIDs);115}116117if ($this->ownerPHIDs !== null) {118$where[] = qsprintf(119$conn,120'ownerPHID IN (%Ls)',121$this->ownerPHIDs);122}123124if ($this->ids !== null) {125$where[] = qsprintf(126$conn,127'id IN (%Ld)',128$this->ids);129}130131if ($this->phids !== null) {132$where[] = qsprintf(133$conn,134'phid IN (%Ls)',135$this->phids);136}137138if ($this->statuses !== null) {139$where[] = qsprintf(140$conn,141'status IN (%Ls)',142$this->statuses);143}144145if ($this->datasourceQuery !== null) {146$where[] = qsprintf(147$conn,148'id = %d',149(int)$this->datasourceQuery);150}151152if ($this->minModified !== null) {153$where[] = qsprintf(154$conn,155'dateModified >= %d',156$this->minModified);157}158159if ($this->maxModified !== null) {160$where[] = qsprintf(161$conn,162'dateModified <= %d',163$this->maxModified);164}165166return $where;167}168169}170171172