Path: blob/master/src/applications/drydock/query/DrydockResourceQuery.php
12256 views
<?php12final class DrydockResourceQuery extends DrydockQuery {34private $ids;5private $phids;6private $statuses;7private $types;8private $blueprintPHIDs;9private $datasourceQuery;10private $needUnconsumedCommands;1112public function withIDs(array $ids) {13$this->ids = $ids;14return $this;15}1617public function withPHIDs(array $phids) {18$this->phids = $phids;19return $this;20}2122public function withTypes(array $types) {23$this->types = $types;24return $this;25}2627public function withStatuses(array $statuses) {28$this->statuses = $statuses;29return $this;30}3132public function withBlueprintPHIDs(array $blueprint_phids) {33$this->blueprintPHIDs = $blueprint_phids;34return $this;35}3637public function withDatasourceQuery($query) {38$this->datasourceQuery = $query;39return $this;40}4142public function needUnconsumedCommands($need) {43$this->needUnconsumedCommands = $need;44return $this;45}4647public function newResultObject() {48return new DrydockResource();49}5051protected function willFilterPage(array $resources) {52$blueprint_phids = mpull($resources, 'getBlueprintPHID');5354$blueprints = id(new DrydockBlueprintQuery())55->setViewer($this->getViewer())56->withPHIDs($blueprint_phids)57->execute();58$blueprints = mpull($blueprints, null, 'getPHID');5960foreach ($resources as $key => $resource) {61$blueprint = idx($blueprints, $resource->getBlueprintPHID());62if (!$blueprint) {63$this->didRejectResult($resource);64unset($resources[$key]);65continue;66}67$resource->attachBlueprint($blueprint);68}6970return $resources;71}7273protected function didFilterPage(array $resources) {74if ($this->needUnconsumedCommands) {75$commands = id(new DrydockCommandQuery())76->setViewer($this->getViewer())77->setParentQuery($this)78->withTargetPHIDs(mpull($resources, 'getPHID'))79->withConsumed(false)80->execute();81$commands = mgroup($commands, 'getTargetPHID');8283foreach ($resources as $resource) {84$list = idx($commands, $resource->getPHID(), array());85$resource->attachUnconsumedCommands($list);86}87}8889return $resources;90}9192protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {93$where = parent::buildWhereClauseParts($conn);9495if ($this->ids !== null) {96$where[] = qsprintf(97$conn,98'resource.id IN (%Ld)',99$this->ids);100}101102if ($this->phids !== null) {103$where[] = qsprintf(104$conn,105'resource.phid IN (%Ls)',106$this->phids);107}108109if ($this->types !== null) {110$where[] = qsprintf(111$conn,112'resource.type IN (%Ls)',113$this->types);114}115116if ($this->statuses !== null) {117$where[] = qsprintf(118$conn,119'resource.status IN (%Ls)',120$this->statuses);121}122123if ($this->blueprintPHIDs !== null) {124$where[] = qsprintf(125$conn,126'resource.blueprintPHID IN (%Ls)',127$this->blueprintPHIDs);128}129130if ($this->datasourceQuery !== null) {131$where[] = qsprintf(132$conn,133'resource.name LIKE %>',134$this->datasourceQuery);135}136137return $where;138}139140protected function getPrimaryTableAlias() {141return 'resource';142}143144}145146147