Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/drydock/query/DrydockLeaseQuery.php
12256 views
1
<?php
2
3
final class DrydockLeaseQuery extends DrydockQuery {
4
5
private $ids;
6
private $phids;
7
private $resourcePHIDs;
8
private $ownerPHIDs;
9
private $statuses;
10
private $datasourceQuery;
11
private $needUnconsumedCommands;
12
private $minModified;
13
private $maxModified;
14
15
public function withIDs(array $ids) {
16
$this->ids = $ids;
17
return $this;
18
}
19
20
public function withPHIDs(array $phids) {
21
$this->phids = $phids;
22
return $this;
23
}
24
25
public function withResourcePHIDs(array $phids) {
26
$this->resourcePHIDs = $phids;
27
return $this;
28
}
29
30
public function withOwnerPHIDs(array $phids) {
31
$this->ownerPHIDs = $phids;
32
return $this;
33
}
34
35
public function withStatuses(array $statuses) {
36
$this->statuses = $statuses;
37
return $this;
38
}
39
40
public function withDatasourceQuery($query) {
41
$this->datasourceQuery = $query;
42
return $this;
43
}
44
45
public function withDateModifiedBetween($min_epoch, $max_epoch) {
46
$this->minModified = $min_epoch;
47
$this->maxModified = $max_epoch;
48
return $this;
49
}
50
51
public function needUnconsumedCommands($need) {
52
$this->needUnconsumedCommands = $need;
53
return $this;
54
}
55
56
public function newResultObject() {
57
return new DrydockLease();
58
}
59
60
protected function willFilterPage(array $leases) {
61
$resource_phids = array_filter(mpull($leases, 'getResourcePHID'));
62
if ($resource_phids) {
63
$resources = id(new DrydockResourceQuery())
64
->setParentQuery($this)
65
->setViewer($this->getViewer())
66
->withPHIDs(array_unique($resource_phids))
67
->execute();
68
$resources = mpull($resources, null, 'getPHID');
69
} else {
70
$resources = array();
71
}
72
73
foreach ($leases as $key => $lease) {
74
$resource = null;
75
if ($lease->getResourcePHID()) {
76
$resource = idx($resources, $lease->getResourcePHID());
77
if (!$resource) {
78
$this->didRejectResult($lease);
79
unset($leases[$key]);
80
continue;
81
}
82
}
83
$lease->attachResource($resource);
84
}
85
86
return $leases;
87
}
88
89
protected function didFilterPage(array $leases) {
90
if ($this->needUnconsumedCommands) {
91
$commands = id(new DrydockCommandQuery())
92
->setViewer($this->getViewer())
93
->setParentQuery($this)
94
->withTargetPHIDs(mpull($leases, 'getPHID'))
95
->withConsumed(false)
96
->execute();
97
$commands = mgroup($commands, 'getTargetPHID');
98
99
foreach ($leases as $lease) {
100
$list = idx($commands, $lease->getPHID(), array());
101
$lease->attachUnconsumedCommands($list);
102
}
103
}
104
105
return $leases;
106
}
107
108
protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
109
$where = parent::buildWhereClauseParts($conn);
110
111
if ($this->resourcePHIDs !== null) {
112
$where[] = qsprintf(
113
$conn,
114
'resourcePHID IN (%Ls)',
115
$this->resourcePHIDs);
116
}
117
118
if ($this->ownerPHIDs !== null) {
119
$where[] = qsprintf(
120
$conn,
121
'ownerPHID IN (%Ls)',
122
$this->ownerPHIDs);
123
}
124
125
if ($this->ids !== null) {
126
$where[] = qsprintf(
127
$conn,
128
'id IN (%Ld)',
129
$this->ids);
130
}
131
132
if ($this->phids !== null) {
133
$where[] = qsprintf(
134
$conn,
135
'phid IN (%Ls)',
136
$this->phids);
137
}
138
139
if ($this->statuses !== null) {
140
$where[] = qsprintf(
141
$conn,
142
'status IN (%Ls)',
143
$this->statuses);
144
}
145
146
if ($this->datasourceQuery !== null) {
147
$where[] = qsprintf(
148
$conn,
149
'id = %d',
150
(int)$this->datasourceQuery);
151
}
152
153
if ($this->minModified !== null) {
154
$where[] = qsprintf(
155
$conn,
156
'dateModified >= %d',
157
$this->minModified);
158
}
159
160
if ($this->maxModified !== null) {
161
$where[] = qsprintf(
162
$conn,
163
'dateModified <= %d',
164
$this->maxModified);
165
}
166
167
return $where;
168
}
169
170
}
171
172