Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / drydock / query / DrydockResourceQuery.php
blob395e758a2390103e1e6fc9e2afbd3b07c7fb03c7
1 <?php
3 final class DrydockResourceQuery extends DrydockQuery {
5 private $ids;
6 private $phids;
7 private $statuses;
8 private $types;
9 private $blueprintPHIDs;
10 private $datasourceQuery;
11 private $needUnconsumedCommands;
13 public function withIDs(array $ids) {
14 $this->ids = $ids;
15 return $this;
18 public function withPHIDs(array $phids) {
19 $this->phids = $phids;
20 return $this;
23 public function withTypes(array $types) {
24 $this->types = $types;
25 return $this;
28 public function withStatuses(array $statuses) {
29 $this->statuses = $statuses;
30 return $this;
33 public function withBlueprintPHIDs(array $blueprint_phids) {
34 $this->blueprintPHIDs = $blueprint_phids;
35 return $this;
38 public function withDatasourceQuery($query) {
39 $this->datasourceQuery = $query;
40 return $this;
43 public function needUnconsumedCommands($need) {
44 $this->needUnconsumedCommands = $need;
45 return $this;
48 public function newResultObject() {
49 return new DrydockResource();
52 protected function willFilterPage(array $resources) {
53 $blueprint_phids = mpull($resources, 'getBlueprintPHID');
55 $blueprints = id(new DrydockBlueprintQuery())
56 ->setViewer($this->getViewer())
57 ->withPHIDs($blueprint_phids)
58 ->execute();
59 $blueprints = mpull($blueprints, null, 'getPHID');
61 foreach ($resources as $key => $resource) {
62 $blueprint = idx($blueprints, $resource->getBlueprintPHID());
63 if (!$blueprint) {
64 $this->didRejectResult($resource);
65 unset($resources[$key]);
66 continue;
68 $resource->attachBlueprint($blueprint);
71 return $resources;
74 protected function didFilterPage(array $resources) {
75 if ($this->needUnconsumedCommands) {
76 $commands = id(new DrydockCommandQuery())
77 ->setViewer($this->getViewer())
78 ->setParentQuery($this)
79 ->withTargetPHIDs(mpull($resources, 'getPHID'))
80 ->withConsumed(false)
81 ->execute();
82 $commands = mgroup($commands, 'getTargetPHID');
84 foreach ($resources as $resource) {
85 $list = idx($commands, $resource->getPHID(), array());
86 $resource->attachUnconsumedCommands($list);
90 return $resources;
93 protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
94 $where = parent::buildWhereClauseParts($conn);
96 if ($this->ids !== null) {
97 $where[] = qsprintf(
98 $conn,
99 'resource.id IN (%Ld)',
100 $this->ids);
103 if ($this->phids !== null) {
104 $where[] = qsprintf(
105 $conn,
106 'resource.phid IN (%Ls)',
107 $this->phids);
110 if ($this->types !== null) {
111 $where[] = qsprintf(
112 $conn,
113 'resource.type IN (%Ls)',
114 $this->types);
117 if ($this->statuses !== null) {
118 $where[] = qsprintf(
119 $conn,
120 'resource.status IN (%Ls)',
121 $this->statuses);
124 if ($this->blueprintPHIDs !== null) {
125 $where[] = qsprintf(
126 $conn,
127 'resource.blueprintPHID IN (%Ls)',
128 $this->blueprintPHIDs);
131 if ($this->datasourceQuery !== null) {
132 $where[] = qsprintf(
133 $conn,
134 'resource.name LIKE %>',
135 $this->datasourceQuery);
138 return $where;
141 protected function getPrimaryTableAlias() {
142 return 'resource';