Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/almanac/storage/AlmanacBinding.php
12256 views
1
<?php
2
3
final class AlmanacBinding
4
extends AlmanacDAO
5
implements
6
PhabricatorPolicyInterface,
7
PhabricatorApplicationTransactionInterface,
8
AlmanacPropertyInterface,
9
PhabricatorDestructibleInterface,
10
PhabricatorExtendedPolicyInterface,
11
PhabricatorConduitResultInterface {
12
13
protected $servicePHID;
14
protected $devicePHID;
15
protected $interfacePHID;
16
protected $isDisabled;
17
18
private $service = self::ATTACHABLE;
19
private $device = self::ATTACHABLE;
20
private $interface = self::ATTACHABLE;
21
private $almanacProperties = self::ATTACHABLE;
22
23
public static function initializeNewBinding(AlmanacService $service) {
24
return id(new AlmanacBinding())
25
->setServicePHID($service->getPHID())
26
->attachService($service)
27
->attachAlmanacProperties(array())
28
->setIsDisabled(0);
29
}
30
31
protected function getConfiguration() {
32
return array(
33
self::CONFIG_AUX_PHID => true,
34
self::CONFIG_COLUMN_SCHEMA => array(
35
'isDisabled' => 'bool',
36
),
37
self::CONFIG_KEY_SCHEMA => array(
38
'key_service' => array(
39
'columns' => array('servicePHID', 'interfacePHID'),
40
'unique' => true,
41
),
42
'key_device' => array(
43
'columns' => array('devicePHID'),
44
),
45
'key_interface' => array(
46
'columns' => array('interfacePHID'),
47
),
48
),
49
) + parent::getConfiguration();
50
}
51
52
public function getPHIDType() {
53
return AlmanacBindingPHIDType::TYPECONST;
54
}
55
56
public function getName() {
57
return pht('Binding %s', $this->getID());
58
}
59
60
public function getURI() {
61
return urisprintf(
62
'/almanac/binding/%s/',
63
$this->getID());
64
}
65
66
public function getService() {
67
return $this->assertAttached($this->service);
68
}
69
70
public function attachService(AlmanacService $service) {
71
$this->service = $service;
72
return $this;
73
}
74
75
public function getDevice() {
76
return $this->assertAttached($this->device);
77
}
78
79
public function attachDevice(AlmanacDevice $device) {
80
$this->device = $device;
81
return $this;
82
}
83
84
public function hasInterface() {
85
return ($this->interface !== self::ATTACHABLE);
86
}
87
88
public function getInterface() {
89
return $this->assertAttached($this->interface);
90
}
91
92
public function attachInterface(AlmanacInterface $interface) {
93
$this->interface = $interface;
94
return $this;
95
}
96
97
98
/* -( AlmanacPropertyInterface )------------------------------------------- */
99
100
101
public function attachAlmanacProperties(array $properties) {
102
assert_instances_of($properties, 'AlmanacProperty');
103
$this->almanacProperties = mpull($properties, null, 'getFieldName');
104
return $this;
105
}
106
107
public function getAlmanacProperties() {
108
return $this->assertAttached($this->almanacProperties);
109
}
110
111
public function hasAlmanacProperty($key) {
112
$this->assertAttached($this->almanacProperties);
113
return isset($this->almanacProperties[$key]);
114
}
115
116
public function getAlmanacProperty($key) {
117
return $this->assertAttachedKey($this->almanacProperties, $key);
118
}
119
120
public function getAlmanacPropertyValue($key, $default = null) {
121
if ($this->hasAlmanacProperty($key)) {
122
return $this->getAlmanacProperty($key)->getFieldValue();
123
} else {
124
return $default;
125
}
126
}
127
128
public function getAlmanacPropertyFieldSpecifications() {
129
return $this->getService()->getBindingFieldSpecifications($this);
130
}
131
132
public function newAlmanacPropertyEditEngine() {
133
return new AlmanacBindingPropertyEditEngine();
134
}
135
136
public function getAlmanacPropertySetTransactionType() {
137
return AlmanacBindingSetPropertyTransaction::TRANSACTIONTYPE;
138
}
139
140
public function getAlmanacPropertyDeleteTransactionType() {
141
return AlmanacBindingDeletePropertyTransaction::TRANSACTIONTYPE;
142
}
143
144
145
/* -( PhabricatorPolicyInterface )----------------------------------------- */
146
147
148
public function getCapabilities() {
149
return array(
150
PhabricatorPolicyCapability::CAN_VIEW,
151
PhabricatorPolicyCapability::CAN_EDIT,
152
);
153
}
154
155
public function getPolicy($capability) {
156
return $this->getService()->getPolicy($capability);
157
}
158
159
public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
160
return $this->getService()->hasAutomaticCapability($capability, $viewer);
161
}
162
163
public function describeAutomaticCapability($capability) {
164
$notes = array(
165
pht('A binding inherits the policies of its service.'),
166
pht(
167
'To view a binding, you must also be able to view its device and '.
168
'interface.'),
169
);
170
171
return $notes;
172
}
173
174
175
/* -( PhabricatorExtendedPolicyInterface )--------------------------------- */
176
177
178
public function getExtendedPolicy($capability, PhabricatorUser $viewer) {
179
switch ($capability) {
180
case PhabricatorPolicyCapability::CAN_EDIT:
181
if ($this->getService()->isClusterService()) {
182
return array(
183
array(
184
new PhabricatorAlmanacApplication(),
185
AlmanacManageClusterServicesCapability::CAPABILITY,
186
),
187
);
188
}
189
break;
190
}
191
192
return array();
193
}
194
195
/* -( PhabricatorApplicationTransactionInterface )------------------------- */
196
197
198
public function getApplicationTransactionEditor() {
199
return new AlmanacBindingEditor();
200
}
201
202
public function getApplicationTransactionTemplate() {
203
return new AlmanacBindingTransaction();
204
}
205
206
207
/* -( PhabricatorDestructibleInterface )----------------------------------- */
208
209
210
public function destroyObjectPermanently(
211
PhabricatorDestructionEngine $engine) {
212
213
$this->delete();
214
}
215
216
217
/* -( PhabricatorConduitResultInterface )---------------------------------- */
218
219
220
public function getFieldSpecificationsForConduit() {
221
return array(
222
id(new PhabricatorConduitSearchFieldSpecification())
223
->setKey('servicePHID')
224
->setType('phid')
225
->setDescription(pht('The bound service.')),
226
id(new PhabricatorConduitSearchFieldSpecification())
227
->setKey('devicePHID')
228
->setType('phid')
229
->setDescription(pht('The device the service is bound to.')),
230
id(new PhabricatorConduitSearchFieldSpecification())
231
->setKey('interfacePHID')
232
->setType('phid')
233
->setDescription(pht('The interface the service is bound to.')),
234
id(new PhabricatorConduitSearchFieldSpecification())
235
->setKey('disabled')
236
->setType('bool')
237
->setDescription(pht('Interface status.')),
238
);
239
}
240
241
public function getFieldValuesForConduit() {
242
return array(
243
'servicePHID' => $this->getServicePHID(),
244
'devicePHID' => $this->getDevicePHID(),
245
'interfacePHID' => $this->getInterfacePHID(),
246
'disabled' => (bool)$this->getIsDisabled(),
247
);
248
}
249
250
public function getConduitSearchAttachments() {
251
return array(
252
id(new AlmanacPropertiesSearchEngineAttachment())
253
->setAttachmentKey('properties'),
254
);
255
}
256
257
}
258
259